gevent._socket2 – Python 2 socket module

Caution

Some of the docstrings are automatically generated and may be correct for Python 3 but not Python 2. Please see the standard library documentation.

Python 2 socket module.

error

alias of OSError

exception gaierror

Bases: OSError

add_note()

Exception.add_note(note) – add a note to the exception

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

errno

POSIX exception code

filename

exception filename

filename2

second exception filename

strerror

exception strerror

exception herror

Bases: OSError

add_note()

Exception.add_note(note) – add a note to the exception

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

errno

POSIX exception code

filename

exception filename

filename2

second exception filename

strerror

exception strerror

timeout

alias of TimeoutError

SocketType

alias of socket

class socket(family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, _sock=None)[source]

Bases: SocketMixin

gevent socket.socket for Python 2.

This object should have the same API as the standard library socket linked to above. Not all methods are specifically documented here; when they are they may point out a difference to be aware of or may document a method the standard library does not.

Changed in version 1.5.0: This object is a context manager, returning itself, like in Python 3.

bind(address)

Bind the socket to a local address. For IP sockets, the address is a pair (host, port); the host must refer to the local host. For raw packet sockets the address is a tuple (ifname, proto [,pkttype [,hatype [,addr]]])

connect(address)

Connect to address.

Changed in version 20.6.0: If the host part of the address includes an IPv6 scope ID, it will be used instead of ignored, if the platform supplies socket.inet_pton().

connect_ex(address) errno

This is like connect(address), but returns an error code (the errno value) instead of raising an exception when an error occurs.

dup() socket object[source]

Return a new socket object connected to the same system resource. Note, that the new socket does not inherit the timeout.

fileno() integer

Return the integer file descriptor of the socket.

getblocking()

Returns whether the socket will approximate blocking behaviour.

New in version 1.3a2: Added in Python 3.7.

getpeername() address info

Return the address of the remote endpoint. For IP sockets, the address info is a pair (hostaddr, port).

getsockname() address info

Return the address of the local endpoint. The format depends on the address family. For IPv4 sockets, the address info is a pair (hostaddr, port).

getsockopt(level, option[, buffersize]) value

Get a socket option. See the Unix manual for level and option. If a nonzero buffersize argument is given, the return value is a string of that length; otherwise it is an integer.

gettimeout() timeout

Returns the timeout in seconds (float) associated with socket operations. A timeout of None indicates that timeouts on socket operations are disabled.

listen([backlog])

Enable a server to accept connections. If backlog is specified, it must be at least 0 (if it is lower, it is set to 0); it specifies the number of unaccepted connections that the system will allow before refusing new connections. If not specified, a default reasonable value is chosen.

recv(buffersize[, flags]) data

Receive up to buffersize bytes from the socket. For the optional flags argument, see the Unix manual. When no data is available, block until at least one byte is available or until the remote end is closed. When the remote end is closed and all data is read, return the empty string.

recv_into(buffer[, nbytes[, flags]]) nbytes_read

A version of recv() that stores its data into a buffer rather than creating a new string. Receive up to buffersize bytes from the socket. If buffersize is not specified (or 0), receive up to the size available in the given buffer.

See recv() for documentation about the flags.

recvfrom(buffersize[, flags]) -> (data, address info)

Like recv(buffersize, flags) but also return the sender’s address info.

recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)

Like recv_into(buffer[, nbytes[, flags]]) but also return the sender’s address info.

send(data[, flags]) count

Send a data string to the socket. For the optional flags argument, see the Unix manual. Return the number of bytes sent; this may be less than len(data) if the network is busy.

sendall(data[, flags])[source]

Send a data string to the socket. For the optional flags argument, see the Unix manual. This calls send() repeatedly until all data is sent. If an error occurs, it’s impossible to tell how much data has been sent.

sendto(data, [flags, ]address) count

Like send(data, flags) but allows specifying the destination address. For IP sockets, the address is a pair (hostaddr, port).

setblocking(flag)

Set the socket to blocking (flag is true) or non-blocking (false). setblocking(True) is equivalent to settimeout(None); setblocking(False) is equivalent to settimeout(0.0).

setsockopt(level, option, value: int)
setsockopt(level, option, value: buffer) None
setsockopt(level, option, None, optlen: int) None

Set a socket option. See the Unix manual for level and option. The value argument can either be an integer, a string buffer, or None, optlen.

settimeout(timeout)

Set a timeout on socket operations. ‘timeout’ can be a float, giving in seconds, or None. Setting a timeout of None disables the timeout feature and is equivalent to setblocking(1). Setting a timeout of zero is the same as setblocking(0).

shutdown(flag)

