Link Search Menu Expand Document

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:

  1. A logical network
  2. A computer's local network
  3. 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 = ‘173.194.40.19’
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:


Back to top

© , Learn Python 101 — All Rights Reserved - Terms of Use - Privacy Policy