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')
133 def __getattr__(self
, name
):
134 raise error(9, 'Bad file descriptor')
136 def __init__(self
, sock
):
140 # Avoid referencing globals here
141 self
._sock
= self
.__class
__._closedsocket
()
147 sock
, addr
= self
._sock
.accept()
148 return _socketobject(sock
), addr
151 return _socketobject(self
._sock
)
153 def makefile(self
, mode
='r', bufsize
=-1):
154 return _fileobject(self
._sock
, mode
, bufsize
)
156 _s
= "def %s(self, *args): return self._sock.%s(*args)\n\n"
157 for _m
in _socketmethods
:
163 def __init__(self
, sock
, mode
, bufsize
):
168 self
._rbufsize
= max(1, bufsize
)
169 self
._wbufsize
= bufsize
170 self
._wbuf
= self
._rbuf
= ""
184 self
._sock
.send(self
._wbuf
)
188 return self
._sock
.fileno()
190 def write(self
, data
):
191 self
._wbuf
= self
._wbuf
+ data
192 if self
._wbufsize
== 1:
196 if len(self
._wbuf
) >= self
._wbufsize
:
199 def writelines(self
, list):
200 filter(self
._sock
.send
, list)
203 def read(self
, n
=-1):
207 data
= self
._rbuf
[:n
]
208 self
._rbuf
= self
._rbuf
[n
:]
214 new
= self
._sock
.recv(max(n
, self
._rbufsize
))
224 k
= max(512, self
._rbufsize
)
228 new
= self
._sock
.recv(k
)
231 k
= min(k
*2, 1024**2)
234 def readline(self
, limit
=-1):
236 i
= self
._rbuf
.find('\n')
237 while i
< 0 and not (0 < limit
<= len(self
._rbuf
)):
238 new
= self
._sock
.recv(self
._rbufsize
)
241 if i
>= 0: i
= i
+ len(self
._rbuf
)
242 self
._rbuf
= self
._rbuf
+ new
243 if i
< 0: i
= len(self
._rbuf
)
245 if 0 <= limit
< len(self
._rbuf
): i
= limit
246 data
, self
._rbuf
= self
._rbuf
[:i
], self
._rbuf
[i
:]
249 def readlines(self
, sizehint
= 0):
253 line
= self
.readline()
257 if sizehint
and total
>= sizehint
: