Sockets in Python
What are Sockets?
Sockets provide communication between two channels. A network socket is a software structure that resides in a node of a computer network. It is responsible for providing an endpoint for the sake of sending and receiving messages. Sockets are the endpoints of a link that allows two-way communication between computer programs running on the network.
What is Socket API?
The socket API refers to an Inter-processing Communication (IPC) programming interface. It is responsible for creating an endpoint for communication between channels. This API provides a socket identifier that represents the communicational endpoint. There is the concept of client and server application while communication is going on between processes. During communication, the client application utilizes the connect() API and establishes a safe connection to the server to ensure the communication goes smoothly and securely. On the other hand, the server application connects with the client by using the accept() API to accept the connection request.
Socket Programming in Python
Socket programming in Python uses both sockets and socket API for sending a message over a network. This network can be in one of the following forms:
- A logical network
- A computer's local network
- Physically connected to an external network
Most commonly, socket programming helps send messages over the internet via an ISP. In socket programming, the server-side waits for the client side’s connection request to initiate communication.
Socket API Functions in Python
Python has a module to support socket programming, which uses the library, namely socket.py. The socket module of Python provides easy access to the Berkeley Software Distribution (BSD) socket interface. This interface provides its services for all modern Unix systems, Windows, macOS, and various other platforms. Following are some basic socket API functions and methods present in the socket module of Python:
- socket ( )
- bind ( )
- listen ( )
- connect ( )
- accept ( )
- send ( )
- recv ( )
- close ( )
The parameter types of these functions can be in the C interface.
Socket Example in Python
Python creates an object of the socket using the socket ( ) function. The one socket node prepares to listen on a specific port and IP to differentiate the communicating processes. Another socket node uses the other communicating node to request the connection. Following is the example code for creating a socket:
# socket programming in Python import socket port_number = 1122 host_ip = ‘126.96.36.199’ s = socket.socket ( socket.AF_INET, socket.SOCK_STREAM ) print ("Socket created successfully") s.conect ( ( host_ip , port_number ) ) print ("Socket successfully connected to the host")
Here, the first parameter of the function, AF_INET, represents the address family of IPV4. Moreover, the second parameter of the function, SOCK_STREAM, refers to the communication medium as the TCP Protocol, a connection-oriented protocol. Above is the example of connecting a socket to host IP at any specific port number using the connect ( ) function.
Client and Server Example in Python
Following is an example of a typical client and server program in Python:
# server side of socket programming import socket port_number = 1122 s = socket.socket ( ) print ( “ Socket is created successfully ”) s.bind ( ( ‘ ’, port_number ) ) print (“ The socket is bound to %s” % ( port_number ) ) s.listen ( ) print ( “ The server is listening to requests” ) while True: c, host_ip = s. accept ( ) print (“ following host is connecting “ , host_ip ) c.send ( “Your connection request is accepted” ) c.close ( )
Here, the bind function helps in binding the socket to a specific IP and Port. The first parameter of the bind () function is empty which enables the server to listen to requests from other computers on the network. Once the accept () function establishes the connection successfully, the server sends the client the connection message through send ().
# client side of socket programming import socket port_number = 1122 host_ip = ‘127.0.0.1’ #using localhost s = socket.socket ( ) print ( “ Socket is created successfully ”) s.conect ( ( host_ip , port_number ) ) print ( s.recv (1024 ) ) s.close ( )
The client can make a connection request using connect() function and using the server’s IP and port. After successfully creating the connection, it can receive messages from the server-side using the recv () function.
Moreover, Python allows to check the working of server and client-side by using the following commands in the terminal:
For the server-side:
$ python server.py
This code displays the socket creation and listening phase at the server in a terminal.
For the client-side:
$ telnet localhost 1122
This sample code displays the connection with the server and message at the client-side in a separate terminal.
Other useful articles:
- OOP in Python
- Python v2 vs Python v3
- Variables, Data Types, and Syntaxes in Python
- Operators, Booleans, and Tuples
- Loops and Statements in Python
- Python Functions and Modules
- Regular Expressions in Python
- Python Interfaces
- JSON Data and Python
- Pip and its Uses in Python
- File Handling in Python
- Searching and Sorting Algorithms in Python
- System Programming (Pipes &Threads etc.)
- Database Programming in Python
- Debugging with Assertion in Python
- Sockets in Python
- InterOp in Python
- Exception Handling in Python
- Environments in Python
- Foundation of Data Science
- Reinforcement Learning
- Python for AI
- Applied Text Mining in Python
- Python Iterations using Libraries
- NumPy vs SciPy
- Python Array Indexing and Slicing
- Python & Libraries