Module socket
[hide private]
[frames] | no frames]

Module 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() -- mape 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

 [*] 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.

Classes [hide private]
_closedsocket
_socketobject
socket([family[, type[, proto]]]) -> socket object
SocketType
socket([family[, type[, proto]]]) -> socket object
socket
socket([family[, type[, proto]]]) -> socket object
_fileobject
Faux file object attached to a socket object.
SSLType
error
gaierror
herror
sslerror
timeout
Functions [hide private]
 
ssl(sock, keyfile=None, certfile=None)
 
getfqdn(name='')
Get fully qualified domain name from name.
 
RAND_add(string, entropy)
Mix string into the OpenSSL PRNG state.
bytes
RAND_egd(path)
Queries the entropy gather daemon (EGD) on socket path.
0 or 1
RAND_status()
Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not.
socket object
fromfd(fd, family, type, proto=...)
Create a socket object from a duplicate of the given file descriptor.
list of (family, socktype, proto, canonname, sockaddr)
getaddrinfo(host, port, family=..., socktype=..., proto=..., flags=...)
Resolve host and port into addrinfo struct.
timeout
getdefaulttimeout()
Returns the default timeout in floating seconds for new socket objects.
(name, aliaslist, addresslist)
gethostbyaddr(host)
Return the true host name, a list of aliases, and a list of IP addresses, for a host.
address
gethostbyname(host)
Return the IP address (a string of the form '255.255.255.255') for a host.
(name, aliaslist, addresslist)
gethostbyname_ex(host)
Return the true host name, a list of aliases, and a list of IP addresses, for a host.
string
gethostname()
Return the current host name.
 
getnameinfo(...)
getnameinfo(sockaddr, flags) --> (host, port)
integer
getprotobyname(name)
Return the protocol number for the named protocol.
integer
getservbyname(servicename, protocolname=...)
Return a port number from a service name and protocol name.
string
getservbyport(port, protocolname=...)
Return the service name from a port number and protocol name.
integer
htonl(integer)
Convert a 32-bit integer from host to network byte order.
integer
htons(integer)
Convert a 16-bit integer from host to network byte order.
packed 32-bit IP representation
inet_aton(string)
Convert an IP address in string format (123.45.67.89) to the 32-bit packed binary format used in low-level network functions.
ip_address_string
inet_ntoa(packed_ip)
Convert an IP address from 32-bit packed binary format to string format
string formatted IP address
inet_ntop(af, packed_ip)
Convert a packed IP address of the given family to string format.
packed IP address string
inet_pton(af, ip)
Convert an IP address from string format to a packed string suitable for use with low-level network functions.
integer
ntohl(integer)
Convert a 32-bit integer from network to host byte order.
integer
ntohs(integer)
Convert a 16-bit integer from network to host byte order.
 
