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 socketpair() -- create a pair of new socket objects [*]
14 fromfd() -- create a socket object from an open file descriptor [*]
15 gethostname() -- return the current hostname
16 gethostbyname() -- map a hostname to its IP number
17 gethostbyaddr() -- map an IP number or hostname to DNS info
18 getservbyname() -- map a service name and a protocol name to a port number
19 getprotobyname() -- mape a protocol name (e.g. 'tcp') to a number
20 ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order
21 htons(), htonl() -- convert 16, 32 bit int from host to network byte order
22 inet_aton() -- convert IP addr string (123.45.67.89) to 32-bit packed format
23 inet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89)
24 ssl() -- secure socket layer support (only available if configured)
25 socket.getdefaulttimeout() -- get the default timeout value
26 socket.setdefaulttimeout() -- set the default timeout value
27 create_connection() -- connects to an address, with an optional timeout
29 [*] not available on all platforms!
33 SocketType -- type object for socket objects
34 error -- exception raised for I/O errors
35 has_ipv6 -- boolean value indicating if IPv6 is supported
39 AF_INET, AF_UNIX -- socket domains (first argument to socket() call)
40 SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument)
42 Many other constants may be defined; these may be used in calls to
43 the setsockopt() and getsockopt() methods.
55 def ssl(sock
, keyfile
=None, certfile
=None):
56 # we do an internal import here because the ssl
57 # module imports the socket module
58 import ssl
as _realssl
59 warnings
.warn("socket.ssl() is deprecated. Use ssl.wrap_socket() instead.",
60 DeprecationWarning, stacklevel
=2)
61 return _realssl
.sslwrap_simple(sock
, keyfile
, certfile
)
63 # we need to import the same constants we used to...
64 from _ssl
import SSLError
as sslerror
69 SSL_ERROR_ZERO_RETURN
, \
70 SSL_ERROR_WANT_READ
, \
71 SSL_ERROR_WANT_WRITE
, \
72 SSL_ERROR_WANT_X509_LOOKUP
, \
75 SSL_ERROR_WANT_CONNECT
, \
77 SSL_ERROR_INVALID_ERROR_CODE
79 import os
, sys
, warnings
82 from cStringIO
import StringIO
84 from StringIO
import StringIO
87 from errno
import EBADF
92 __all__
.extend(os
._get
_exports
_list
(_socket
))
98 if sys
.platform
.lower().startswith("win"):
100 errorTab
[10004] = "The operation was interrupted."
101 errorTab
[10009] = "A bad file handle was passed."
102 errorTab
[10013] = "Permission denied."
103 errorTab
[10014] = "A fault occurred on the network??" # WSAEFAULT
104 errorTab
[10022] = "An invalid operation was attempted."
105 errorTab
[10035] = "The socket operation would block"
106 errorTab
[10036] = "A blocking operation is already in progress."
107 errorTab
[10048] = "The network address is in use."
108 errorTab
[10054] = "The connection has been reset."
109 errorTab
[10058] = "The network has been shut down."
110 errorTab
[10060] = "The operation timed out."
111 errorTab
[10061] = "Connection refused."
112 errorTab
[10063] = "The name is too long."
113 errorTab
[10064] = "The host is down."
114 errorTab
[10065] = "The host is unreachable."
115 __all__
.append("errorTab")
119 def getfqdn(name
=''):
120 """Get fully qualified domain name from name.
122 An empty argument is interpreted as meaning the local host.
124 First the hostname returned by gethostbyaddr() is checked, then
125 possibly existing aliases. In case no FQDN is available, hostname
126 from gethostname() is returned.
129 if not name
or name
== '0.0.0.0':
132 hostname
, aliases
, ipaddrs
= gethostbyaddr(name
)
136 aliases
.insert(0, hostname
)
146 'bind', 'connect', 'connect_ex', 'fileno', 'listen',
147 'getpeername', 'getsockname', 'getsockopt', 'setsockopt',
148 'sendall', 'setblocking',
149 'settimeout', 'gettimeout', 'shutdown')
152 _socketmethods
= _socketmethods
+ ('ioctl',)
154 if sys
.platform
== "riscos":
155 _socketmethods
= _socketmethods
+ ('sleeptaskw',)
157 # All the method names that must be delegated to either the real socket
158 # object or the _closedsocket object.
159 _delegate_methods
= ("recv", "recvfrom", "recv_into", "recvfrom_into",
162 class _closedsocket(object):
165 raise error(EBADF
, 'Bad file descriptor')
166 # All _delegate_methods must also be initialized here.
167 send
= recv
= recv_into
= sendto
= recvfrom
= recvfrom_into
= _dummy
170 # Wrapper around platform socket objects. This implements
171 # a platform-independent dup() functionality. The
172 # implementation currently relies on reference counting
173 # to close the underlying socket object.
174 class _socketobject(object):
176 __doc__
= _realsocket
.__doc
__
178 __slots__
= ["_sock", "__weakref__"] + list(_delegate_methods
)
180 def __init__(self
, family
=AF_INET
, type=SOCK_STREAM
, proto
=0, _sock
=None):
182 _sock
= _realsocket(family
, type, proto
)
184 for method
in _delegate_methods
:
185 setattr(self
, method
, getattr(_sock
, method
))
188 self
._sock
= _closedsocket()
189 dummy
= self
._sock
._dummy
190 for method
in _delegate_methods
:
191 setattr(self
, method
, dummy
)
192 close
.__doc
__ = _realsocket
.close
.__doc
__
195 sock
, addr
= self
._sock
.accept()
196 return _socketobject(_sock
=sock
), addr
197 accept
.__doc
__ = _realsocket
.accept
.__doc
__
200 """dup() -> socket object
202 Return a new socket object connected to the same system resource."""
203 return _socketobject(_sock
=self
._sock
)
205 def makefile(self
, mode
='r', bufsize
=-1):
206 """makefile([mode[, bufsize]]) -> file object
208 Return a regular file object corresponding to the socket. The mode
209 and bufsize arguments are as for the built-in open() function."""
210 return _fileobject(self
._sock
, mode
, bufsize
)
212 family
= property(lambda self
: self
._sock
.family
, doc
="the socket family")
213 type = property(lambda self
: self
._sock
.type, doc
="the socket type")
214 proto
= property(lambda self
: self
._sock
.proto
, doc
="the socket protocol")
216 _s
= ("def %s(self, *args): return self._sock.%s(*args)\n\n"
217 "%s.__doc__ = _realsocket.%s.__doc__\n")
218 for _m
in _socketmethods
:
219 exec _s
% (_m
, _m
, _m
, _m
)
222 socket
= SocketType
= _socketobject
224 class _fileobject(object):
225 """Faux file object attached to a socket object."""
227 default_bufsize
= 8192
230 __slots__
= ["mode", "bufsize", "softspace",
231 # "closed" is a property, see below
232 "_sock", "_rbufsize", "_wbufsize", "_rbuf", "_wbuf",
235 def __init__(self
, sock
, mode
='rb', bufsize
=-1, close
=False):
237 self
.mode
= mode
# Not actually used in this version
239 bufsize
= self
.default_bufsize
240 self
.bufsize
= bufsize
241 self
.softspace
= False
242 # _rbufsize is the suggested recv buffer size. It is *strictly*
243 # obeyed within readline() for recv calls. If it is larger than
244 # default_bufsize it will be used for recv calls within read().
248 self
._rbufsize
= self
.default_bufsize
250 self
._rbufsize
= bufsize
251 self
._wbufsize
= bufsize
252 # We use StringIO for the read buffer to avoid holding a list
253 # of variously sized string objects which have been known to
254 # fragment the heap due to how they are malloc()ed and often
255 # realloc()ed down much smaller than their original allocation.
256 self
._rbuf
= StringIO()
257 self
._wbuf
= [] # A list of strings
260 def _getclosed(self
):
261 return self
._sock
is None
262 closed
= property(_getclosed
, doc
="True if the file is closed")
277 # close() may fail if __init__ didn't complete
282 buffer = "".join(self
._wbuf
)
284 self
._sock
.sendall(buffer)
287 return self
._sock
.fileno()
289 def write(self
, data
):
290 data
= str(data
) # XXX Should really reject non-string non-buffers
293 self
._wbuf
.append(data
)
294 if (self
._wbufsize
== 0 or
295 self
._wbufsize
== 1 and '\n' in data
or
296 self
._get
_wbuf
_len
() >= self
._wbufsize
):
299 def writelines(self
, list):
300 # XXX We could do better here for very long lists
301 # XXX Should really reject non-string non-buffers
302 self
._wbuf
.extend(filter(None, map(str, list)))
303 if (self
._wbufsize
<= 1 or
304 self
._get
_wbuf
_len
() >= self
._wbufsize
):
307 def _get_wbuf_len(self
):
313 def read(self
, size
=-1):
314 # Use max, disallow tiny reads in a loop as they are very inefficient.
315 # We never leave read() with any leftover data from a new recv() call
316 # in our internal buffer.
317 rbufsize
= max(self
._rbufsize
, self
.default_bufsize
)
318 # Our use of StringIO rather than lists of string objects returned by
319 # recv() minimizes memory usage and fragmentation that occurs when
320 # rbufsize is large compared to the typical return value of recv().
322 buf
.seek(0, 2) # seek end
325 self
._rbuf
= StringIO() # reset _rbuf. we consume it via buf.
327 data
= self
._sock
.recv(rbufsize
)
331 return buf
.getvalue()
333 # Read until size bytes or EOF seen, whichever comes first
336 # Already have size bytes in our buffer? Extract and return.
339 self
._rbuf
= StringIO()
340 self
._rbuf
.write(buf
.read())
343 self
._rbuf
= StringIO() # reset _rbuf. we consume it via buf.
345 left
= size
- buf_len
346 # recv() will malloc the amount of memory given as its
347 # parameter even though it often returns much less data
348 # than that. The returned data string is short lived
349 # as we copy it into a StringIO and free it. This avoids
350 # fragmentation issues on many platforms.
351 data
= self
._sock
.recv(left
)
355 if n
== size
and not buf_len
:
356 # Shortcut. Avoid buffer data copies when:
357 # - We have no data in our buffer.
359 # - Our call to recv returned exactly the
360 # number of bytes we were asked to read.
364 del data
# explicit free
366 assert n
<= left
, "recv(%d) returned %d bytes" % (left
, n
)
369 del data
# explicit free
370 #assert buf_len == buf.tell()
371 return buf
.getvalue()
373 def readline(self
, size
=-1):
375 buf
.seek(0, 2) # seek end
377 # check if we already have it in our buffer
379 bline
= buf
.readline(size
)
380 if bline
.endswith('\n') or len(bline
) == size
:
381 self
._rbuf
= StringIO()
382 self
._rbuf
.write(buf
.read())
386 # Read until \n or EOF, whichever comes first
387 if self
._rbufsize
<= 1:
388 # Speed up unbuffered case
390 buffers
= [buf
.read()]
391 self
._rbuf
= StringIO() # reset _rbuf. we consume it via buf.
393 recv
= self
._sock
.recv
399 return "".join(buffers
)
401 buf
.seek(0, 2) # seek end
402 self
._rbuf
= StringIO() # reset _rbuf. we consume it via buf.
404 data
= self
._sock
.recv(self
._rbufsize
)
411 self
._rbuf
.write(data
[nl
:])
415 return buf
.getvalue()
417 # Read until size bytes or \n or EOF seen, whichever comes first
418 buf
.seek(0, 2) # seek end
423 self
._rbuf
= StringIO()
424 self
._rbuf
.write(buf
.read())
426 self
._rbuf
= StringIO() # reset _rbuf. we consume it via buf.
428 data
= self
._sock
.recv(self
._rbufsize
)
431 left
= size
- buf_len
432 # did we just receive a newline?
433 nl
= data
.find('\n', 0, left
)
436 # save the excess data to _rbuf
437 self
._rbuf
.write(data
[nl
:])
442 # Shortcut. Avoid data copy through buf when returning
443 # a substring of our first recv().
446 if n
== size
and not buf_len
:
447 # Shortcut. Avoid data copy through buf when
448 # returning exactly all of our first recv().
451 buf
.write(data
[:left
])
452 self
._rbuf
.write(data
[left
:])
456 #assert buf_len == buf.tell()
457 return buf
.getvalue()
459 def readlines(self
, sizehint
=0):
463 line
= self
.readline()
468 if sizehint
and total
>= sizehint
:
478 line
= self
.readline()
483 _GLOBAL_DEFAULT_TIMEOUT
= object()
485 def create_connection(address
, timeout
=_GLOBAL_DEFAULT_TIMEOUT
):
486 """Connect to *address* and return the socket object.
488 Convenience function. Connect to *address* (a 2-tuple ``(host,
489 port)``) and return the socket object. Passing the optional
490 *timeout* parameter will set the timeout on the socket instance
491 before attempting to connect. If no *timeout* is supplied, the
492 global default timeout setting returned by :func:`getdefaulttimeout`
496 msg
= "getaddrinfo returns an empty list"
498 for res
in getaddrinfo(host
, port
, 0, SOCK_STREAM
):
499 af
, socktype
, proto
, canonname
, sa
= res
502 sock
= socket(af
, socktype
, proto
)
503 if timeout
is not _GLOBAL_DEFAULT_TIMEOUT
:
504 sock
.settimeout(timeout
)