5 This module provides socket operations and some related functions.
6 On Unix, it supports IP (Internet Protocol) and Unix domain sockets.
7 On other systems, it only supports IP. Functions specific for a
8 socket are available as methods of the socket object.
12 socket() -- create a new socket object
13 fromfd() -- create a socket object from an open file descriptor [*]
14 gethostname() -- return the current hostname
15 gethostbyname() -- map a hostname to its IP number
16 gethostbyaddr() -- map an IP number or hostname to DNS info
17 getservbyname() -- map a service name and a protocol name to a port number
18 getprotobyname() -- mape a protocol name (e.g. 'tcp') to a number
19 ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order
20 htons(), htonl() -- convert 16, 32 bit int from host to network byte order
21 inet_aton() -- convert IP addr string (123.45.67.89) to 32-bit packed format
22 inet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89)
23 ssl() -- secure socket layer support (only available if configured)
25 [*] not available on all platforms!
29 SocketType -- type object for socket objects
30 error -- exception raised for I/O errors
34 AF_INET, AF_UNIX -- socket domains (first argument to socket() call)
35 SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument)
37 Many other constants may be defined; these may be used in calls to
38 the setsockopt() and getsockopt() methods.
49 if (sys.platform.lower().startswith(
"win")
50 or (hasattr(os,
'uname')
and os.uname()[0] ==
"BeOS")
51 or (sys.platform==
"riscos")):
53 _realsocketcall = _socket.socket
59 _realsslcall = _socket.ssl
60 except AttributeError:
63 def ssl(sock, keyfile=None, certfile=None):
64 if hasattr(sock,
"_sock"):
70 if sys.platform.lower().startswith(
"win"):
72 errorTab[10004] =
"The operation was interrupted."
73 errorTab[10009] =
"A bad file handle was passed."
74 errorTab[10013] =
"Permission denied."
75 errorTab[10014] =
"A fault occurred on the network??"
76 errorTab[10022] =
"An invalid operation was attempted."
77 errorTab[10035] =
"The socket operation would block"
78 errorTab[10036] =
"A blocking operation is already in progress."
79 errorTab[10048] =
"The network address is in use."
80 errorTab[10054] =
"The connection has been reset."
81 errorTab[10058] =
"The network has been shut down."
82 errorTab[10060] =
"The operation timed out."
83 errorTab[10061] =
"Connection refused."
84 errorTab[10063] =
"The name is too long."
85 errorTab[10064] =
"The host is down."
86 errorTab[10065] =
"The host is unreachable."
87 __all__.append(
"errorTab")
92 """Get fully qualified domain name from name.
94 An empty argument is interpreted as meaning the local host.
96 First the hostname returned by gethostbyaddr() is checked, then
97 possibly existing aliases. In case no FQDN is available, hostname
101 if not name
or name ==
'0.0.0.0':
104 hostname, aliases, ipaddrs = gethostbyaddr(name)
108 aliases.insert(0, hostname)
126 'bind',
'connect',
'connect_ex',
'fileno',
'listen',
127 'getpeername',
'getsockname',
'getsockopt',
'setsockopt',
128 'recv',
'recvfrom',
'send',
'sendall',
'sendto',
'setblocking',
'shutdown')
134 raise error(9,
'Bad file descriptor')
141 self.
_sock = self.__class__._closedsocket()
147 sock, addr = self._sock.accept()
156 _s =
"def %s(self, *args): return self._sock.%s(*args)\n\n"
157 for _m
in _socketmethods:
184 self._sock.sendall(self.
_wbuf)
188 return self._sock.fileno()
200 filter(self._sock.sendall, list)
207 data = self.
_rbuf[:n]
228 new = self._sock.recv(k)
231 k =
min(k*2, 1024**2)
236 i = self._rbuf.find(
'\n')
237 while i < 0
and not (0 < limit <= len(self.
_rbuf)):
241 if i >= 0: i = i + len(self.
_rbuf)
243 if i < 0: i = len(self.
_rbuf)
245 if 0 <= limit < len(self.
_rbuf): i = limit
257 if sizehint
and total >= sizehint: