Networking in Python

Networking in Python – Part 1

When a connection is formed among computers, it is called a network. You can create such connections even using a cable. The internet we use daily is a network of millions of computers. One of the main advantages of using a network is sharing files or data. For instance, an automobile organization in a country can have a network through which all its offices can connect and share information.

Similarly, if you work in an office with many team members, you must have only one printer to which all the computers are connected. Such networking eliminates the need to have a standalone printer for each computer. In any network, there are computers that either receive or provide data. The computers that receive data are called ‘clients’, and those that provide data are called ‘servers’. Sometimes, a computer can act as a client and a server.

You need the three requirements listed below to set up a network.

  • Hardware: contains hubs, routers, modems, cables, computers etc.
  • Software: The programs used for establishing the communication between clients and servers.
  • Protocol: Refers to the way followed in setting up the connection and favours in receiving and sending data in a particular format.

Sockets in Python Networking

We can set up a logical connecting point, called a socket, between a client and a server so that their communication gets done through that point. Every socket has an identification number known as a ‘port number’, which takes 2 bytes and can be from 0 to 65535. We establish communication via sockets through ‘socket programming’.

We should use a new port number for each socket based on the service we require. The table below contains specific reserved port numbers, and our computers use port numbers from 0 to 1023 for different applications.

Port NumberService or Application
443HTTPS, to transfer web pages safely
80HTTP, to transfer web pages
119NNTP, to transfer articles
110POP3 – a mailing service
109POP2 – a mailing service
67BOOTP – offers configuration at boot time
25SMTP – delivers mails
23Telnet – provides remote login
21FTP – to transfer files
13Date and Time services

The diagram below shows a server that’s connected to clients via sockets.

Server connected with Clents through sockets

Python offers the module ‘socket’ to create sockets. Using the socket() function as mentioned below, we can create sockets in a Python program:

s = socket.socket(address_family, type)

The ‘address_family’ in the format mentioned above refers to the version of the IP address used in the program, either IPv4(socket.AF_INET) or IPv6(socket.AF_INET6).

The second argument, ‘type’, refers to the protocol used in the program, either TCP/IP(socket.SOCK_STREAM) or UDP(socket.SOCK_DGRAM).

IP Address check through Python program using socket module

We can use the gethostbyname() function in the socket module to know the IP address of a website in any Python program. It takes the name of the website and returns the IP address. Let us look at an example program to find the IP address of a website.

import socket

host_name = "www.bing.com"

try:
    #check the IP Address of the website
    address_1 = socket.gethostbyname(host_name)
    print("IP Address: ",address_1)

except socket.gaierror:
    print("The website does not exist online")
Output:

IP Address:  204.79.197.200
Scroll to Top