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)
24 socket.getdefaulttimeout() -- get the default timeout value
25 socket.setdefaulttimeout() -- set the default timeout value
27 [*] not available on all platforms!
31 SocketType -- type object for socket objects
32 error -- exception raised for I/O errors
33 has_ipv6 -- boolean value indicating if IPv6 is supported
37 AF_INET, AF_UNIX -- socket domains (first argument to socket() call)
38 SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument)
40 Many other constants may be defined; these may be used in calls to
41 the setsockopt() and getsockopt() methods.
58 from errno
import EBADF
63 __all__
.extend(os
._get
_exports
_list
(_socket
))
65 __all__
.extend(os
._get
_exports
_list
(_ssl
))
70 def ssl(sock
, keyfile
=None, certfile
=None):
71 if hasattr(sock
, "_sock"):
73 return _realssl(sock
, keyfile
, certfile
)
76 if sys
.platform
.lower().startswith("win"):
78 errorTab
[10004] = "The operation was interrupted."
79 errorTab
[10009] = "A bad file handle was passed."
80 errorTab
[10013] = "Permission denied."
81 errorTab
[10014] = "A fault occurred on the network??" # WSAEFAULT
82 errorTab
[10022] = "An invalid operation was attempted."
83 errorTab
[10035] = "The socket operation would block"
84 errorTab
[10036] = "A blocking operation is already in progress."
85 errorTab
[10048] = "The network address is in use."
86 errorTab
[10054] = "The connection has been reset."
87 errorTab
[10058] = "The network has been shut down."
88 errorTab
[10060] = "The operation timed out."
89 errorTab
[10061] = "Connection refused."
90 errorTab
[10063] = "The name is too long."
91 errorTab
[10064] = "The host is down."
92 errorTab
[10065] = "The host is unreachable."
93 __all__
.append("errorTab")
98 """Get fully qualified domain name from name.
100 An empty argument is interpreted as meaning the local host.
102 First the hostname returned by gethostbyaddr() is checked, then
103 possibly existing aliases. In case no FQDN is available, hostname
107 if not name
or name
== '0.0.0.0':
110 hostname
, aliases
, ipaddrs
= gethostbyaddr(name
)
114 aliases
.insert(0, hostname
)
124 # These classes are used by the socket() defined on Windows and BeOS
125 # platforms to provide a best-effort implementation of the cleanup
126 # semantics needed when sockets can't be dup()ed.
128 # These are not actually used on other platforms.
132 'bind', 'connect', 'connect_ex', 'fileno', 'listen',
133 'getpeername', 'getsockname', 'getsockopt', 'setsockopt',
134 'sendall', 'setblocking',
135 'settimeout', 'gettimeout', 'shutdown')
137 if sys
.platform
== "riscos":
138 _socketmethods
= _socketmethods
+ ('sleeptaskw',)
140 class _closedsocket(object):
143 raise error(EBADF
, 'Bad file descriptor')
144 send
= recv
= sendto
= recvfrom
= __getattr__
= _dummy
146 class _socketobject(object):
148 __doc__
= _realsocket
.__doc
__
150 __slots__
= ["_sock", "send", "recv", "sendto", "recvfrom"]
152 def __init__(self
, family
=AF_INET
, type=SOCK_STREAM
, proto
=0, _sock
=None):
154 _sock
= _realsocket(family
, type, proto
)
156 self
.send
= self
._sock
.send
157 self
.recv
= self
._sock
.recv
158 self
.sendto
= self
._sock
.sendto
159 self
.recvfrom
= self
._sock
.recvfrom
162 self
._sock
= _closedsocket()
163 self
.send
= self
.recv
= self
.sendto
= self
.recvfrom
= self
._sock
._dummy
164 close
.__doc
__ = _realsocket
.close
.__doc
__
167 sock
, addr
= self
._sock
.accept()
168 return _socketobject(_sock
=sock
), addr
169 accept
.__doc
__ = _realsocket
.accept
.__doc
__
172 """dup() -> socket object
174 Return a new socket object connected to the same system resource."""
175 return _socketobject(_sock
=self
._sock
)
177 def makefile(self
, mode
='r', bufsize
=-1):
178 """makefile([mode[, bufsize]]) -> file object
180 Return a regular file object corresponding to the socket. The mode
181 and bufsize arguments are as for the built-in open() function."""
182 return _fileobject(self
._sock
, mode
, bufsize
)
184 _s
= ("def %s(self, *args): return self._sock.%s(*args)\n\n"
185 "%s.__doc__ = _realsocket.%s.__doc__\n")
186 for _m
in _socketmethods
:
187 exec _s
% (_m
, _m
, _m
, _m
)
190 socket
= SocketType
= _socketobject
192 class _fileobject(object):
193 """Faux file object attached to a socket object."""
195 default_bufsize
= 8192
198 __slots__
= ["mode", "bufsize", "softspace",
199 # "closed" is a property, see below
200 "_sock", "_rbufsize", "_wbufsize", "_rbuf", "_wbuf"]
202 def __init__(self
, sock
, mode
='rb', bufsize
=-1):
204 self
.mode
= mode
# Not actually used in this version
206 bufsize
= self
.default_bufsize
207 self
.bufsize
= bufsize
208 self
.softspace
= False
212 self
._rbufsize
= self
.default_bufsize
214 self
._rbufsize
= bufsize
215 self
._wbufsize
= bufsize
216 self
._rbuf
= "" # A string
217 self
._wbuf
= [] # A list of strings
219 def _getclosed(self
):
220 return self
._sock
is not None
221 closed
= property(_getclosed
, doc
="True if the file is closed")
234 # close() may fail if __init__ didn't complete
239 buffer = "".join(self
._wbuf
)
241 self
._sock
.sendall(buffer)
244 return self
._sock
.fileno()
246 def write(self
, data
):
247 data
= str(data
) # XXX Should really reject non-string non-buffers
250 self
._wbuf
.append(data
)
251 if (self
._wbufsize
== 0 or
252 self
._wbufsize
== 1 and '\n' in data
or
253 self
._get
_wbuf
_len
() >= self
._wbufsize
):
256 def writelines(self
, list):
257 # XXX We could do better here for very long lists
258 # XXX Should really reject non-string non-buffers
259 self
._wbuf
.extend(filter(None, map(str, list)))
260 if (self
._wbufsize
<= 1 or
261 self
._get
_wbuf
_len
() >= self
._wbufsize
):
264 def _get_wbuf_len(self
):
270 def read(self
, size
=-1):
278 if self
._rbufsize
<= 1:
279 recv_size
= self
.default_bufsize
281 recv_size
= self
._rbufsize
283 data
= self
._sock
.recv(recv_size
)
287 return "".join(buffers
)
289 # Read until size bytes or EOF seen, whichever comes first
292 self
._rbuf
= data
[size
:]
299 left
= size
- buf_len
300 recv_size
= max(self
._rbufsize
, left
)
301 data
= self
._sock
.recv(recv_size
)
307 self
._rbuf
= data
[left
:]
308 buffers
[-1] = data
[:left
]
311 return "".join(buffers
)
313 def readline(self
, size
=-1):
316 # Read until \n or EOF, whichever comes first
317 if self
._rbufsize
<= 1:
318 # Speed up unbuffered case
321 recv
= self
._sock
.recv
327 return "".join(buffers
)
331 self
._rbuf
= data
[nl
:]
338 data
= self
._sock
.recv(self
._rbufsize
)
345 self
._rbuf
= data
[nl
:]
346 buffers
[-1] = data
[:nl
]
348 return "".join(buffers
)
350 # Read until size bytes or \n or EOF seen, whichever comes first
351 nl
= data
.find('\n', 0, size
)
354 self
._rbuf
= data
[nl
:]
358 self
._rbuf
= data
[size
:]
365 data
= self
._sock
.recv(self
._rbufsize
)
369 left
= size
- buf_len
370 nl
= data
.find('\n', 0, left
)
373 self
._rbuf
= data
[nl
:]
374 buffers
[-1] = data
[:nl
]
378 self
._rbuf
= data
[left
:]
379 buffers
[-1] = data
[:left
]
382 return "".join(buffers
)
384 def readlines(self
, sizehint
=0):
388 line
= self
.readline()
393 if sizehint
and total
>= sizehint
:
403 line
= self
.readline()