In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of how to use the socket () function in Python, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will have something to gain after reading this Python article on how to use the socket () function. Let's take a look.
What is Socket?
Socket, also known as "sockets", applications usually send requests to or reply to network requests through "sockets", so that hosts or processes on a computer can communicate.
Socket () function
In Python, we use the socket () function to create a socket in the following syntax format:
Socket.socket ([family [, type [, proto]) parameters
Family: the socket family can be AF_UNIX or AF_INET
Type: socket types can be divided into SOCK_STREAM or SOCK_DGRAM according to whether they are connection-oriented or connectionless
Protocol: generally left empty, the default is 0.
The Socket object (built-in) method function describes the server-side socket s.bind () binding address (host,port) to the socket, which is represented as a host,port under AF_INET. S.listen () starts TCP snooping. Backlog specifies the maximum number of connections that the operating system can suspend before denying connections. The value is at least 1, and most applications can set it to 5. S.accept () passively accepts the TCP client connection and (blocking) waits for the connection to arrive. The client socket s.connect () initializes the TCP server connection actively. Generally speaking, the format of address is tuple (hostname,port). If there is an error in the connection, an socket.error error is returned. An extended version of the s.connect_ex () connect () function, which returns the error code when an error occurs, rather than the socket function s.recv () that throws an exception for common purpose, receives TCP data, which is returned as a string, and bufsize specifies the maximum amount of data to receive. Flag provides additional information about the message, which can usually be ignored. S.send () sends TCP data, sending data from string to the connected socket. The return value is the number of bytes to be sent, which may be less than the byte size of string. S.sendall () sends the TCP data completely, and the TCP data completely. Sends data from string to the connected socket, but attempts to send all data before returning. None is returned for success, and an exception is thrown for failure. S.recvfrom () receives UDP data, similar to recv (), but the return value is (data,address). Where data is the string containing the received data and address is the socket address where the data is sent. S.sendto () sends UDP data to the socket, where address is a tuple in the form (ipaddr,port) that specifies the remote address. The return value is the number of bytes sent. S.close () closes the socket s.getpeername () and returns the remote address of the connection socket. The return value is usually an ipaddr,port. S.getsockname () returns the socket's own address. It is usually an ipaddr,port s.setsockopt (level,optname,value) that sets the value of a given socket option. S.getsockopt (level,optname [.buflen]) returns the value of the socket option. S.settimeout (timeout) sets the timeout period for socket operations. Timeout is a floating-point number in seconds. A value of None indicates that there is no timeout. In general, the timeout should be set when the socket is first created, because the operation they may be used to connect (such as connect ()) s.gettimeout () returns the value of the current timeout in seconds, or None if the timeout is not set. S.fileno () returns the file descriptor of the socket. S.setblocking (flag) if flag is False, set the socket to non-blocking mode, otherwise set the socket to blocking mode (the default). In non-blocking mode, if the call to recv () does not find any data, or if the call to send () cannot send the data immediately, it will cause a socket.error exception. S.makefile () creates a file associated with the socket. That's all for the article on "how to use the socket () function in Python". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use the socket () function in Python". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.