Cooperative socket module (gevent.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 0x2ba5a9e7c310>)

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.

gevent.socket.fromfd(*args)
gevent.socket.getaddrinfo(host, port, *args, **kwargs)

Some approximation of socket.getaddrinfo() implemented using gevent.dns.

If host is not a string, does not has any dots or is a numeric IP address, then the standard socket.getaddrinfo() is called.

Otherwise, calls either resolve_ipv4() or resolve_ipv6() and formats the result the way socket.getaddrinfo() does it.

Differs in the following ways:

  • raises DNSError (a subclass of gaierror) with libevent-dns error codes instead of standard socket error codes
  • IPv6 support is untested.
  • AF_UNSPEC only tries IPv4
  • only supports TCP, UDP, IP protocols
  • port must be numeric, does not support string service names. see socket.getservbyname
  • flags argument is ignored

Additionally, supports evdns_flags keyword arguments (default 0) that is passed to dns functions.

gevent.socket.gethostbyname(hostname)

socket.gethostbyname() implemented using gevent.dns.

Differs in the following ways:

  • raises DNSError (a subclass of socket.gaierror) with dns error codes instead of standard socket error codes
  • does not support /etc/hosts but calls the original socket.gethostbyname() if hostname has no dots
  • does not iterate through all addresses, instead picks a random one each time
class gevent.socket.socket(family=2, type=1, proto=0, _sock=None)
family
the socket family
proto
the socket protocol
type
the socket type
bind(*args)

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(*args)

fileno() -> integer

Return the integer file descriptor of the socket.

getpeername(*args)

getpeername() -> address info

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

getsockname(*args)

getsockname() -> address info

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

getsockopt(*args)

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(*args)

listen(backlog)

Enable a server to accept connections. The backlog argument must be at least 1; it specifies the number of unaccepted connection that the system will allow before refusing new connections.

setsockopt(*args)

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.

shutdown(*args)

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

accept()
close()
connect(address)
connect_ex(address)
dup()

dup() -> socket object

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

makefile(mode='r', bufsize=-1)
recv(*args)
recvfrom(*args)
recvfrom_into(*args)
recv_into(*args)
send(data, flags=0, timeout=<object object at 0x2ba5a9e7c320>)
sendall(data, flags=0)
sendto(*args)
setblocking(flag)
settimeout(howlong)
gettimeout()
gevent.socket.socketpair(*args)
gevent.socket.ssl(sock, keyfile=None, certfile=None)
A replacement for the old socket.ssl function. Designed for compability with Python 2.5 and earlier. Will disappear in Python 3.0.
gevent.socket.SocketType
alias of socket

Previous topic

Synchronized queues (gevent.queue module)

Next topic

Monkey patching (gevent.monkey module)

This Page