Sockets common function functions:
Socket.socket(family=af_inet, type=sock_stream, proto=0, fileno=none) #创建socket对象
Socket families (address cluster):
Af_unix communication between native UNIX ——————————
Af_inet —————————— using IPV4
Af_inet6 —————————— using IPV6
Note: The above values can be used in the first of Socket.socket ().
Socket types:
SOCK_STREAM # TCP Socket type
SOCK_DGRAM # UDP Socket type
Sock_raw #原始套接字类型, this socket is powerful, creating such a socket to listen to all the data frames on the NIC
SOCK_RDM #是一种可靠的UDP形式, which guarantees the delivery of the datagram but does not guarantee the order. Sock_ram is used to provide low-level access to the original protocol, which is used when certain special operations are required, such as sending ICMP packets. Sock_ram is typically used only by advanced users or by programs that are run by administrators.
Note: These constants are socket types and are applied to socket()
the second parameter in the function. Depending on the system, there may be more constants. ( only SOCK_STREAM
and SOCK_DGRAM
seems usually very useful.) )
Span id= "yiyi-193" class= "Yiyi-st" > The protocol number is usually zero, can be omitted, or in the address family is af_can
Case, the agreement should be can_raw
or can_bcm
. with SOCKET.FROMFD ()
, Fileno returns the same socket, not duplicates. This may help to use Socket.close ()
closes the detached socket, normally the latter two parameters are ignored .
Socket.bind (address) #将socket绑定到地址 (commonly used on the service side)
The format of address addresses depends on the address family, and in Af_inet, the address is represented as a tuple (host,port).
Socket.listen ([backlog]) #启用服务器以接受连接(commonly used on the server side).
Backlog >=0 that specifies the number of unacceptable connections that the system will allow before rejecting a new connection. if not specified, the default reasonable value is selected.
Socket.accept () # Receives a connection. The socket must be bound to an address and listen to the connection. The return value is a pair of (Conn,address (common to server)
Conn is a socket object that can send and receive data on that connection, and address is the one at the other end of the connection.
SOCKET.RECV (BufSize[, Flags]) #从socket接收数据, the return value is a Byte object that represents the data received.
The maximum amount of data received at once is specified by BufSize, and the parameter flags are usually ignored.
Socket.send (Data[, Flags]) #将数据发送到socket.
Only bytes types of data can be sent in Python3.
Socket.connect (address) #连接到远程socket (commonly used for clients)
< Span class= "yiyi-st" > If the connection is signaled, the method waits until the connection is complete, or if the signal handler does not throw an exception and the socket is blocking or is blocked, the socket.timeout
timed out. interruptederror exception.
Socket.close () #关闭socket
Note: After being called, the connection disconnects, the socket cannot send the data, and the other end of the connection will not receive the data.
Python---Socket module detailed