python series: detailed explanation of socket library

Detailed explanation of python series-socket library application

1. Functions of the socket library

Function name parameter effect
gethostname() no parameters Return hostname
gethostbyname('hostname') CPU name Convert hostname to ipv4 address
gethostbyname_ex('hostname') CPU name Return host name, host alias list, host IP address list
gethostbyaddr('ip address') IP address Returns a triplet (hostname, list of host aliases, list of host IP addresses)
getservbyport(port number','protocol name') Port number, protocol name Returns the service name of this port number, limited to this machine
getservbyname('service name','protocol name') Service name, protocol name Returns the port number where the service is located
getsockname() no parameters Returns its own bundled IPv4 address
getaddrinfo('hostname','port number') host name, port number Returns a five-tuple, used to obtain the address information of the host name, compatible with ipv4 and ipv6
socket(family,type) family:socket.AF_INET(IPv4)
         socket.AF_INET6(IPv6)
             AF_UNIX (inter-process communication on the same machine)
type:socket.SOCK_STREAM (TCP connection)
     socket.SOCK_DGRAM (UDP protocol)
Create a connection with a known IP address and protocol type
connect((host,port)) (host name, port number) Connect remotely to the host's port, pay attention to the brackets
gettimeout() no parameters Returns the socket timeout time, unit is s
settimeout(timeout) overtime time Set socket timeout

2. Methods of socket library

1.connect(address):

      Connect to a remote socket corresponding to address (IP address). If the connection is interrupted, this method will wait until the connection is completed, or throw a socket.timeout error

2.accept():

      Accept a connection, but the premise is that the socket must have been bound to an address and is waiting for a connection. By default, sockets are blocking, which means that socket method calls will not return until the task is completed.
      When the accept method is called, the socket will enter 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 with two elements (connection, address). The first element, connection, is the new socket object through which the server must communicate with the client; the second element, address, is the client's Internet address.

3.recv(bufsize):

      Receive data from the socket, note that it is of byte type, and bufsize specifies the maximum data size received at one time.

4.recvfrom(bufsize) :

      The difference from the previous recv(bufsize) method is that in addition to the data, the return value also has the address to send the data. The return value is a data, address pair (data, address).

5.send(bytes):

      To send data to the socket, the premise is that you have connected to the remote socket. The return value is the amount of data sent. It is the application's responsibility to check whether the data has been sent.

6.sendto(bytes, flags, address):

      Basically the same as socket.send().

7.close():

      Close the connection. When socket.close() is executed, the underlying operations related to this connection will also be closed (such as the file descriptor). Once closed, operations on related file objects will fail.

8.bind(address):

      Bind the socket object to an address, but this address must not be occupied, otherwise the connection will fail.
The address here is generally an ip, port pair.

9.listen([backlog]) :

      Monitoring enables the server to receive server connections. If the backlog is specified (at least 0, if it is smaller than 0, the system defaults to 0), it limits the number of connections that can be made. If not specified, a default reasonable value will be assigned.

Guess you like

Origin blog.csdn.net/hjz2196987870/article/details/127907363