Python | Port numbers and sockets

Port numbers and sockets


port
The port is like the door of a house, the only way to get in and out of the house. If a program needs to send and receive network data, it needs to have such a port

In the Linux system, there can be as many as 65536 ports (2 to the 16th power)!

Since there are so many, the operating system has been numbered for unified management. This is the port number.

The port number
Ports are marked by port numbers, which are only integers, ranging from 0 to 65535. Port numbers are not used arbitrarily, but are allocated according to certain regulations. There are several classification standards for ports. We will not explain them in detail here, but only introduce well-known ports and dynamic ports.

Well-known port number
Well-known ports are well-known port numbers, ranging from 0 to 1023, to understand that the numbers used by some commonly used functions are estimated, such as phone numbers 110, 10086, 10010. In general, if a program needs to use a well-known port, it needs root privileges.

dynamic port number
The dynamic port range is from 1024 to 65535

The reason why it is called a dynamic port is that it generally does not allocate a certain service fixedly, but allocates it dynamically.

Dynamic allocation means that when a system program or application program needs network communication, it applies for a port to the host, and the host allocates one for its use from the available port numbers.

When the program is closed, the occupied port number is also released.

Port number role
We know that a host with an IP address can provide many services, such as HTTP (World Wide Web Service), FTP (File Transfer), SMTP (E-mail), etc. These services can be achieved through 1 IP address. So, how do hosts differentiate between different network services? Obviously can not rely on IP addresses, because the relationship between IP addresses and network services is a one-to-many relationship. In fact, different services are distinguished by "IP address + port number". It should be noted that the ports are not one-to-one correspondence. For example, when your computer accesses a WWW server as a client, the WWW server uses port "80" to communicate with your computer, but your computer may use a port like "3457".

Introduction to sockets


How processes on different computers communicate
The first problem to be solved is how to uniquely identify a process, otherwise communication will be impossible! On a computer, a process can be uniquely identified by its process number (PID), but this does not work on a network. In fact, the TCP/IP protocol family has helped us solve this problem. The "ip address" of the network layer can uniquely identify the host in the network, and the "protocol + port" of the transport layer can uniquely identify the application process (process) in the host. In this way, the ip address, protocol, and port can be used to identify the process of the network, and the process communication in the network can use this flag to interact with other processes.

Notice:

The so-called process refers to: the running program and the resources used at runtime are called the process as a whole (detailed when explaining multitasking programming)

The so-called inter-process communication refers to: data sharing between running programs

what is socket
Socket (referred to as socket) is a way of inter-process communication. One of the main differences between it and other inter-process communication is:

It can realize inter-process communication between different hosts. Most of the various services on our network are based on Socket to complete the communication.

For example, we browse the web, chat on QQ, send and receive emails and so on every day.

create socket
This can be done in Python using the socket module's function socket:

import socket
socket.socket(AddressFamily, Type)
illustrate:
The function socket.socket creates a socket with two parameters:

Address Family: You can choose AF_INET (for Internet inter-process communication) or AF_UNIX (for the same machine inter-process communication), AF_INET is commonly used in actual work
Type: socket type, which can be SOCK_STREAM (stream socket, mainly used for TCP protocol) or SOCK_DGRAM (datagram socket, mainly used for UDP protocol)
Create a tcp socket (tcp socket)

import socket

# create tcp socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# ...here is the function using sockets (omitted)...

# When not in use, close the socket
s.close()
Create a udp socket (udp socket)

import socket

# create a udp socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# ...here is the function using sockets (omitted)...
# When not in use, close the socket
s.close()
illustrate:
The socket usage process is very similar to the file usage process

create socket
Send/receive data using sockets
close the socket

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00