Shut down the reading side of the socket (flag == SHUT_RD), the writing side of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).

property family

Read-only access to the address family for this socket.

property proto

the socket protocol

property type

Read-only access to the socket type.

create_server(address, *, family=AddressFamily.AF_INET, backlog=None, reuse_port=False, dualstack_ipv6=False)[source]

Convenience function which creates a SOCK_STREAM type socket bound to address (a 2-tuple (host, port)) and return the socket object.

family should be either AF_INET or AF_INET6. backlog is the queue size passed to socket.listen(). reuse_port dictates whether to use the SO_REUSEPORT socket option. dualstack_ipv6: if true and the platform supports it, it will create an AF_INET6 socket able to accept both IPv4 or IPv6 connections. When false it will explicitly disable this option on platforms that enable it by default (e.g. Linux).

>>> with create_server(('', 8000)) as server:
...     while True:
...         conn, addr = server.accept()
...         # handle new connection
getaddrinfo(host, port, family=0, type=0, proto=0, flags=0)[source]

Resolve host and port into list of address info entries.

Translate the host/port argument into a sequence of 5-tuples that contain all the necessary arguments for creating a socket connected to that service. host is a domain name, a string representation of an IPv4/v6 address or None. port is a string service name such as ‘http’, a numeric port number or None. By passing None as the value of host and port, you can pass NULL to the underlying C API.

The family, type and proto arguments can be optionally specified in order to narrow the list of addresses returned. Passing zero as a value for each of these arguments selects the full range of results.

getdefaulttimeout() timeout

Returns the default timeout in seconds (float) for new socket objects. A value of None indicates that new socket objects have no timeout. When the socket module is first imported, the default is None.

getfqdn(name='')[source]

Get fully qualified domain name from name.

An empty argument is interpreted as meaning the local host.

First the hostname returned by gethostbyaddr() is checked, then possibly existing aliases. In case no FQDN is available, hostname from gethostname() is returned.

gethostbyaddr(ip_address)[source]

Return the true host name, a list of aliases, and a list of IP addresses, for a host. The host argument is a string giving a host name or IP number.

gethostbyname(host) address[source]

Return the IP address (a string of the form ‘255.255.255.255’) for a host.

gethostbyname_ex(host)[source]

Return the true host name, a list of aliases, and a list of IP addresses, for a host. The host argument is a string giving a host name or IP number. Resolve host and port into list of address info entries.

gethostname() string

Return the current host name.

getnameinfo(sockaddr, flags)[source]

Get host and port for a sockaddr.

getprotobyname(name) integer

Return the protocol number for the named protocol. (Rarely used.)

getservbyname(servicename[, protocolname]) integer

Return a port number from a service name and protocol name. The optional protocol name, if given, should be ‘tcp’ or ‘udp’, otherwise any protocol will match.

getservbyport(port[, protocolname]) string

Return the service name from a port number and protocol name. The optional protocol name, if given, should be ‘tcp’ or ‘udp’, otherwise any protocol will match.

has_dualstack_ipv6()[source]

Return True if the platform supports creating a SOCK_STREAM socket which can handle both AF_INET and AF_INET6 (IPv4 / IPv6) connections.

htonl(integer) integer

Convert a 32-bit integer from host to network byte order.

htons(integer) integer

Convert a 16-bit unsigned integer from host to network byte order.

inet_aton(string) bytes giving packed 32-bit IP representation

Convert an IP address in string format (123.45.67.89) to the 32-bit packed binary format used in low-level network functions.

inet_ntoa(packed_ip) ip_address_string

Convert an IP address from 32-bit packed binary format to string format

inet_ntop(af, packed_ip) string formatted IP address

Convert a packed IP address of the given family to string format.

inet_pton(af, ip) packed IP address string

Convert an IP address from string format to a packed string suitable for use with low-level network functions.

ntohl(integer) integer

Convert a 32-bit integer from network to host byte order.

ntohs(integer) integer

Convert a 16-bit unsigned integer from network to host byte order.

recv_fds(sock, bufsize, maxfds[, flags]) (data, list of file[source]

descriptors, msg_flags, address)

Receive up to maxfds file descriptors returning the message data and a list containing the descriptors.

send_fds(sock, buffers, fds[, flags[, address]]) integer[source]

Send the list of file descriptors fds over an AF_UNIX socket.

setdefaulttimeout(timeout)

Set the default timeout in seconds (float) for new socket objects. A value of None indicates that new socket objects have no timeout. When the socket module is first imported, the default is None.

sethostname(name)

Sets the hostname to name.

Next page: gevent.ssl – Secure Sockets Layer (SSL/TLS) module