Get the App
SLTechnology News&Howtos  ›  Network Security  › 

Network programming socket module

Shulou Source: shulou.com Published: 2022-06-01 03:48:20 10月04日 Update

The address family of sockets (AF, Address Family) is divided into two types, one is file-based AF_UNIX, the other is network-based AF_INET,; sockets, there are two types of sockets, connection-oriented socket SOCK_STREAM and connectionless socket SOCK_DGRAM.

There are 3 modules commonly used in UDP:

(1) socket ([family [, type [proto]])

(2) sendto (string,address)

(3) recvfrom (bufsize [, flags]) receives and sends UDP data and receives data,address.

There are 6 modules commonly used in TCP:

(1) when a connect (address) client connects to a TCP server, the address address must be a two-element tuple in the format (host,port)

(2) send (bytes [, flags])

(3) recv (bufsize [, flags]) receives and sends TCP data and receives data.

(4) bind (address)

(5) listen (backlog) backlog specifies the maximum number of clients allowed to connect to the server. Its value is at least 1. When connection requests are received, they need to be queued, and if the queue is full, the request is rejected.

(6) when accept () calls the accept method, socket enters the "waiting" state. When the client requests a connection, the method establishes the connection and returns to the server. The accept method returns a tuple of two elements (connection,address). The first element, connection, is the new socket object through which the server must communicate with the customer; the second element, address, is the customer's Internet address.

The flow of the TCP connection is shown in the following figure.

It is important to note that when socket transmits information, it can only be sent in byte format, while python3 defaults to unicode format. Therefore, python3 needs to encode str.encode () when sending str and decoding str.decode () when receiving str.

Tags: Client element socket server service address method format module common data network two information client family object file process status Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Huawei MySQL macOS MariaDB