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() -- map 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 and
28 optional source address.
30 [*] not available on all platforms!
34 SocketType -- type object for socket objects
35 error -- exception raised for I/O errors
36 has_ipv6 -- boolean value indicating if IPv6 is supported
40 AF_INET, AF_UNIX -- socket domains (first argument to socket() call)
41 SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument)
43 Many other constants may be defined; these may be used in calls to
44 the setsockopt() and getsockopt() methods.
49 from functools
import partial
50 from types
import MethodType
58 def ssl(sock
, keyfile
=None, certfile
=None):
59 # we do an internal import here because the ssl
60 # module imports the socket module
61 import ssl
as _realssl
62 warnings
.warn("socket.ssl() is deprecated. Use ssl.wrap_socket() instead.",
63 DeprecationWarning, stacklevel
=2)
64 return _realssl
.sslwrap_simple(sock
, keyfile
, certfile
)
66 # we need to import the same constants we used to...
67 from _ssl
import SSLError
as sslerror
72 SSL_ERROR_ZERO_RETURN
, \
73 SSL_ERROR_WANT_READ
, \
74 SSL_ERROR_WANT_WRITE
, \
75 SSL_ERROR_WANT_X509_LOOKUP
, \
78 SSL_ERROR_WANT_CONNECT
, \
80 SSL_ERROR_INVALID_ERROR_CODE
82 import os
, sys
, warnings
85 from cStringIO
import StringIO
87 from StringIO
import StringIO
93 EBADF
= getattr(errno
, 'EBADF', 9)
94 EINTR
= getattr(errno
, 'EINTR', 4)
96 __all__
= ["getfqdn", "create_connection"]
97 __all__
.extend(os
._get
_exports
_list
(_socket
))
103 if sys
.platform
.lower().startswith("win"):
105 errorTab
[10004] = "The operation was interrupted."
106 errorTab
[10009] = "A bad file handle was passed."
107 errorTab
[10013] = "Permission denied."
108 errorTab
[10014] = "A fault occurred on the network??" # WSAEFAULT
109 errorTab
[10022] = "An invalid operation was attempted."
110 errorTab
[10035] = "The socket operation would block"
111 errorTab
[10036] = "A blocking operation is already in progress."
112 errorTab
[10048] = "The network address is in use."
113 errorTab
[10054] = "The connection has been reset."
114 errorTab
[10058] = "The network has been shut down."
115 errorTab
[10060] = "The operation timed out."
116 errorTab
[10061] = "Connection refused."
117 errorTab
[10063] = "The name is too long."
118 errorTab
[10064] = "The host is down."
119 errorTab
[10065] = "The host is unreachable."
120 __all__
.append("errorTab")
124 def getfqdn(name
=''):
125 """Get fully qualified domain name from name.
127 An empty argument is interpreted as meaning the local host.
129 First the hostname returned by gethostbyaddr() is checked, then
130 possibly existing aliases. In case no FQDN is available, hostname
131 from gethostname() is returned.
134 if not name
or name
== '0.0.0.0':
137 hostname
, aliases
, ipaddrs
= gethostbyaddr(name
)
141 aliases
.insert(0, hostname
)
151 'bind', 'connect', 'connect_ex', 'fileno', 'listen',
152 'getpeername', 'getsockname', 'getsockopt', 'setsockopt',
153 'sendall', 'setblocking',
154 'settimeout', 'gettimeout', 'shutdown')
157 _socketmethods
= _socketmethods
+ ('ioctl',)
159 if sys
.platform
== "riscos":
160 _socketmethods
= _socketmethods
+ ('sleeptaskw',)
162 # All the method names that must be delegated to either the real socket
163 # object or the _closedsocket object.
164 _delegate_methods
= ("recv", "recvfrom", "recv_into", "recvfrom_into",
167 class _closedsocket(object):
170 raise error(EBADF
, 'Bad file descriptor')
171 # All _delegate_methods must also be initialized here.
172 send
= recv
= recv_into
= sendto
= recvfrom
= recvfrom_into
= _dummy
175 # Wrapper around platform socket objects. This implements
176 # a platform-independent dup() functionality. The
177 # implementation currently relies on reference counting
178 # to close the underlying socket object.
179 class _socketobject(object):
181 __doc__
= _realsocket
.__doc
__
183 __slots__
= ["_sock", "__weakref__"] + list(_delegate_methods
)
185 def __init__(self
, family
=AF_INET
, type=SOCK_STREAM
, proto
=0, _sock
=None):
187 _sock
= _realsocket(family
, type, proto
)
189 for method
in _delegate_methods
:
190 setattr(self
, method
, getattr(_sock
, method
))
193 self
._sock
= _closedsocket()
194 dummy
= self
._sock
._dummy
195 for method
in _delegate_methods
:
196 setattr(self
, method
, dummy
)
197 close
.__doc
__ = _realsocket
.close
.__doc
__
200 sock
, addr
= self
._sock
.accept()
201 return _socketobject(_sock
=sock
), addr
202 accept
.__doc
__ = _realsocket
.accept
.__doc
__
205 """dup() -> socket object
207 Return a new socket object connected to the same system resource."""
208 return _socketobject(_sock
=self
._sock
)
210 def makefile(self
, mode
='r', bufsize
=-1):
211 """makefile([mode[, bufsize]]) -> file object
213 Return a regular file object corresponding to the socket. The mode
214 and bufsize arguments are as for the built-in open() function."""
215 return _fileobject(self
._sock
, mode
, bufsize
)
217 family
= property(lambda self
: self
._sock
.family
, doc
="the socket family")
218 type = property(lambda self
: self
._sock
.type, doc
="the socket type")
219 proto
= property(lambda self
: self
._sock
.proto
, doc
="the socket protocol")
221 def meth(name
,self
,*args
):
222 return getattr(self
._sock
,name
)(*args
)
224 for _m
in _socketmethods
:
227 p
.__doc
__ = getattr(_realsocket
,_m
).__doc
__
228 m
= MethodType(p
,None,_socketobject
)
229 setattr(_socketobject
,_m
,m
)
231 socket
= SocketType
= _socketobject
233 class _fileobject(object):
234 """Faux file object attached to a socket object."""
236 default_bufsize
= 8192
239 __slots__
= ["mode", "bufsize", "softspace",
240 # "closed" is a property, see below
241 "_sock", "_rbufsize", "_wbufsize", "_rbuf", "_wbuf", "_wbuf_len",
244 def __init__(self
, sock
, mode
='rb', bufsize
=-1, close
=False):
246 self
.mode
= mode
# Not actually used in this version
248 bufsize
= self
.default_bufsize
249 self
.bufsize
= bufsize
250 self
.softspace
= False
251 # _rbufsize is the suggested recv buffer size. It is *strictly*
252 # obeyed within readline() for recv calls. If it is larger than
253 # default_bufsize it will be used for recv calls within read().
257 self
._rbufsize
= self
.default_bufsize
259 self
._rbufsize
= bufsize
260 self
._wbufsize
= bufsize
261 # We use StringIO for the read buffer to avoid holding a list
262 # of variously sized string objects which have been known to
263 # fragment the heap due to how they are malloc()ed and often
264 # realloc()ed down much smaller than their original allocation.
265 self
._rbuf
= StringIO()
266 self
._wbuf
= [] # A list of strings
270 def _getclosed(self
):
271 return self
._sock
is None
272 closed
= property(_getclosed
, doc
="True if the file is closed")
287 # close() may fail if __init__ didn't complete
292 data
= "".join(self
._wbuf
)
295 buffer_size
= max(self
._rbufsize
, self
.default_bufsize
)
296 data_size
= len(data
)
298 view
= memoryview(data
)
300 while write_offset
< data_size
:
301 self
._sock
.sendall(view
[write_offset
:write_offset
+buffer_size
])
302 write_offset
+= buffer_size
304 if write_offset
< data_size
:
305 remainder
= data
[write_offset
:]
306 del view
, data
# explicit free
307 self
._wbuf
.append(remainder
)
308 self
._wbuf
_len
= len(remainder
)
311 return self
._sock
.fileno()
313 def write(self
, data
):
314 data
= str(data
) # XXX Should really reject non-string non-buffers
317 self
._wbuf
.append(data
)
318 self
._wbuf
_len
+= len(data
)
319 if (self
._wbufsize
== 0 or
320 self
._wbufsize
== 1 and '\n' in data
or
321 self
._wbuf
_len
>= self
._wbufsize
):
324 def writelines(self
, list):
325 # XXX We could do better here for very long lists
326 # XXX Should really reject non-string non-buffers
327 lines
= filter(None, map(str, list))
328 self
._wbuf
_len
+= sum(map(len, lines
))
329 self
._wbuf
.extend(lines
)
330 if (self
._wbufsize
<= 1 or
331 self
._wbuf
_len
>= self
._wbufsize
):
334 def read(self
, size
=-1):
335 # Use max, disallow tiny reads in a loop as they are very inefficient.
336 # We never leave read() with any leftover data from a new recv() call
337 # in our internal buffer.
338 rbufsize
= max(self
._rbufsize
, self
.default_bufsize
)
339 # Our use of StringIO rather than lists of string objects returned by
340 # recv() minimizes memory usage and fragmentation that occurs when
341 # rbufsize is large compared to the typical return value of recv().
343 buf
.seek(0, 2) # seek end
346 self
._rbuf
= StringIO() # reset _rbuf. we consume it via buf.
349 data
= self
._sock
.recv(rbufsize
)
351 if e
.args
[0] == EINTR
:
357 return buf
.getvalue()
359 # Read until size bytes or EOF seen, whichever comes first
362 # Already have size bytes in our buffer? Extract and return.
365 self
._rbuf
= StringIO()
366 self
._rbuf
.write(buf
.read())
369 self
._rbuf
= StringIO() # reset _rbuf. we consume it via buf.
371 left
= size
- buf_len
372 # recv() will malloc the amount of memory given as its
373 # parameter even though it often returns much less data
374 # than that. The returned data string is short lived
375 # as we copy it into a StringIO and free it. This avoids
376 # fragmentation issues on many platforms.
378 data
= self
._sock
.recv(left
)
380 if e
.args
[0] == EINTR
:
386 if n
== size
and not buf_len
:
387 # Shortcut. Avoid buffer data copies when:
388 # - We have no data in our buffer.
390 # - Our call to recv returned exactly the
391 # number of bytes we were asked to read.
395 del data
# explicit free
397 assert n
<= left
, "recv(%d) returned %d bytes" % (left
, n
)
400 del data
# explicit free
401 #assert buf_len == buf.tell()
402 return buf
.getvalue()
404 def readline(self
, size
=-1):
406 buf
.seek(0, 2) # seek end
408 # check if we already have it in our buffer
410 bline
= buf
.readline(size
)
411 if bline
.endswith('\n') or len(bline
) == size
:
412 self
._rbuf
= StringIO()
413 self
._rbuf
.write(buf
.read())
417 # Read until \n or EOF, whichever comes first
418 if self
._rbufsize
<= 1:
419 # Speed up unbuffered case
421 buffers
= [buf
.read()]
422 self
._rbuf
= StringIO() # reset _rbuf. we consume it via buf.
424 recv
= self
._sock
.recv
433 # The try..except to catch EINTR was moved outside the
434 # recv loop to avoid the per byte overhead.
435 if e
.args
[0] == EINTR
:
439 return "".join(buffers
)
441 buf
.seek(0, 2) # seek end
442 self
._rbuf
= StringIO() # reset _rbuf. we consume it via buf.
445 data
= self
._sock
.recv(self
._rbufsize
)
447 if e
.args
[0] == EINTR
:
456 self
._rbuf
.write(data
[nl
:])
460 return buf
.getvalue()
462 # Read until size bytes or \n or EOF seen, whichever comes first
463 buf
.seek(0, 2) # seek end
468 self
._rbuf
= StringIO()
469 self
._rbuf
.write(buf
.read())
471 self
._rbuf
= StringIO() # reset _rbuf. we consume it via buf.
474 data
= self
._sock
.recv(self
._rbufsize
)
476 if e
.args
[0] == EINTR
:
481 left
= size
- buf_len
482 # did we just receive a newline?
483 nl
= data
.find('\n', 0, left
)
486 # save the excess data to _rbuf
487 self
._rbuf
.write(data
[nl
:])
492 # Shortcut. Avoid data copy through buf when returning
493 # a substring of our first recv().
496 if n
== size
and not buf_len
:
497 # Shortcut. Avoid data copy through buf when
498 # returning exactly all of our first recv().
501 buf
.write(data
[:left
])
502 self
._rbuf
.write(data
[left
:])
506 #assert buf_len == buf.tell()
507 return buf
.getvalue()
509 def readlines(self
, sizehint
=0):
513 line
= self
.readline()
518 if sizehint
and total
>= sizehint
:
528 line
= self
.readline()
533 _GLOBAL_DEFAULT_TIMEOUT
= object()
535 def create_connection(address
, timeout
=_GLOBAL_DEFAULT_TIMEOUT
,
536 source_address
=None):
537 """Connect to *address* and return the socket object.
539 Convenience function. Connect to *address* (a 2-tuple ``(host,
540 port)``) and return the socket object. Passing the optional
541 *timeout* parameter will set the timeout on the socket instance
542 before attempting to connect. If no *timeout* is supplied, the
543 global default timeout setting returned by :func:`getdefaulttimeout`
544 is used. If *source_address* is set it must be a tuple of (host, port)
545 for the socket to bind as a source address before making the connection.
546 An host of '' or port 0 tells the OS to use the default.
549 msg
= "getaddrinfo returns an empty list"
551 for res
in getaddrinfo(host
, port
, 0, SOCK_STREAM
):
552 af
, socktype
, proto
, canonname
, sa
= res
555 sock
= socket(af
, socktype
, proto
)
556 if timeout
is not _GLOBAL_DEFAULT_TIMEOUT
:
557 sock
.settimeout(timeout
)
559 sock
.bind(source_address
)