socket

This module provides socket operations and some related functions. On Unix, it supports IP (Internet Protocol) and Unix domain sockets. On other systems, it only supports IP. Functions specific for a socket are available as methods of the socket object.

Functions:

socket() – create a new socket object socketpair() – create a pair of new socket objects [*] fromfd() – create a socket object from an open file descriptor [*] gethostname() – return the current hostname gethostbyname() – map a hostname to its IP number gethostbyaddr() – map an IP number or hostname to DNS info getservbyname() – map a service name and a protocol name to a port number getprotobyname() – map a protocol name (e.g. ‘tcp’) to a number ntohs(), ntohl() – convert 16, 32 bit int from network to host byte order htons(), htonl() – convert 16, 32 bit int from host to network byte order inet_aton() – convert IP addr string (123.45.67.89) to 32-bit packed format inet_ntoa() – convert 32-bit packed format IP to string (123.45.67.89) ssl() – secure socket layer support (only available if configured) socket.getdefaulttimeout() – get the default timeout value socket.setdefaulttimeout() – set the default timeout value create_connection() – connects to an address, with an optional timeout and

optional source address.

[*] not available on all platforms!

Special objects:

SocketType – type object for socket objects error – exception raised for I/O errors has_ipv6 – boolean value indicating if IPv6 is supported

Integer constants:

AF_INET, AF_UNIX – socket domains (first argument to socket() call) SOCK_STREAM, SOCK_DGRAM, SOCK_RAW – socket types (second argument)

Many other constants may be defined; these may be used in calls to the setsockopt() and getsockopt() methods.

Functions

RAND_add(string, entropy) Mix string into the OpenSSL PRNG state.
RAND_egd((path) -> bytes) Queries the entropy gather daemon (EGD) on the socket named by ‘path’.
RAND_status(() -> 0 or 1) Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not.
StringIO StringIO([s]) – Return a StringIO-like stream for reading or writing
create_connection(address[, timeout, ...]) Connect to address and return the socket object.
fromfd((fd, family, type[, ...) Create a socket object from a duplicate of the given file descriptor.
getaddrinfo(host, port [, family, socktype, ...) -> list of (family, socktype, proto, canonname, sockaddr)
getdefaulttimeout(() -> timeout) Returns the default timeout in seconds (float) for new socket objects.
getfqdn([name]) Get fully qualified domain name from name.
gethostbyaddr(host) -> (name, aliaslist, ...) Return the true host name, a list of aliases, and a list of IP addresses, for a host.
gethostbyname((host) -> address) Return the IP address (a string of the form ‘255.255.255.255’) for a host.
gethostbyname_ex(host) -> (name, aliaslist, ...) Return the true host name, a list of aliases, and a list of IP addresses, for a host.
gethostname(() -> string) Return the current host name.
getnameinfo(sockaddr, flags) –> (host, port) Get host and port for a sockaddr.
getprotobyname((name) -> integer) Return the protocol number for the named protocol.
getservbyname((servicename[, ...) Return a port number from a service name and protocol name.
getservbyport((port[, protocolname]) -> string) Return the service name from a port number and protocol name.
htonl((integer) -> integer) Convert a 32-bit integer from host to network byte order.
htons((integer) -> integer) Convert a 16-bit integer from host to network byte order.
inet_aton(...) 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, ...) 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.
meth(name, self, *args)
ntohl((integer) -> integer) Convert a 32-bit integer from network to host byte order.
ntohs((integer) -> integer) Convert a 16-bit integer from network to host byte order.
setdefaulttimeout(timeout) Set the default timeout in seconds (float) for new socket objects.
socketpair([family[, type[, ...) Create a pair of socket objects from the sockets returned by the platform socketpair() function.
ssl(sock[, keyfile, certfile])

Classes

MethodType alias of instancemethod
SocketType alias of _socketobject
partial partial(func, *args, **keywords) - new function with partial application
socket alias of _socketobject

Exceptions

error
gaierror
herror
sslerror alias of SSLError
timeout