gevent.socket – Cooperative socket module

This module provides socket operations and some related functions. The API of the functions and classes matches the API of the corresponding items in standard socket module exactly, but the synchronous functions in this module only block the current greenlet and let the others run.

For convenience, exceptions (like error and timeout) as well as the constants from socket module are imported into this module.

gevent.socket.create_connection(address, timeout=<object object at 0x139d2d0>, source_address=None)

Connect to address and return the 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. An host of ‘’ or port 0 tells the OS to use the default.

class gevent.socket.socket(family=2, type=1, proto=0, _sock=None)
closed None
family None

the socket family

proto None

the socket protocol

ref None
type None

the socket type

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

fileno() → integer

Return the integer file descriptor of the socket.

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. For IP 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.

listen(backlog)

Enable a server to accept connections. The backlog argument 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.

setsockopt(level, option, value)

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

accept()
close(_closedsocket=<class 'gevent.socket._closedsocket'>, cancel_wait_ex=error(9, 'File descriptor was closed in another greenlet'))
connect(address)
connect_ex(address)
dup() → socket object

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

makefile(mode='r', bufsize=-1)
recv(*args)
recvfrom(*args)
recvfrom_into(*args)
recv_into(*args)
send(data, flags=0, timeout=<object object at 0x139d3c0>)
sendall(data, flags=0)
sendto(*args)
setblocking(flag)
settimeout(howlong)
gettimeout()
shutdown(how)
gevent.socket.SocketType

alias of socket

gevent.socket.fromfd(*args)
gevent.socket.socketpair(*args)
gevent.socket.getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0)
gevent.socket.gethostbyname(hostname)
gevent.socket.gethostbyname_ex(hostname)
gevent.socket.gethostbyaddr(ip_address)
gevent.socket.getnameinfo(sockaddr, flags)
gevent.socket.getfqdn(name='')

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.

gevent.socket.wait_read(fileno, timeout=None, timeout_exc=timeout('timed out', ))

Block the current greenlet until fileno is ready to read.

If timeout is non-negative, then timeout_exc is raised after timeout second has passed. By default timeout_exc is socket.timeout('timed out').

If cancel_wait() is called, raise socket.error(EBADF, 'File descriptor was closed in another greenlet').

gevent.socket.wait_write(fileno, timeout=None, timeout_exc=timeout('timed out', ), event=None)

Block the current greenlet until fileno is ready to write.

If timeout is non-negative, then timeout_exc is raised after timeout second has passed. By default timeout_exc is socket.timeout('timed out').

If cancel_wait() is called, raise socket.error(EBADF, 'File descriptor was closed in another greenlet').

gevent.socket.wait_readwrite(fileno, timeout=None, timeout_exc=timeout('timed out', ), event=None)

Block the current greenlet until fileno is ready to read or write.

If timeout is non-negative, then timeout_exc is raised after timeout second has passed. By default timeout_exc is socket.timeout('timed out').

If cancel_wait() is called, raise socket.error(EBADF, 'File descriptor was closed in another greenlet').

Next page: gevent.ssl – SSL wrapper for socket objects