Python--socket

Source: Internet
Author: User

Directory:

Abnormal

Address family

Socket type

Module method

Socket object and instance method

The socket module provides a low-level network connection interface in Python for operation of socket operations.

Abnormal

A total of 4 exceptions are defined for the socket module:

Socket.error

A socket-related error will throw the exception, the accompanying value is a string that interprets the error message, or a system call (errno, String) is returned in the form of an exception. Now Socket.error is a subclass of IOError.

Socket.herror

Address-related errors, C API calls, and so on may throw the exception, such as using H_errno 's C API, such as gethostbyname_ex () and gethostbyaddr () . The accompanying value is a (H_errno, String) two tuple representing an error returned by a library function call, string describing h_errno , hstrerror () by C function to return.

Socket.gaierror

Address-related errors will throw the exception, such as the getaddrinfo () and getnameinfo () functions. The accompanying value is an (error, String) Two tuple that represents an error returned by the library function call, where string describes the error, which is returned by the gai_strerror () function of C.

socket.timeout

The exception was thrown when the socket timed out. The timeout for a socket is set by the instance method settimeout () function, and the accompanying value of the exception is always the string "timed out".

Addresses family (address Family)

The address family is usually used for the first parameter of the socket () function, of course, Af_unix is only used for Unix-like platforms, and if the former is not defined, then the address family of the protocol is not available.

Socket.af_unix

The address that belongs to that type is a string. The Af_unix corresponds to a value of: 1.

socket.af_inet

(host, port) in the form of a two-tuple. The af_inet corresponds to a value of: 2.

Socket.af_inet6

(host, Port, Flowinfo, ScopeID) in the form of a four-tuple. The AF_INET6 corresponds to a value of: 23.

Socket types (type)

The socket type is used for the second parameter of the socket () function, but only sock_stream and sock_dgram are more common.

socket. Sock_stream

For a stream (TCP connection) socket, the actual value is: 1.

socket. Sock_dgram

Socket for datagram (UDP connection), the actual value is: 2.

socket. Sock_raw

The actual value is: 3.

socket. SOCK_RDM

The actual value is: 4.

socket. Sock_seqpacket

The actual value is: 5.

Other constants

Socket.has_ipv6

A Boolean constant that shows whether the current platform supports the IPV6 protocol.

Module method