setdefaulttimeout(timeout)
Set the default timeout in floating seconds for new socket objects.
(socket object, socket object)
socketpair(family=..., type=..., proto=...=...)
Create a pair of socket objects from the sockets returned by the platform socketpair() function.
Variables [hide private]
  _have_ssl = True
  EBADF = 9
  errorTab = {}
  _socketmethods = ('bind', 'connect', 'connect_ex', 'fileno', '...
  _delegate_methods = ('recv', 'recvfrom', 'recv_into', 'recvfro...
  AF_APPLETALK = 5
  AF_ASH = 18
  AF_ATMPVC = 8
  AF_ATMSVC = 20
  AF_AX25 = 3
  AF_BLUETOOTH = 31
  AF_BRIDGE = 7
  AF_DECnet = 12
  AF_ECONET = 19
  AF_INET = 2
  AF_INET6 = 10
  AF_IPX = 4
  AF_IRDA = 23
  AF_KEY = 15
  AF_NETBEUI = 13
  AF_NETLINK = 16
  AF_NETROM = 6
  AF_PACKET = 17
  AF_PPPOX = 24
  AF_ROSE = 11
  AF_ROUTE = 16
  AF_SECURITY = 14
  AF_SNA = 22
  AF_UNIX = 1
  AF_UNSPEC = 0
  AF_WANPIPE = 25
  AF_X25 = 9
  AI_ADDRCONFIG = 32
  AI_ALL = 16
  AI_CANONNAME = 2
  AI_NUMERICHOST = 4
  AI_NUMERICSERV = 1024
  AI_PASSIVE = 1
  AI_V4MAPPED = 8
  BDADDR_ANY = '00:00:00:00:00:00'
  BDADDR_LOCAL = '00:00:00:FF:FF:FF'
  BTPROTO_L2CAP = 0
  BTPROTO_RFCOMM = 3
  BTPROTO_SCO = 2
  CAPI = <PyCObject object at 0x4062b698>
  EAI_ADDRFAMILY = -9
  EAI_AGAIN = -3
  EAI_BADFLAGS = -1
  EAI_FAIL = -4
  EAI_FAMILY = -6
  EAI_MEMORY = -10
  EAI_NODATA = -5
  EAI_NONAME = -2
  EAI_OVERFLOW = -12
  EAI_SERVICE = -8
  EAI_SOCKTYPE = -7
  EAI_SYSTEM = -11
  INADDR_ALLHOSTS_GROUP = -536870911
  INADDR_ANY = 0
  INADDR_BROADCAST = -1
  INADDR_LOOPBACK = 2130706433
  INADDR_MAX_LOCAL_GROUP = -536870657
  INADDR_NONE = -1
  INADDR_UNSPEC_GROUP = -536870912
  IPPORT_RESERVED = 1024
  IPPORT_USERRESERVED = 5000
  IPPROTO_AH = 51
  IPPROTO_DSTOPTS = 60
  IPPROTO_EGP = 8
  IPPROTO_ESP = 50
  IPPROTO_FRAGMENT = 44
  IPPROTO_GRE = 47
  IPPROTO_HOPOPTS = 0
  IPPROTO_ICMP = 1
  IPPROTO_ICMPV6 = 58
  IPPROTO_IDP = 22
  IPPROTO_IGMP = 2
  IPPROTO_IP = 0
  IPPROTO_IPIP = 4
  IPPROTO_IPV6 = 41
  IPPROTO_NONE = 59
  IPPROTO_PIM = 103
  IPPROTO_PUP = 12
  IPPROTO_RAW = 255
  IPPROTO_ROUTING = 43
  IPPROTO_RSVP = 46
  IPPROTO_TCP = 6
  IPPROTO_TP = 29
  IPPROTO_UDP = 17
  IPV6_CHECKSUM = 7
  IPV6_DSTOPTS = 4
  IPV6_HOPLIMIT = 8
  IPV6_HOPOPTS = 3
  IPV6_JOIN_GROUP = 20
  IPV6_LEAVE_GROUP = 21
  IPV6_MULTICAST_HOPS = 18
  IPV6_MULTICAST_IF = 17
  IPV6_MULTICAST_LOOP = 19
  IPV6_NEXTHOP = 9
  IPV6_PKTINFO = 2
  IPV6_RTHDR = 5
  IPV6_RTHDR_TYPE_0 = 0
  IPV6_UNICAST_HOPS = 16
  IPV6_V6ONLY = 26
  IP_ADD_MEMBERSHIP = 35
  IP_DEFAULT_MULTICAST_LOOP = 1
  IP_DEFAULT_MULTICAST_TTL = 1
  IP_DROP_MEMBERSHIP = 36
  IP_HDRINCL = 3
  IP_MAX_MEMBERSHIPS = 20
  IP_MULTICAST_IF = 32
  IP_MULTICAST_LOOP = 34
  IP_MULTICAST_TTL = 33
  IP_OPTIONS = 4
  IP_RECVOPTS = 6
  IP_RECVRETOPTS = 7
  IP_RETOPTS = 7
  IP_TOS = 1
  IP_TTL = 2
  MSG_CTRUNC = 8
  MSG_DONTROUTE = 4
  MSG_DONTWAIT = 64
  MSG_EOR = 128
  MSG_OOB = 1
  MSG_PEEK = 2
  MSG_TRUNC = 32
  MSG_WAITALL = 256
  NETLINK_DNRTMSG = 14
  NETLINK_FIREWALL = 3
  NETLINK_IP6_FW = 13
  NETLINK_NFLOG = 5
  NETLINK_ROUTE = 0
  NETLINK_USERSOCK = 2
  NETLINK_XFRM = 6
  NI_DGRAM = 16
  NI_MAXHOST = 1025
  NI_MAXSERV = 32
  NI_NAMEREQD = 8
  NI_NOFQDN = 4
  NI_NUMERICHOST = 1
  NI_NUMERICSERV = 2
  PACKET_BROADCAST = 1
  PACKET_FASTROUTE = 6
  PACKET_HOST = 0
  PACKET_LOOPBACK = 5
  PACKET_MULTICAST = 2
  PACKET_OTHERHOST = 3
  PACKET_OUTGOING = 4
  PF_PACKET = 17
  SHUT_RD = 0
  SHUT_RDWR = 2
  SHUT_WR = 1
  SOCK_DGRAM = 2
  SOCK_RAW = 3
  SOCK_RDM = 4
  SOCK_SEQPACKET = 5
  SOCK_STREAM = 1
  SOL_IP = 0
  SOL_SOCKET = 1
  SOL_TCP = 6
  SOL_UDP = 17
  SOMAXCONN = 128
  SO_ACCEPTCONN = 30
  SO_BROADCAST = 6
  SO_DEBUG = 1
  SO_DONTROUTE = 5
  SO_ERROR = 4
  SO_KEEPALIVE = 9
  SO_LINGER = 13
  SO_OOBINLINE = 10
  SO_RCVBUF = 8
  SO_RCVLOWAT = 18
  SO_RCVTIMEO = 20
  SO_REUSEADDR = 2
  SO_SNDBUF = 7
  SO_SNDLOWAT = 19
  SO_SNDTIMEO = 21
  SO_TYPE = 3
  SSL_ERROR_EOF = 8
  SSL_ERROR_INVALID_ERROR_CODE = 9
  SSL_ERROR_SSL = 1
  SSL_ERROR_SYSCALL = 5
  SSL_ERROR_WANT_CONNECT = 7
  SSL_ERROR_WANT_READ = 2
  SSL_ERROR_WANT_WRITE = 3
  SSL_ERROR_WANT_X509_LOOKUP = 4
  SSL_ERROR_ZERO_RETURN = 6
  TCP_CORK = 3
  TCP_DEFER_ACCEPT = 9
  TCP_INFO = 11
  TCP_KEEPCNT = 6
  TCP_KEEPIDLE = 4
  TCP_KEEPINTVL = 5
  TCP_LINGER2 = 8
  TCP_MAXSEG = 2
  TCP_NODELAY = 1
  TCP_QUICKACK = 12
  TCP_SYNCNT = 7
  TCP_WINDOW_CLAMP = 10
  has_ipv6 = True

Imports: _socket, _ssl, os, sys, _realsocket, _realssl


Function Details [hide private]

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.

RAND_add(string, entropy)

 

Mix string into the OpenSSL PRNG state. entropy (a float) is a lower bound on the entropy contained in string.

RAND_egd(path)

 

Queries the entropy gather daemon (EGD) on socket path. Returns number of bytes read. Raises socket.sslerror if connection to EGD fails or if it does provide enough data to seed PRNG.

Returns: bytes

RAND_status()

 

Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not. It is necessary to seed the PRNG with RAND_add() on some platforms before using the ssl() function.

Returns: 0 or 1

fromfd(fd, family, type, proto=...)

 

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

Returns: socket object

getdefaulttimeout()

 

Returns the default timeout in floating seconds 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.

Returns: timeout

gethostbyaddr(host)

 

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.

Returns: (name, aliaslist, addresslist)

gethostbyname_ex(host)

 

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.

Returns: (name, aliaslist, addresslist)

getnameinfo(...)

 

getnameinfo(sockaddr, flags) --> (host, port)

Get host and port for a sockaddr.

getprotobyname(name)

 

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

Returns: integer

getservbyname(servicename, protocolname=...)

 

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.

Returns: integer

getservbyport(port, protocolname=...)

 

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.

Returns: string

setdefaulttimeout(timeout)

 

Set the default timeout in floating seconds 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.

socketpair(family=..., type=..., proto=...=...)

 

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.

Returns: (socket object, socket object)

Variables Details [hide private]

_socketmethods

Value:
('bind',
 'connect',
 'connect_ex',
 'fileno',
 'listen',
 'getpeername',
 'getsockname',
 'getsockopt',
...

_delegate_methods

Value:
('recv', 'recvfrom', 'recv_into', 'recvfrom_into', 'send', 'sendto')