Socket Programming in Python

Socket Programming in Python

Last updated on 21st Sep 2020, Artciles, Blog

About author

Suresh (Sr Testing Manager )

He is a TOP-Rated Domain Expert with 11+ Years Of Experience, Also He is a Respective Technical Recruiter for Past 5 Years & Share's this Informative Articles For Freshers

(5.0) | 17377 Ratings 549

Socket programming is the way of enabling the communication for sending and receiving the data between the socket endpoints, by using the code logics. The sockets can be a node, such as a server and a single or multiple client systems. In Python, socket programming can be put into operation with the help of the socket library functions, and the transmission control protocol (TCP) is the protocol applied for this function by default.

 Socket

A socket is an endpoint to send or receive data hence we need a server and a client socket program for this purpose. For our example program, we will be hosting a server and client on the same machine. We can even send and receive via a server code. It is to be noted that we can have just one server program whereas we can have multiple client programs.

Transmission Control Protocol(TCP) is the default protocol that is chosen. TCP is reliable as the packets which are dropped in the network are detected and transmitted by the sender. TCP also provides in-order data delivery i.e., the order in which it is sent by the sender in a similar order is read by the application.

Subscribe For Free Demo

Error: Contact form not found.

Python Socket Server

Here we discuss about python socket client: Server.py

Have a look at the program below: server.py

Socket-Programming-in-Python

Line 3: Socket is a pre-installed library and an object will be created for that. This socket module is imported in the python program and it will be used as a socket function later in the program. We are creating an object here, which will call the socket function.

Line 4: socket.AF_INET corresponds to IPV4  (If one wishes to use IPV6, then socket.AF_INET6 can be used. This refers to the socket family. AF_INET specifies the protocol that will be used for communication. IPV4 and IPV6 are different transmission control ways or protocols of transmitting data via the network.

IPV4 and IPV6 are just different types of IP addresses. The socket.AF_INET corresponds to TCP. Transmission Control Protocol (TCP) works with the IP and defines how the computer sends data packets to each other.Next what is needed is TCP for that we will need a socket.SOCK_STREAM, which is the socket type. TCP is a connection-oriented protocol, essentially involving performing a three-way handshake connection with a server and a client. Then we provide a hostname where the client will connect.

Line 5: For the hostname, “localhost” or “127.0.01” or socket.gethostname() either of these can be used. It gets the IP address of the server.

The above are the two parameters that we define for the socket.

Line 6: We usually have many ports in our system so we need to make use of a non-used host port. Any four-digit can be used as mostly the lower digits are occupied.

Line 7: The bind() method, which binds the host and the port to a socket.

All these steps are needed to start our server. Now, we need to make the client. As mentioned before that we can have multiple clients.

Line 8: We have an option to mention the number of the client who can interact with the server. This is achieved by s.listen(6). It starts the TCP listener. The numerical represents the number of clients that can interact. If for example, the 7th client comes then it will be dropped.

Line 9: s.accept() will accept the request from the client and it returns two objects, one is the socket-client object and the other is the address. The IP address will be the address of the client where our client is present.

Line 10: This is more like a notification to the server about the connection that has been established.

Lastly, we print a statement that should be displayed when both the programs are running in the terminal. After we are done with our server program we will be writing out the client program.

Python Socket Client

Here we discuss about python socket client:client.py

Here is our program for the client: client.py

Socket-Programming-client-in-Python

The client program also has a similar socket, as we see in the case of the server program. Usually, the client program runs from the server program. It is not in the same machine as we have in this example here. With sockets, what we intend to achieve is to communicate via Python programs on a locally networked machine or even remotely networked machines.

Line 8: There is a binding() method in the server program, here will use the connect() method. The tuple here will take host and port in the method. We take the same port as we had used in our server.py

We need to run server.py and client.py in different terminals and this is how we will see the connection occur.

Output:

Socket-Programming-output

The screenshot is from two different terminals one opened in CMD and the other opened in PowerShell in Windows. The left window opens up the server.py program and the terminal on the right opens the client.py program.

We see the output printed in the server-side terminal.

Sending String from One File to the Another File

We will try sending a message from the client-side to the server-side. We shall write a message in the client.py program.

1. client.py

Here is our program for the client: client.py

Socket-Programming-client-py

Line 9 and Line 10: We set the message in a variable and send the message in the form of bytes and convert it or encode it in UTF-8 format. (UTF is a Unicode standard as all the contents of the message are characters and they come under the Unicode chart).We are supposed to encode and keep the data as small as possible, else the transmission would fail at some time.

Line 11: We close the connection.

In the server program, we will make the following changes.

python Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

2. server.py

Here is our program for the server: server.py

Socket-Programming-server-py

Line 11: In order to build the connection, we make use of socket client object. We are mentioning the buffer size by adding 1024 bytes(1 KB) to the method and we keep in the variable message.

Line 13: Then we are printing the method.

Output:

We open up both the terminals again and see the following output.

Socket-Programming-output

Conclusion – Socket Programming in Python

The basics of socket programming have been covered here but it has to be noted that it is a massive domain and there are many more things to cover in socket programming. Understanding the basics well would aid in dealing with complex client-server scenarios.

Are you looking training with Right Jobs?

Contact Us

Popular Courses