Python - Network Programming

Introduction:

In this tutorial, we are learning about Network Programming in Python. Python plays an important role in network programming. Python's standard operating system library supports networking concepts such as networking, data encoding, and decoding, and writing network services in Python is easier than using C++. Here we will understand the basics of Python network programming. Python provides two levels of network access, which are -

1. Low-Level Access:

In low-level access, you have access to the operating system's basic socket support. You can use clients and servers for both connection-oriented and connectionless protocols.

2. High-Level Access:

You can use the high-level access provided by Python libraries to access application-level network protocols. The protocols are FTP, HTTP, etc.

What is a Socket?

Consider a two-way communication channel; the socket is the end of the communication channel. These sockets can communicate within processes, or between the processes on the same machine, or between processes on different machines. Sockets use different methods to determine the connection type of port-to-port communication between the client and the server. The protocols used in the sockets are DNS, IP addressing, E-mail, FTP, etc.

Vocabulary of the Sockets:

The vocabulary of the socket is given below -

TermsDescription of the terms
TypeThe communication type of the socket.
ProtocolSpecify the type and type of protocol used in the type. Usually, it is zero.
DomainThe set of protocols used for transportation methods such as AF_INET, PF_INET, etc.
PortThe server listens for client calls on one or more ports. The port number, service name, or Fixnum port can be a string.
HostnameCheck the network interface. This can be a string containing a hostname, an IPv6 address, or a string containing both addresses. It can be an integer or a string which have zero length or a string ""

Programming of the Sockets:

The Socket programming is a method of connecting two nodes in a network to communicate with each other. While one socket listens on a specific IP port, another socket establishes a connection with another socket. When a client connects to a server, then the server creates a listening socket. These are the real backbone behind web browsing. Simply put, there is a server and a client. For socket programming, we can use the socket module. So, we need to add the socket module by using the following command -

We need to use the Socket.socket() method to create a socket.

Syntax:

The syntax for the socket programming is given below -

Here we give a program code of the socket programming in Python. The code is given below -

Here,

  • socket_family is either AF_UNIX or AF_INET.
  • socket_type is either SOCK_DGRAM or SOCK_STREAM.
  • protocol is usually left out, and its default value is 0.

Program Code:

Here we give a program code of the socket programming in Python. The code is given below -

Output:

Now we run the above code and find the socket name from it. The output is given below -

The socket is: <socket.socket fd=624, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0>

The socket module provides many opportunities for client-side and server-side programming. Let us learn each method in detail.

1. Socket Server method:

This method is used on the server side. Let us understand all the functions of this method below -

Function NameDescription
soc.bind()It connects the address to the socket. This address consists of a hostname and port number pair.
soc.accept()TCP accepts client connections passively and blocks them until a connection arrives.
soc.listen()It starts as a TCP listener.

2. Socket Client method:

This method is used on the client side. Let us understand all the functions of this method below -

Function NameDescription
soc.connect()It is used to initiate a TCP server connection.

3. Socket General method:

This is the general method of the socket module in Python. Let us understand all the functions of this method below -

Function NameDescription
soc.send()It is used to send the TCP message.
soc.sendto()It is used to send the UDP message.
soc.recv()It is used to receive the TCP message.
soc.recvfrom()It is used to receive the UDP message.
soc.ghostname()It is used to returns the host name.
soc.close()It is used to close all the sockets.

Server Client program in Python:

Here we learn the server client program in Python. Firstly, we learn the server part -

Server:

The server has a bind() method that binds to an IP and port and allows it to listen for requests from that IP and port. The server has a listen() method that puts the server into the listening mode, allowing it to listen for incoming connections. The server's accept() and close() methods terminate. The Accept method initiates the connection with the client. The Close method is used to close all the connections with the client.

Program Code:

Here, we give the program code of the network programming on the server side using Python. The code is given below -

Output:

Now we run the above code and find the result from it. The result is given below -

The Socket is created successfully
The socket is binded to 50675
The socket is listening

Explanation:

We create a socket object and store a port to our computer. Passing an empty string indicates that the server is able to listen for connections from other computers. If we would have passed 127.0.0.1 then it will only listen on the local computer. After that, we put the server in listening mode. Here, 10 means that if the server is busy, 10 connections will be held; if socket 11 tries to connect, then the connection will be rejected. Finally, we create a while loop that starts accepting all incoming connections and closes it after sending a thank you message to each connection socket.

Client:

Now we need something the server can interact with. We can let this server know that our server is working. Enter the following command in the terminal:

Program Code:

Here, we give the program code of the network programming on the client side using Python. The code is given below -

Output:

Now we run the above code and find the result from it. The result is given below -

The Socket is created successfully
The socket is binded to 50675
The socket is listening
Got a connection from ('127.0.0.4', 50675)
b'Thank you for connecting with the socket'

Explanation:

We connect to localhost from port 50675 (the port on which the server is running), get the data from the server, and then close all the connections. Now, we save this file as client.py and after that, run it from the terminal after starting the server script.

Conclusion:

In this tutorial, we learn about Network Programming in Python and how to write a server-client program using Python.