gevent._socket3 – Python 3 socket module#

Python 3 socket module.

SocketType#

alias of socket

class socket(family=-1, type=-1, proto=-1, fileno=None)[source]#

Bases: SocketMixin

gevent socket.socket for Python 3.

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.

accept() -> (socket object, address info)[source]#

Wait for an incoming connection. Return a new socket representing the connection, and the address of the client. For IP sockets, the address info is a pair (hostaddr, port).

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)#

Connect to address, returning a result code.

Changed in version 23.7.0: No longer uses an overridden connect method on this object. Instead, like the standard library, this method always uses a non-replacable internal connection function.

detach() file descriptor[source]#

Close the socket object without closing the underlying file descriptor. The object cannot be used after this call; when the real file descriptor is closed, the number that was previously used here may be reused. The fileno() method, after this call, will return an invalid socket id.

The previous descriptor is returned.

Changed in version 1.5: Also immediately drop any native event loop resources.

dup() socket object[source]#

Return a new socket object connected to the same system resource.

fileno() integer#

Return the integer file descriptor of the socket.

get_inheritable()[source]#

Get the inheritable flag 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). For IPv6 sockets, the address info is a 4-tuple (hostaddr, port, flowinfo, scope_id).

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.

makefile(mode='r', buffering=None, *, encoding=None, errors=None, newline=None)[source]#

Return an I/O stream connected to the socket

The arguments are as for io.open() after the filename, except the only mode characters supported are ‘r’, ‘w’ and ‘b’. The semantics are similar too.

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])#

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.

sendfile(file[, offset[, count]]) sent[source]#

Send a file until EOF is reached by using high-performance os.sendfile() and return the total number of bytes which were sent. file must be a regular file object opened in binary mode. If os.sendfile() is not available (e.g. Windows) or file is not a regular file socket.send() will be used instead. offset tells from where to start reading the file. If specified, count is the total number of bytes to transmit as opposed to sending the file until EOF is reached. File position is updated on return or also in case of error in which case file.tell() can be used to figure out the number of bytes which were sent. The socket must be of SOCK_STREAM type. Non-blocking sockets are not supported.

New in version 1.1rc4: Added in Python 3.5, but available under all Python 3 versions in gevent.

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).

set_inheritable(inheritable)[source]#

Set the inheritable flag of the socket

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_connection(address, timeout=None, source_address=None, *, all_errors=False) socket[source]#

Connect to address and return the gevent.socket.socket object.

Convenience function. Connect to address (a 2-tuple (host, port)) and return the socket object. Passing the optional timeout parameter will set the timeout on the socket instance before attempting to connect. If no timeout is supplied, the global default timeout setting returned by getdefaulttimeout() is used. If source_address is set it must be a tuple of (host, port) for the socket to bind as a source address before making the connection. A host of ‘’ or port 0 tells the OS to use the default.

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().

Changed in version 22.08.0: Add the all_errors argument. This only has meaning on Python 3.11+; it is a programming error to pass it on earlier versions.

Changed in version 23.7.0: You can pass a value for all_errors on any version of Python. It is forced to false for any version before 3.11 inside the function.

fromfd(fd, family, type[, proto]) socket object[source]#

Create a socket object from a duplicate of the given file descriptor. The remaining arguments are the same as for socket().

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.

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.

Changed in version 23.7.0: The IPv6 generic address ‘::’ now returns the result of gethostname, like the IPv4 address ‘0.0.0.0’.

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.

getnameinfo(sockaddr, flags)[source]#

Get host and port for a sockaddr.

socketpair([family[, type[, proto]]]) -> (socket object, socket object)[source]#

Create a pair of socket objects from the sockets returned by the platform socketpair() function. The arguments are the same as for socket() except the default family is AF_UNIX if defined on the platform; otherwise, the default is AF_INET.

Changed in version 1.2: All Python 3 versions on Windows supply this function (natively supplied by Python 3.5 and above).