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. 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.
47 __all__
.extend(os
._get
_exports
_list
(_socket
))
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
55 def socket(family
, type, proto
=0):
56 return _socketobject(_realsocketcall(family
, type, proto
))
59 _realsslcall
= _socket
.ssl
60 except AttributeError:
63 def ssl(sock
, keyfile
=None, certfile
=None):
64 if hasattr(sock
, "_sock"):
66 return _realsslcall(sock
, keyfile
, certfile
)
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??" # WSAEFAULT
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
)
118 # These classes are used by the socket() defined on Windows and BeOS
119 # platforms to provide a best-effort implementation of the cleanup
120 # semantics needed when sockets can't be dup()ed.
122 # These are not actually used on other platforms.
127 def __init__(self
, sock
):
137 sock
, addr
= self
._sock
.accept()
138 return _socketobject(sock
), addr
141 return _socketobject(self
._sock
)
143 def makefile(self
, mode
='r', bufsize
=-1):
144 return _fileobject(self
._sock
, mode
, bufsize
)
146 _s
= "def %s(self, *args): return apply(self._sock.%s, args)\n\n"
147 for _m
in ('bind', 'connect', 'connect_ex', 'fileno', 'listen',
148 'getpeername', 'getsockname',
149 'getsockopt', 'setsockopt',
150 'recv', 'recvfrom', 'send', 'sendto',
158 def __init__(self
, sock
, mode
, bufsize
):
163 self
._rbufsize
= max(1, bufsize
)
164 self
._wbufsize
= bufsize
165 self
._wbuf
= self
._rbuf
= ""
179 self
._sock
.send(self
._wbuf
)
183 return self
._sock
.fileno()
185 def write(self
, data
):
186 self
._wbuf
= self
._wbuf
+ data
187 if self
._wbufsize
== 1:
191 if len(self
._wbuf
) >= self
._wbufsize
:
194 def writelines(self
, list):
195 filter(self
._sock
.send
, list)
198 def read(self
, n
=-1):
202 data
= self
._rbuf
[:n
]
203 self
._rbuf
= self
._rbuf
[n
:]
209 new
= self
._sock
.recv(max(n
, self
._rbufsize
))
219 k
= max(512, self
._rbufsize
)
223 new
= self
._sock
.recv(k
)
226 k
= min(k
*2, 1024**2)
229 def readline(self
, limit
=-1):
231 i
= self
._rbuf
.find('\n')
232 while i
< 0 and not (0 < limit
<= len(self
._rbuf
)):
233 new
= self
._sock
.recv(self
._rbufsize
)
236 if i
>= 0: i
= i
+ len(self
._rbuf
)
237 self
._rbuf
= self
._rbuf
+ new
238 if i
< 0: i
= len(self
._rbuf
)
240 if 0 <= limit
< len(self
._rbuf
): i
= limit
241 data
, self
._rbuf
= self
._rbuf
[:i
], self
._rbuf
[i
:]
244 def readlines(self
, sizehint
= 0):
248 line
= self
.readline()
252 if sizehint
and total
>= sizehint
: