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 Number | Service or Application |
443 | HTTPS, to transfer web pages safely |
80 | HTTP, to transfer web pages |
119 | NNTP, to transfer articles |
110 | POP3 – a mailing service |
109 | POP2 – a mailing service |
67 | BOOTP – offers configuration at boot time |
25 | SMTP – delivers mails |
23 | Telnet – provides remote login |
21 | FTP – to transfer files |
13 | Date and Time services |
The diagram below shows a server that’s connected to clients via 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