socket.create_connection (addr[, timeout[, source_addr])

Socket.getaddrinfo (Host, port[, family[, socktype[, proto[, flags]])

At least two parameters are required to convert a host/port information into a five-tuple that contains all the socket information needed to connect to the target host/port. The returned five-tuple has the following form: (family, Socktype, Proto, Canonname, sockaddr) .

  

Convert the host/port parameter to a 5-tuple containing all the parameters used to create the socket. host represents the domain name, which can be either a IPV4/V6 address or None. Port can be a service string name such as ' http ', or it can be a numeric port number or None. Passing None as the value of host and Port is equivalent to passing NULL to the underlying C API.

Parameter family, socktype and Proto are optional and default is 0, which means that all results are selected. The parameter flags can be one or more of the 'ai_* ' constants, which can affect how the result is computed and returned, and the default value is also 0. For example, ai_numerichost will turn off the domain name query, and if host is a domain name, an exception will be thrown here.

The function returns a 5-tuple, with the structure of the 5 tuples as follows:

(family, Socktype, Proto, Canonname, sockaddr)

Where family,socktype,proto are all integers and are used to pass to the socket () function.

canonname  will be a string representing the canonical name of the  host  if ai_canonname  is part of THE  flags  argument; else  canonname  will be empty.

sockaddr   is a tuple that describes a socket address in a format that relies on   in the returned results; family   (for example   family   is  af_inet     sockaddr  is a  port)   Two-tuple; when   family   is   < TT class= "xref py py-const docutils literal" >af_inet6  when   sockaddr  is a   (Address, port, < Span class= "Pre" >flow info, scope id)   Four tuples, the results of this section can be passed to   socket.connect ()   method.

The following example shows the connection information for a 80 port that gets an HTTP connection to www.python.org:

>>> socket.getaddrinfo ("www.python.org",0, 0, socket. IPPROTO_TCP) [(2, 1, 6, ', (' 82.94.164.162 ', +)), (1, 6, ", (' 2001:888:2000:d::a2 ', +, 0, 0))] 

Socket.getfqdn ([name])

Socket.gethostbyname (hostname)

Translates the hostname into a IPv4 address.

SOCKET.GETHOSTBYNAME_EX (hostname)

Socket.gethostname ()

socket.gethostbyaddr (ip_address)

socket.getnameinfo (SOCKADDR, flags)

socket.getprotobyname (ProtocolName)

socket.getservbyname (servicename[, ProtocolName])

Socket.getservbyport (port[, ProtocolName])

Socket.socket ([family[, type[, Proto]])

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

SOCKET.FROMFD (FD, Family, type[, Proto])

Socket.ntohl (x)

Socket.ntohs (x)

socket.htonl (x)

socket.htons (x)

Socket.inet_aton (ip_string)

Socket.inet_ntoa (PACKED_IP)

Socket.inet_pton (address_family, ip_string)

socket.inet_ntop (address_family, PACKED_IP)

socket.getdefaulttimeout ()

socket.setdefaulttimeout (Timeout)

  

Socket object and Object methods

socket.accept ()

Function: A socket that accepts a connection that is capable of invoking the method must (1). has been bound to a specific address, and (2). Listen for connections.

return value: (conn, addr) in the form of a two-tuple, where:

Conn : A new socket object connected to this socket that can be used to send and receive messages.

addr : The network address of the socket object connected to this socket.

  

Socket.bind (address)

Function: Binds a socket to an address, provided that the socket has not been bound to an address;

Parameters: addresses that correspond to the address family format declared when the socket was created;

Return value: An attempt to bind an already bound socket will throw socket.error . The return value is null on normal invocation.

socket.close ()

Function: Closes the socket, and when the socket is closed, all operations on the closed socket will fail. When the socket is GC, it is automatically closed. Closing a socket frees the resources used by the connection, but does not necessarily close the connection immediately, and if you want to close the connection in a timely manner, you should call shutdown () before close () .

Parameters: no parameters required;

Return value: The return value is null.

Socket.connect (address)

Function: Connect to a remote socket.

Parameters: addresses that correspond to the address family format declared when the socket was created;

Return value: The return value is null.

socket.connect_ex (address)

Socket.fileno ()

Function: Return the socket FD, the socket FD can be used for select mechanism, etc.

Parameters: No parameters are required when calling;

Return value: This value cannot be used for a function similar to os.fdopen () that is directly based on the FD operation under the fd,windows of the current socket, and there is no such restriction on UNIX-like systems.

Socket.getpeername ()

Role: Gets the address of the remote socket that the socket is connected to.

Parameters: No parameters are required when calling;

Return value: The address of the remote connection socket, depending on the address family of the socket.

Socket.getsockname ()

Function: Returns the address of a socket.

Parameters: No parameters are required when calling;

Return value: The address of the current socket, depending on the address family of the socket.

socket.getsockopt (level, optname[, Buflen])

Function: Returns some parameters of a given socket;

Parameters:

Level

Optname:

Buflen:

return value:

socket.ioctl (Control, option)

Socket.listen (backlog)

Role: Listens for connections to the socket.

Parameters: The backlog specifies the maximum number of connections that the socket can hold, at least 0;

Return value: The return value is null.

Socket.makefile ([mode[, BufSize]])

SOCKET.RECV (bufsize[, flags])

Role: receiving data from sockets

Parameters:

BufSize--The maximum received data length, usually should be set to 2 index times;

Flags-The default is 0, and the parameter Flags in UNIX recv (2) have the same meaning,

Return value: The string data received.

Socket.recvfrom (bufsize[, flags])

Role:

Parameters:

Return value: (String, address) in the form of a two-tuple, string is the received data, address is sent to send this data socket addresses, the specific address form depends on the address family to which the socket belongs.

Socket.recvfrom_into (buffer[, nbytes[, flags])

Socket.recv_into (buffer[, nbytes[, flags])

socket.send (string[, flags])

Socket.sendall (string[, flags])

Socket.sendto (string, address)

socket.sendto (string, flags, address)

socket.setblocking (flag)

Function: Sets the blocking state of a socket.

Parameters: Flags--sets the socket to non-blocking for 0 o'clock and sets the socket to block for 1 o'clock. By default, the initially created sockets are blocked. In a nonblocking state, if the recv () function does not get any data or the Send () function does not emit data immediately, the Socket.error exception is thrown, and in blocking mode, the calls are blocked until they can continue. s.setblocking (0) is equivalent to s.settimeout (0.0) , while s.setblocking (1) is equivalent to s.settimeout (None) .

Return value: The return value is meaningless.

socket.settimeout (value)

Role:

Parameters:

return value:

socket.gettimeout ()

socket.setsockopt (level, optname, value)

Socket.shutdown (How)

Function: Closing one or both ends of a connection;

Parameters:

How--the way to close the connection-- shut_rd can no longer read (receive), SHUT_WR thereafter cannot write (send), shut_rdwr Thereafter cannot read and write. Depending on the platform, closing one end of the connection may cause the other end to close as well.

return value:

Read-only properties of the socket object

socket.family

Corresponds to the address family that was passed in when the socket was created, but the value is displayed.

Socket.type

Corresponds to the type of socket that was passed in when the socket was created, but a numeric value is displayed.

Socket.proto

  

Python--socket

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.