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.
126 'bind', 'connect', 'connect_ex', 'fileno', 'listen',
127 'getpeername', 'getsockname', 'getsockopt', 'setsockopt',
128 'recv', 'recvfrom', 'send', 'sendall', 'sendto', 'setblocking', 'shutdown')
132 def __init__(self
, sock
):
136 self
._sock
= _closedsocket()
142 sock
, addr
= self
._sock
.accept()
143 return _socketobject(sock
), addr
146 return _socketobject(self
._sock
)
148 def makefile(self
, mode
='r', bufsize
=-1):
149 return _fileobject(self
._sock
, mode
, bufsize
)
151 _s
= "def %s(self, *args): return self._sock.%s(*args)\n\n"
152 for _m
in _socketmethods
:
158 def __getattr__(self
, name
):
159 raise error(9, 'Bad file descriptor')
164 def __init__(self
, sock
, mode
, bufsize
):
169 self
._rbufsize
= max(1, bufsize
)
170 self
._wbufsize
= bufsize
171 self
._wbuf
= self
._rbuf
= ""
185 self
._sock
.send(self
._wbuf
)
189 return self
._sock
.fileno()
191 def write(self
, data
):
192 self
._wbuf
= self
._wbuf
+ data
193 if self
._wbufsize
== 1:
197 if len(self
._wbuf
) >= self
._wbufsize
:
200 def writelines(self
, list):
201 filter(self
._sock
.send
, list)
204 def read(self
, n
=-1):
208 data
= self
._rbuf
[:n
]
209 self
._rbuf
= self
._rbuf
[n
:]
215 new
= self
._sock
.recv(max(n
, self
._rbufsize
))
225 k
= max(512, self
._rbufsize
)
229 new
= self
._sock
.recv(k
)
232 k
= min(k
*2, 1024**2)
235 def readline(self
, limit
=-1):
237 i
= self
._rbuf
.find('\n')
238 while i
< 0 and not (0 < limit
<= len(self
._rbuf
)):
239 new
= self
._sock
.recv(self
._rbufsize
)
242 if i
>= 0: i
= i
+ len(self
._rbuf
)
243 self
._rbuf
= self
._rbuf
+ new
244 if i
< 0: i
= len(self
._rbuf
)
246 if 0 <= limit
< len(self
._rbuf
): i
= limit
247 data
, self
._rbuf
= self
._rbuf
[:i
], self
._rbuf
[i
:]
250 def readlines(self
, sizehint
= 0):
254 line
= self
.readline()
258 if sizehint
and total
>= sizehint
: