- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program that Sends and Receives Message from Client
Python socket is a set of modules used for socket programming, which enables communication between processes over IP networks. Sockets in Python provide the backbone for most network programming tasks. By importing relevant modules, we can write Python programs to create client and server programs for different types of network applications such as web scraping, file transfer, email clients, and chat applications.
The "socket" module is the most important module of socket programming in Python. It provides different types of connection-oriented and connection-less sockets that implement specific protocol levels such as TCP, UDP, etc.
TCP (Transmission Control Protocol) sockets
TCP (Transmission Control Protocol) sockets are used for communication that requires reliable transmission of data. They follow a specific set of rules for data transmission, which helps in ensuring that the message sent is received by the intended recipient successfully. These types of sockets are preferred for streaming services like video calls, online gaming, file sharing, etc.
UDP (User Datagram Protocol) Sockets
UDP (User Datagram Protocol) sockets are used for communication that doesn’t require reliable transmission of data. These sockets enable fast transmission of data by firing data packets at full speed without worrying about whether the recipient gets them or not. This prevents data retransmission and reduces network congestion. Examples of such applications that use UDP sockets include video streaming, VoIP, and online gaming.
The following are the steps to be followed to start the communication between the server and the client.
Importing the Socket Module
First of all, we have to import the socket module in the Python environment as below.
Creating a Socket
Now we have to create a socket using the socket.socket() function with the below-mentioned code. In the code first argument socket.AF_INET specifies the address family (IPv4) and the second argument socket.SOCK_STREAM indicates that it's a TCP socket. We can also use a socket.SOCK_DGRAM for UDP sockets.
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Binding the Socket
Here, in this step, we are specifying the address and port on which the socket will listen for incoming connections. For example, if the socket is bound to the localhost address ('localhost') and port 5000 then the code will be as follows.
server_address = ('localhost', 5000) sock.bind(server_address)
Listening to the Connection
The listen() function of the socket method sets the socket to the listening mode, allowing it to accept incoming connections and to the listen() function we are assigning the argument 1 specifying the maximum number of pending connections that can be queued for acceptance.
The accept() function blocks until a client connects to the socket. It returns a new socket object representing the connection and the client's address. We can then handle the connection in a separate thread or process.
while True: connection, client_address = sock.accept()
Sending and Receiving Data
Once a connection is established, we can use the socket object to send and receive data. The recv() function reads data from the socket, and the sendall() function sends data to the client. The data is usually transmitted in bytes, so it's common to prefix strings with b to indicate bytes literals.
data = connection.recv(1024) connection.sendall(b'Hello, client!')
Closing the connection
After we have finished communicating with a client, we can close the connection by calling the close() method on the socket object.
Closing the Server Socket
Finally, when you no longer need the server socket, you can close it using the close() method.
Following is the complete example –
import socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = ('localhost', 5000) sock.bind(server_address) sock.listen(1) while True: connection, client_address = sock.accept() data = connection.recv(1024) connection.sendall(b'Hello, client!') connection.close() sock.close()
Kickstart Your Career
Get certified by completing the courseGet Started