Python Socket Programming - Server, Client Example

Good Day Learners! In our previous tutorial, we discussed about Python unittest module. Today we will look into python socket programming example. We will create python socket server and client applications.

Python Socket Programming

To understand python socket programming, we need to know about three interesting topics – Socket Server, Socket Client and Socket.

So, what is a server? Well, a server is a software that waits for client requests and serves or processes them accordingly.

On the other hand, a client is requester of this service. A client program request for some resources to the server and server responds to that request.

Socket is the endpoint of a bidirectional communications channel between server and client. Sockets may communicate within a process, between processes on the same machine, or between processes on different machines. For any communication with a remote program, we have to connect through a socket port.

The main objective of this socket programming tutorial is to get introduce you how socket server and client communicate with each other. You will also learn how to write python socket server program.

Python Socket Example

We have said earlier that a socket client requests for some resources to the socket server and the server responds to that request.

So we will design both server and client model so that each can communicate with them. The steps can be considered like this.

  1. Python socket server program executes at first and wait for any request
  2. Python socket client program will initiate the conversation at first.
  3. Then server program will response accordingly to client requests.
  4. Client program will terminate if user enters “bye” message. Server program will also terminate when client program terminates, this is optional and we can keep server program running indefinitely or terminate with some specific command in client request.

Python Socket Server

We will save python socket server program as To use python socket connection, we need to import socket module.

Then, sequentially we need to perform some task to establish connection between server and client.

We can obtain host address by using socket.gethostname() function. It is recommended to user port address above 1024 because port number lesser than 1024 are reserved for standard internet protocol.

See the below python socket server example code, the comments will help you to understand the code.

So our python socket server is running on port 5000 and it will wait for client request. If you want server to not quit when client connection is closed, just remove the if condition and break statement. Python while loop is used to run the server program indefinitely and keep waiting for client request.

Python Socket Client

We will save python socket client program as This program is similar to the server program, except binding.

The main difference between server and client program is, in server program, it needs to bind host address and port address together.

See the below python socket client example code, the comment will help you to understand the code.

Python Socket Programming Output

To see the output, first run the socket server program. Then run the socket client program. After that, write something from client program. Then again write reply from server program. At last, write bye from client program to terminate both program. Below short video will show how it worked on my test run of socket server and client example programs.

Python Socket Programming - Server, Client Example

Notice that socket server is running on port 5000 but client also requires a socket port to connect to the server. This port is assigned randomly by client connect call. In this case, it’s 57822.

So, that’s all for Python socket programming, python socket server and socket client example programs.

Reference: Official Documentation

By admin

Leave a Reply

%d bloggers like this: