Python Networking

How Is Python Used in Networking?

Python is a high-level language specifically designed to make the developers’ life easy. On the other hand, network programming may be a tedious and complicated task in other programming languages, but it is simple and easy in a Python programming language. It allows the user to write scripts that can automate the complex network configuration. In this article, we will see how to perform the network programming using Python.

Python programming language plays a vital role in network programming. Python has a built-in standard library that provides complete support for data encoding and decoding, network protocols, and all other networking concepts. Writing the codes for network programming in Python is way much easier than in C++ or Java. Python programming language provides two levels of network service access: low-level and high-level access. The low-level access offers the basic socket support of the operating system. While the high-level access allows the implementing protocols like FTP. HTTP, etc.

This article will help you with a basic understanding of the most famous concept of networking in Python which is socket programming.

What Are Sockets?

Sockets provide a bidirectional communication channel between two endpoints. Processes on other machines, processes on the same machine, and socket manages to communicate with all. They can be implemented on different channels such as UDP, TCP, Unix domain sockets, etc. The socket library in Python provides different classes for managing the common data transfer or a generic interface for managing the rest. Sockets use various types of protocols to determine the port-to-port connection between servers and clients. These protocols are used for FTP, SMTP, Email, IP address, DNS, etc. The vocabulary of sockets is different. Have a look at the following illustration:

Term
Description

Domain
Set of protocols used as a transport mechanism.

Type
Defines the type of communication between sockets.

Protocol
Determines the type of protocol within the type and domain.

Port
A listening point through which the servers listen to the client calls. It consists of the name of a service, a string containing the port number, and a Fixnum port number.

Hostname
Identifies a network interface and is consists of the following things:

A string “<broadcast>” which identifies the broadcast address.

A zero-length string.

A string that contains the hostname, a double-quad address, or an IPv6 address.

An integer which defines the binary address in host byte order.

What Is Socket Programming?

Socket programming connects two nodes on a network of communication. One node is the listener which is commonly known as Server, and the other one reaches out to the server which is commonly known as the Client. For socket programming in Python, the socket module needs to be included.

Let’s explore some examples to learn how to implement the network programming in Python.

Example 1:

In this example, we will learn the syntax of the socket module and how to import it into a Python program. First, we import the socket module in the Python program using the “import socket” statement. Then, we can use the functions of the socket modules. Here is the syntax of the socket() module:

import

socket

socketmodule

=

socket

.

socket

(

socket

.

AF_INET

,

socket

.

SOCK_STREAM

)

print

(

socketmodule

)

Now let’s execute the statement to see what it returns:

Here is the following output:

Example 2:

Now, let us learn how to create a simple server and a simple client and see how they communicate with each other.

Here is the server-side coding:

First, import a socket and then create a socket object:

import

socket

as

s

socketmodule

=

socket

.

socket

(

)

Get the name of the machine by calling the gethostname() statement:

hostname

=

socket

.

gethostname

(

)

Provide the port number that you want the server to make a connection between server and client:

portnum

=

12345

Now, bind the host with the port to start a connection:

s.

bind

(

(

hostname

,

portnum

)

)

The next step is to wait for the client connection:

socketmodule.

listen

(

5

)

Finally, establish a connection with the client:

while

True

:

  a

,

adrs

=

s.

accept

(

)

 

print

(

‘Connection received from’

,

adrs

)

  a.

send

(

b

‘Thanks for connecting’

)

  a.

close

(

)

The following is the client-side coding:

To start a connection, the socket module needs to be imported on the client side just as it was imported on the server side:

import

socket

as

s

socketmodule

=

socket

.

socket

(

)

Follow the same process, get the name of the local machine, and reserve the port which was reserved by the server to establish a connection:

hostname

=

socket

.

gethostname

(

)

portnum

=

12345

Now, connect the host with the port and make a connection between them:

socketmodule.

connect

(

(

‘127.0.0.1’

,

portnum

)

)

print

(

socketmodule.

recv

(

1024

)

)

s.

close

(

)

Once you are done with both client and server-side coding, start both server and client in the background to see the results:

After a successful connection, you will see the following output:

Example 3:

Let us see another example to have a clearer understanding on how the server connects with the client as well as how the server listens to the client.

Here is the server-side coding:

For the server-side coding, follow the same process. Import the socket, create a socket module, reserve the port number, bind the host and port, and listen to the client. If you might observed, the “if” condition is added to the loop to check a particular condition. See the following code:

import

socket

portnum

=

60

IPadrs

=

‘127.0.0.1’

bufferS

=

30

socketmodule

=

socket

.

socket

(

socket

.

AF_INET

,

socket

.

SOCK_STREAM

)

socketmodule.

bind

(

(

IPadrs

,

portnum

)

)

socketmodule.

listen

(

1

)

conn

,

adrs

=

socketmodule.

accept

(

)

print

(

‘The Address of the connection is: ‘

,

adrs

)

while

True

:
   data

=

conn.

recv

(

bufferS

)

if

not

data:
   

break

print

(

“The received data is: “

,

data

)

con.

send

(

data

)

con.

close

(

)

The following is the client-side coding:

The process of making a connection on the client side is again, the same. Import the socket, create a socket module, reserve the port number, and connect the port with the host:

import

socket

portnum

=

5006

IPadrs

=

‘127.0.0.1’

bufferS

=

1024

PrintMessage

=

“Hi! I’m a message from client.”

socketmodule = socket.socket (socket.AF_INET, socket.SOCK_STREAM)

socketmodule.connect((IPadrs, portnum))

socketmodule.send(PrintMessage)

data = socketmodule.recv(bufferS)

socketmodule.close

Now, observe that the client sends a message to the server – “Hi! I’m a message from client”. This message is received by the server, and it gets printed.

After the successful connection between the server and the client, you will get to see the following output:

Conclusion

This is a basic overview of socket programming in Python and a quick start with the implementation. However, it is a vast subject and contains a lot of details with complex coding. Once you get hands-on with these simple programs, you will be able to get into the depth of socket programming.