Python socket module

Source: Internet
Author: User

1. Socket module

Establish a network communication connection with at least a pair of port numbers (sockets). Socket is the nature of the programming Interface (API), TCP/IP encapsulation, TCP/IP also to provide the interface for programmers to do network development, this is the socket programming interface; HTTP is a sedan that provides a specific form of encapsulation or display data; the socket is the engine, Provides the ability to communicate on the network.

Socket function

In Python, we use the socket () function to create a socket with the following syntax:

Socket.socket (Family=af_inet, Type=sock_stream, proto=0)

    • Family: Protocol Family default: Af_inet (IPV4)
    • Type: The socket type can be divided into sock_stream or sock_dgram depending on whether it is connection-oriented or non-connected
    • Protocol: General does not fill the default is 0.protocol: General does not fill the default is 0.

Socket object (built-in) method

Function Describe
Server-side sockets
S.bind () The binding address (Host,port) to the socket, under Af_inet, represents the address in the form of a tuple (host,port).
S.listen () Start TCP snooping. The backlog specifies the maximum number of connections that the operating system can suspend before rejecting the connection. This value is at least 1, and most applications are set to 5.
S.accept () Passively accepts TCP client connections, (blocking) waits for a connection to arrive
Client sockets
S.connect () Actively initializing the TCP server connection. The format of the general address is a tuple (hostname,port) and returns a socket.error error if there is an error in the connection.
S.CONNECT_EX () Extended version of the Connect () function, which returns an error code instead of throwing an exception when an error occurs
Socket functions for public use
S.RECV () Receives TCP data, the data 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 the TCP data to the connected socket by sending the data in the string. The return value is the number of bytes to send, which may be less than the byte size of the string.
S.sendall () Complete sending of TCP data, complete sending of TCP data. Sends data from a string to a connected socket, but attempts to send all data before returning. Successful return none, Failure throws an exception.
S.recvfrom () The UDP data is received, similar to recv (), but the return value is (data,address). Where data is the string that contains the received information, address is the socket addressing that sent the data.
S.sendto () Sends UDP data, sends the data to the socket, address is a tuple in the form of (Ipaddr,port), specifies the remote address. The return value is the number of bytes sent.
S.close () Close socket
S.getpeername () Returns the remote address of the connection socket. The return value is typically a tuple (ipaddr,port).
S.getsockname () Returns the socket's own address. Typically a tuple (ipaddr,port)
S.setsockopt (Level,optname,value) Sets the value of the given socket option.
S.getsockopt (Level,optname[.buflen]) Returns the value of the socket option.
S.settimeout (Timeout) Sets the timeout period for the socket operation, and timeout is a floating-point number in seconds. A value of None indicates no over-time. In general, hyper-times should be set when a socket is just created, as they may be used for connected operations (such as Connect ())
S.gettimeout () Returns the value of the current timeout, in seconds, or none if the timeout period is not set.
S.fileno () Returns the file descriptor for the socket.
S.setblocking (flag) If flag is 0, the socket is set to nonblocking mode, otherwise the socket is set to blocking mode (the default value). In nonblocking mode, if the call recv () does not find any data, or the Send () call cannot send the data immediately, the Socket.error exception is raised.
S.makefile () Create a file associated with the socket

Python socket module

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.