1 # Wrapper module for _socket, providing some additional facilities
2 # implemented in Python.
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.
11 socket() -- create a new socket object
12 fromfd() -- create a socket object from an open file descriptor [*]
13 gethostname() -- return the current hostname
14 gethostbyname() -- map a hostname to its IP number
15 gethostbyaddr() -- map an IP number or hostname to DNS info
16 getservbyname() -- map a service name and a protocol name to a port number
17 getprotobyname() -- mape a protocol name (e.g. 'tcp') to a number
18 ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order
19 htons(), htonl() -- convert 16, 32 bit int from host to network byte order
20 inet_aton() -- convert IP addr string (123.45.67.89) to 32-bit packed format
21 inet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89)
22 ssl() -- secure socket layer support (only available if configured)
24 [*] not available on all platforms!
28 SocketType -- type object for socket objects
29 error -- exception raised for I/O errors
33 AF_INET, AF_UNIX -- socket domains (first argument to socket() call)
34 SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument)
36 Many other constants may be defined; these may be used in calls to
37 the setsockopt() and getsockopt() methods.
44 if (sys
.platform
.lower().startswith("win")
45 or (hasattr(os
, 'uname') and os
.uname()[0] == "BeOS")):
47 # be sure this happens only once, even in the face of reload():
51 _realsocketcall
= socket
53 def socket(family
, type, proto
=0):
54 return _socketobject(_realsocketcall(family
, type, proto
))
58 if sys
.platform
.lower().startswith("win"):
60 errorTab
[10004] = "The operation was interrupted."
61 errorTab
[10009] = "A bad file handle was passed."
62 errorTab
[10013] = "Permission denied."
63 errorTab
[10014] = "A fault occurred on the network??" # WSAEFAULT
64 errorTab
[10022] = "An invalid operation was attempted."
65 errorTab
[10035] = "The socket operation would block"
66 errorTab
[10036] = "A blocking operation is already in progress."
67 errorTab
[10048] = "The network address is in use."
68 errorTab
[10054] = "The connection has been reset."
69 errorTab
[10058] = "The network has been shut down."
70 errorTab
[10060] = "The operation timed out."
71 errorTab
[10061] = "Connection refused."
72 errorTab
[10063] = "The name is too long."
73 errorTab
[10064] = "The host is down."
74 errorTab
[10065] = "The host is unreachable."
79 """Get fully qualified domain name from name.
81 An empty argument is interpreted as meaning the local host.
83 First the hostname returned by gethostbyaddr() is checked, then
84 possibly existing aliases. In case no FQDN is available, hostname
88 if not name
or name
== '0.0.0.0':
91 hostname
, aliases
, ipaddrs
= gethostbyaddr(name
)
95 aliases
.insert(0, hostname
)
105 # These classes are used by the socket() defined on Windows and BeOS
106 # platforms to provide a best-effort implementation of the cleanup
107 # semantics needed when sockets can't be dup()ed.
109 # These are not actually used on other platforms.
114 def __init__(self
, sock
):
124 sock
, addr
= self
._sock
.accept()
125 return _socketobject(sock
), addr
128 return _socketobject(self
._sock
)
130 def makefile(self
, mode
='r', bufsize
=-1):
131 return _fileobject(self
._sock
, mode
, bufsize
)
133 _s
= "def %s(self, *args): return apply(self._sock.%s, args)\n\n"
134 for _m
in ('bind', 'connect', 'connect_ex', 'fileno', 'listen',
135 'getpeername', 'getsockname',
136 'getsockopt', 'setsockopt',
137 'recv', 'recvfrom', 'send', 'sendto',
145 def __init__(self
, sock
, mode
, bufsize
):
150 self
._rbufsize
= max(1, bufsize
)
151 self
._wbufsize
= bufsize
152 self
._wbuf
= self
._rbuf
= ""
166 self
._sock
.send(self
._wbuf
)
170 return self
._sock
.fileno()
172 def write(self
, data
):
173 self
._wbuf
= self
._wbuf
+ data
174 if self
._wbufsize
== 1:
178 if len(self
._wbuf
) >= self
._wbufsize
:
181 def writelines(self
, list):
182 filter(self
._sock
.send
, list)
185 def read(self
, n
=-1):
189 data
= self
._rbuf
[:n
]
190 self
._rbuf
= self
._rbuf
[n
:]
196 new
= self
._sock
.recv(max(n
, self
._rbufsize
))
206 k
= max(512, self
._rbufsize
)
210 new
= self
._sock
.recv(k
)
213 k
= min(k
*2, 1024**2)
216 def readline(self
, limit
=-1):
218 i
= self
._rbuf
.find('\n')
219 while i
< 0 and not (0 < limit
<= len(self
._rbuf
)):
220 new
= self
._sock
.recv(self
._rbufsize
)
223 if i
>= 0: i
= i
+ len(self
._rbuf
)
224 self
._rbuf
= self
._rbuf
+ new
225 if i
< 0: i
= len(self
._rbuf
)
227 if 0 <= limit
< len(self
._rbuf
): i
= limit
228 data
, self
._rbuf
= self
._rbuf
[:i
], self
._rbuf
[i
:]
231 def readlines(self
, sizehint
= 0):
235 line
= self
.readline()
239 if sizehint
and total
>= sizehint
: