Common Socket syntax and socketserver instance, socketsocketserver

Source: Internet
Author: User

Common Socket syntax and socketserver instance, socketsocketserver

1 Socket related:

1> Socket Families (address cluster ):

Socket. AF_UNIX local Process Communication

Socket. AF_INET IPV4

Socket. AF_INET6 IPV6

2> Socket Types:

Socket. SOCK_STREAM # for tcp

Socket. SOCK_DGRAM # for udp

Socket. SOCK_RAW # The original socket. A common socket cannot process network packets such as ICMP and IGMP, while SOCK_RAW can. Secondly, SOCK_RAW can also process special IPv4 packets. In addition, the original socket can be used, you can use the IP_HDRINCL socket option to construct an IP address header;

Socket. SOCK_RDM # is a reliable form of UDP, that is, to ensure the delivery of data packets but not to ensure the order; SOCK_RAM is used to provide low-level access to the original protocol, when you need to perform some special operations, for example, if an ICMP packet is sent, SOCK_RAM is generally only used by programs run by senior users or administrators;

Socket. SOCK_SEQPACKET # discarded

3> Socket method:

Socket. socket (family = AF_INET, type = SOCK_STREAM, proto = 0, fileno = None)

Socket. socketpair ([family [, type [, proto])

Socket. create_connection (address [, timeout [, source_address])

Socket. getaddrinfo (host, port, family = 0, type = 0, proto = 0, flags = 0) # obtain the address of the Peer host to be connected;

Sk. bind (address)

S. bind (address): bind the socket to the address. The address format depends on the address cluster. In AF_INET, the address is expressed in the form of (host, port;

Sk. listen (backlog): starts listening for incoming connections. backlog specifies the maximum number of connections that can be suspended before the connection is denied. backlog equals to 5, indicating that the kernel has received the connection request, however, the server has not yet called accept to process the number of connections. This value cannot be infinitely large because the connection queue needs to be maintained in the kernel;

Sk. setblocking (bool): whether to block (True by default). If False is set, an error is returned if no data exists in accept and recv;

Sk. accept (): accept the connection and return (conn, address). conn is a new socket object, which can be used to receive and send data. address is used to receive the client address; receiving TCP client connection (blocking) waiting for the arrival of the connection;

Sk. connect (address): connect to the socket at the address. Generally, the address format is tuples (hostname, port). If a connection error occurs, the socket. error is returned;

Sk. connect_ex (address): Same as above, but there will be a return value. If the connection is successful, 0 will be returned. If the connection fails, the encoding will be returned, for example, 10061;

Sk. close (): closes the socket;

Sk. recv (bufsize [, flag]): receives data from the socket. The data is returned as a string. bufsize specifies the maximum number of received data. flag provides relevant information, which can be ignored;

Sk. recvfrom (bufsize [, flag]): similar to recv (), but the return value is (data, address). data is a string containing received data, and address is a socket address;

Sk. send (string [, flag]): sends data in string to the connected socket. The returned value is the number of bytes to be sent, which may be less than the number of bytes in string, that is, all specified content may not be sent;

Sk. sendall (string [, flag]): sends data in the string to the connected socket. However, before returning the message, all data is sent. If successful, None is returned. Otherwise, None is returned; internally, send all content by calling send recursively;

Sk. sendto (string [, flag], address): sends data to the socket. The address is a tuple in the form of (ipaddr, port). It specifies the route address. This function is mainly used for UDP protocol;

Sk. settimeout (timeout): sets the superperiod of socket operations. timeout is a floating point number in seconds. If the value is None, it indicates that there is no superperiod. It should be set when the socket is just created, because they may be used for connection operations (for example, a client connection can wait up to 5 s );

Sk. getpeername (): return the remote address of the connected socket. The returned value is usually a tuples (ipaddr, port );

Sk. getsockname (): return the address of the socket, usually a tuple (ipaddr, port );

Sk. fileno (): The file descriptor of the socket;

Socket. sendfile (file, offset = 0, count = None); send the file, but it is useless in most cases;

2 Socket Server:

1> common socket server:

Class socketserver. ForkingTCPServer

Class socketserver. ForkingUDPServer

Class socketserver. ThreadingTCPServer

Class socketserver. ThreadingUDPServer

2>

2. Socketserver instance:

Server:

            

Client:

          

Instance effect:

Client:

              

       

            

 

          

                  

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.