/lib-python/2.7/socket.py

https://bitbucket.org/dac_io/pypy · Python · 573 lines · 496 code · 15 blank · 62 comment · 60 complexity · a0df601d30dc9c382e0c8d8e022eeeec MD5 · raw file

  1. # Wrapper module for _socket, providing some additional facilities
  2. # implemented in Python.
  3. """\
  4. This module provides socket operations and some related functions.
  5. On Unix, it supports IP (Internet Protocol) and Unix domain sockets.
  6. On other systems, it only supports IP. Functions specific for a
  7. socket are available as methods of the socket object.
  8. Functions:
  9. socket() -- create a new socket object
  10. socketpair() -- create a pair of new socket objects [*]
  11. fromfd() -- create a socket object from an open file descriptor [*]
  12. gethostname() -- return the current hostname
  13. gethostbyname() -- map a hostname to its IP number
  14. gethostbyaddr() -- map an IP number or hostname to DNS info
  15. getservbyname() -- map a service name and a protocol name to a port number
  16. getprotobyname() -- map a protocol name (e.g. 'tcp') to a number
  17. ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order
  18. htons(), htonl() -- convert 16, 32 bit int from host to network byte order
  19. inet_aton() -- convert IP addr string (123.45.67.89) to 32-bit packed format
  20. inet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89)
  21. ssl() -- secure socket layer support (only available if configured)
  22. socket.getdefaulttimeout() -- get the default timeout value
  23. socket.setdefaulttimeout() -- set the default timeout value
  24. create_connection() -- connects to an address, with an optional timeout and
  25. optional source address.
  26. [*] not available on all platforms!
  27. Special objects:
  28. SocketType -- type object for socket objects
  29. error -- exception raised for I/O errors
  30. has_ipv6 -- boolean value indicating if IPv6 is supported
  31. Integer constants:
  32. AF_INET, AF_UNIX -- socket domains (first argument to socket() call)
  33. SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument)
  34. Many other constants may be defined; these may be used in calls to
  35. the setsockopt() and getsockopt() methods.
  36. """
  37. import _socket
  38. from _socket import *
  39. from functools import partial
  40. from types import MethodType
  41. try:
  42. import _ssl
  43. except ImportError:
  44. # no SSL support
  45. pass
  46. else:
  47. def ssl(sock, keyfile=None, certfile=None):
  48. # we do an internal import here because the ssl
  49. # module imports the socket module
  50. import ssl as _realssl
  51. warnings.warn("socket.ssl() is deprecated. Use ssl.wrap_socket() instead.",
  52. DeprecationWarning, stacklevel=2)
  53. return _realssl.sslwrap_simple(sock, keyfile, certfile)
  54. # we need to import the same constants we used to...
  55. from _ssl import SSLError as sslerror
  56. from _ssl import \
  57. RAND_add, \
  58. RAND_egd, \
  59. RAND_status, \
  60. SSL_ERROR_ZERO_RETURN, \
  61. SSL_ERROR_WANT_READ, \
  62. SSL_ERROR_WANT_WRITE, \
  63. SSL_ERROR_WANT_X509_LOOKUP, \
  64. SSL_ERROR_SYSCALL, \
  65. SSL_ERROR_SSL, \
  66. SSL_ERROR_WANT_CONNECT, \
  67. SSL_ERROR_EOF, \
  68. SSL_ERROR_INVALID_ERROR_CODE
  69. import os, sys, warnings
  70. try:
  71. from cStringIO import StringIO
  72. except ImportError:
  73. from StringIO import StringIO
  74. try:
  75. import errno
  76. except ImportError:
  77. errno = None
  78. EBADF = getattr(errno, 'EBADF', 9)
  79. EINTR = getattr(errno, 'EINTR', 4)
  80. __all__ = ["getfqdn", "create_connection"]
  81. __all__.extend(os._get_exports_list(_socket))
  82. _realsocket = socket
  83. # WSA error codes
  84. if sys.platform.lower().startswith("win"):
  85. errorTab = {}
  86. errorTab[10004] = "The operation was interrupted."
  87. errorTab[10009] = "A bad file handle was passed."
  88. errorTab[10013] = "Permission denied."
  89. errorTab[10014] = "A fault occurred on the network??" # WSAEFAULT
  90. errorTab[10022] = "An invalid operation was attempted."
  91. errorTab[10035] = "The socket operation would block"
  92. errorTab[10036] = "A blocking operation is already in progress."
  93. errorTab[10048] = "The network address is in use."
  94. errorTab[10054] = "The connection has been reset."
  95. errorTab[10058] = "The network has been shut down."
  96. errorTab[10060] = "The operation timed out."
  97. errorTab[10061] = "Connection refused."
  98. errorTab[10063] = "The name is too long."
  99. errorTab[10064] = "The host is down."
  100. errorTab[10065] = "The host is unreachable."
  101. __all__.append("errorTab")
  102. def getfqdn(name=''):
  103. """Get fully qualified domain name from name.
  104. An empty argument is interpreted as meaning the local host.
  105. First the hostname returned by gethostbyaddr() is checked, then
  106. possibly existing aliases. In case no FQDN is available, hostname
  107. from gethostname() is returned.
  108. """
  109. name = name.strip()
  110. if not name or name == '0.0.0.0':
  111. name = gethostname()
  112. try:
  113. hostname, aliases, ipaddrs = gethostbyaddr(name)
  114. except error:
  115. pass
  116. else:
  117. aliases.insert(0, hostname)
  118. for name in aliases:
  119. if '.' in name:
  120. break
  121. else:
  122. name = hostname
  123. return name
  124. _socketmethods = (
  125. 'bind', 'connect', 'connect_ex', 'fileno', 'listen',
  126. 'getpeername', 'getsockname', 'getsockopt', 'setsockopt',
  127. 'sendall', 'setblocking',
  128. 'settimeout', 'gettimeout', 'shutdown')
  129. if os.name == "nt":
  130. _socketmethods = _socketmethods + ('ioctl',)
  131. if sys.platform == "riscos":
  132. _socketmethods = _socketmethods + ('sleeptaskw',)
  133. # All the method names that must be delegated to either the real socket
  134. # object or the _closedsocket object.
  135. _delegate_methods = ("recv", "recvfrom", "recv_into", "recvfrom_into",
  136. "send", "sendto")
  137. class _closedsocket(object):
  138. __slots__ = []
  139. def _dummy(*args):
  140. raise error(EBADF, 'Bad file descriptor')
  141. # All _delegate_methods must also be initialized here.
  142. send = recv = recv_into = sendto = recvfrom = recvfrom_into = _dummy
  143. __getattr__ = _dummy
  144. # Wrapper around platform socket objects. This implements
  145. # a platform-independent dup() functionality. The
  146. # implementation currently relies on reference counting
  147. # to close the underlying socket object.
  148. class _socketobject(object):
  149. __doc__ = _realsocket.__doc__
  150. __slots__ = ["_sock", "__weakref__"] + list(_delegate_methods)
  151. def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, _sock=None):
  152. if _sock is None:
  153. _sock = _realsocket(family, type, proto)
  154. self._sock = _sock
  155. for method in _delegate_methods:
  156. setattr(self, method, getattr(_sock, method))
  157. def close(self, _closedsocket=_closedsocket,
  158. _delegate_methods=_delegate_methods, setattr=setattr):
  159. # This function should not reference any globals. See issue #808164.
  160. self._sock = _closedsocket()
  161. dummy = self._sock._dummy
  162. for method in _delegate_methods:
  163. setattr(self, method, dummy)
  164. close.__doc__ = _realsocket.close.__doc__
  165. def accept(self):
  166. sock, addr = self._sock.accept()
  167. return _socketobject(_sock=sock), addr
  168. accept.__doc__ = _realsocket.accept.__doc__
  169. def dup(self):
  170. """dup() -> socket object
  171. Return a new socket object connected to the same system resource."""
  172. return _socketobject(_sock=self._sock)
  173. def makefile(self, mode='r', bufsize=-1):
  174. """makefile([mode[, bufsize]]) -> file object
  175. Return a regular file object corresponding to the socket. The mode
  176. and bufsize arguments are as for the built-in open() function."""
  177. return _fileobject(self._sock, mode, bufsize)
  178. family = property(lambda self: self._sock.family, doc="the socket family")
  179. type = property(lambda self: self._sock.type, doc="the socket type")
  180. proto = property(lambda self: self._sock.proto, doc="the socket protocol")
  181. def meth(name,self,*args):
  182. return getattr(self._sock,name)(*args)
  183. for _m in _socketmethods:
  184. p = partial(meth,_m)
  185. p.__name__ = _m
  186. p.__doc__ = getattr(_realsocket,_m).__doc__
  187. m = MethodType(p,None,_socketobject)
  188. setattr(_socketobject,_m,m)
  189. socket = SocketType = _socketobject
  190. class _fileobject(object):
  191. """Faux file object attached to a socket object."""
  192. default_bufsize = 8192
  193. name = "<socket>"
  194. __slots__ = ["mode", "bufsize", "softspace",
  195. # "closed" is a property, see below
  196. "_sock", "_rbufsize", "_wbufsize", "_rbuf", "_wbuf", "_wbuf_len",
  197. "_close"]
  198. def __init__(self, sock, mode='rb', bufsize=-1, close=False):
  199. self._sock = sock
  200. self.mode = mode # Not actually used in this version
  201. if bufsize < 0:
  202. bufsize = self.default_bufsize
  203. self.bufsize = bufsize
  204. self.softspace = False
  205. # _rbufsize is the suggested recv buffer size. It is *strictly*
  206. # obeyed within readline() for recv calls. If it is larger than
  207. # default_bufsize it will be used for recv calls within read().
  208. if bufsize == 0:
  209. self._rbufsize = 1
  210. elif bufsize == 1:
  211. self._rbufsize = self.default_bufsize
  212. else:
  213. self._rbufsize = bufsize
  214. self._wbufsize = bufsize
  215. # We use StringIO for the read buffer to avoid holding a list
  216. # of variously sized string objects which have been known to
  217. # fragment the heap due to how they are malloc()ed and often
  218. # realloc()ed down much smaller than their original allocation.
  219. self._rbuf = StringIO()
  220. self._wbuf = [] # A list of strings
  221. self._wbuf_len = 0
  222. self._close = close
  223. def _getclosed(self):
  224. return self._sock is None
  225. closed = property(_getclosed, doc="True if the file is closed")
  226. def close(self):
  227. try:
  228. if self._sock:
  229. self.flush()
  230. finally:
  231. if self._close:
  232. self._sock.close()
  233. self._sock = None
  234. def __del__(self):
  235. try:
  236. self.close()
  237. except:
  238. # close() may fail if __init__ didn't complete
  239. pass
  240. def flush(self):
  241. if self._wbuf:
  242. data = "".join(self._wbuf)
  243. self._wbuf = []
  244. self._wbuf_len = 0
  245. buffer_size = max(self._rbufsize, self.default_bufsize)
  246. data_size = len(data)
  247. write_offset = 0
  248. view = memoryview(data)
  249. try:
  250. while write_offset < data_size:
  251. self._sock.sendall(view[write_offset:write_offset+buffer_size])
  252. write_offset += buffer_size
  253. finally:
  254. if write_offset < data_size:
  255. remainder = data[write_offset:]
  256. del view, data # explicit free
  257. self._wbuf.append(remainder)
  258. self._wbuf_len = len(remainder)
  259. def fileno(self):
  260. return self._sock.fileno()
  261. def write(self, data):
  262. data = str(data) # XXX Should really reject non-string non-buffers
  263. if not data:
  264. return
  265. self._wbuf.append(data)
  266. self._wbuf_len += len(data)
  267. if (self._wbufsize == 0 or
  268. self._wbufsize == 1 and '\n' in data or
  269. self._wbuf_len >= self._wbufsize):
  270. self.flush()
  271. def writelines(self, list):
  272. # XXX We could do better here for very long lists
  273. # XXX Should really reject non-string non-buffers
  274. lines = filter(None, map(str, list))
  275. self._wbuf_len += sum(map(len, lines))
  276. self._wbuf.extend(lines)
  277. if (self._wbufsize <= 1 or
  278. self._wbuf_len >= self._wbufsize):
  279. self.flush()
  280. def read(self, size=-1):
  281. # Use max, disallow tiny reads in a loop as they are very inefficient.
  282. # We never leave read() with any leftover data from a new recv() call
  283. # in our internal buffer.
  284. rbufsize = max(self._rbufsize, self.default_bufsize)
  285. # Our use of StringIO rather than lists of string objects returned by
  286. # recv() minimizes memory usage and fragmentation that occurs when
  287. # rbufsize is large compared to the typical return value of recv().
  288. buf = self._rbuf
  289. buf.seek(0, 2) # seek end
  290. if size < 0:
  291. # Read until EOF
  292. self._rbuf = StringIO() # reset _rbuf. we consume it via buf.
  293. while True:
  294. try:
  295. data = self._sock.recv(rbufsize)
  296. except error, e:
  297. if e.args[0] == EINTR:
  298. continue
  299. raise
  300. if not data:
  301. break
  302. buf.write(data)
  303. return buf.getvalue()
  304. else:
  305. # Read until size bytes or EOF seen, whichever comes first
  306. buf_len = buf.tell()
  307. if buf_len >= size:
  308. # Already have size bytes in our buffer? Extract and return.
  309. buf.seek(0)
  310. rv = buf.read(size)
  311. self._rbuf = StringIO()
  312. self._rbuf.write(buf.read())
  313. return rv
  314. self._rbuf = StringIO() # reset _rbuf. we consume it via buf.
  315. while True:
  316. left = size - buf_len
  317. # recv() will malloc the amount of memory given as its
  318. # parameter even though it often returns much less data
  319. # than that. The returned data string is short lived
  320. # as we copy it into a StringIO and free it. This avoids
  321. # fragmentation issues on many platforms.
  322. try:
  323. data = self._sock.recv(left)
  324. except error, e:
  325. if e.args[0] == EINTR:
  326. continue
  327. raise
  328. if not data:
  329. break
  330. n = len(data)
  331. if n == size and not buf_len:
  332. # Shortcut. Avoid buffer data copies when:
  333. # - We have no data in our buffer.
  334. # AND
  335. # - Our call to recv returned exactly the
  336. # number of bytes we were asked to read.
  337. return data
  338. if n == left:
  339. buf.write(data)
  340. del data # explicit free
  341. break
  342. assert n <= left, "recv(%d) returned %d bytes" % (left, n)
  343. buf.write(data)
  344. buf_len += n
  345. del data # explicit free
  346. #assert buf_len == buf.tell()
  347. return buf.getvalue()
  348. def readline(self, size=-1):
  349. buf = self._rbuf
  350. buf.seek(0, 2) # seek end
  351. if buf.tell() > 0:
  352. # check if we already have it in our buffer
  353. buf.seek(0)
  354. bline = buf.readline(size)
  355. if bline.endswith('\n') or len(bline) == size:
  356. self._rbuf = StringIO()
  357. self._rbuf.write(buf.read())
  358. return bline
  359. del bline
  360. if size < 0:
  361. # Read until \n or EOF, whichever comes first
  362. if self._rbufsize <= 1:
  363. # Speed up unbuffered case
  364. buf.seek(0)
  365. buffers = [buf.read()]
  366. self._rbuf = StringIO() # reset _rbuf. we consume it via buf.
  367. data = None
  368. recv = self._sock.recv
  369. while True:
  370. try:
  371. while data != "\n":
  372. data = recv(1)
  373. if not data:
  374. break
  375. buffers.append(data)
  376. except error, e:
  377. # The try..except to catch EINTR was moved outside the
  378. # recv loop to avoid the per byte overhead.
  379. if e.args[0] == EINTR:
  380. continue
  381. raise
  382. break
  383. return "".join(buffers)
  384. buf.seek(0, 2) # seek end
  385. self._rbuf = StringIO() # reset _rbuf. we consume it via buf.
  386. while True:
  387. try:
  388. data = self._sock.recv(self._rbufsize)
  389. except error, e:
  390. if e.args[0] == EINTR:
  391. continue
  392. raise
  393. if not data:
  394. break
  395. nl = data.find('\n')
  396. if nl >= 0:
  397. nl += 1
  398. buf.write(data[:nl])
  399. self._rbuf.write(data[nl:])
  400. del data
  401. break
  402. buf.write(data)
  403. return buf.getvalue()
  404. else:
  405. # Read until size bytes or \n or EOF seen, whichever comes first
  406. buf.seek(0, 2) # seek end
  407. buf_len = buf.tell()
  408. if buf_len >= size:
  409. buf.seek(0)
  410. rv = buf.read(size)
  411. self._rbuf = StringIO()
  412. self._rbuf.write(buf.read())
  413. return rv
  414. self._rbuf = StringIO() # reset _rbuf. we consume it via buf.
  415. while True:
  416. try:
  417. data = self._sock.recv(self._rbufsize)
  418. except error, e:
  419. if e.args[0] == EINTR:
  420. continue
  421. raise
  422. if not data:
  423. break
  424. left = size - buf_len
  425. # did we just receive a newline?
  426. nl = data.find('\n', 0, left)
  427. if nl >= 0:
  428. nl += 1
  429. # save the excess data to _rbuf
  430. self._rbuf.write(data[nl:])
  431. if buf_len:
  432. buf.write(data[:nl])
  433. break
  434. else:
  435. # Shortcut. Avoid data copy through buf when returning
  436. # a substring of our first recv().
  437. return data[:nl]
  438. n = len(data)
  439. if n == size and not buf_len:
  440. # Shortcut. Avoid data copy through buf when
  441. # returning exactly all of our first recv().
  442. return data
  443. if n >= left:
  444. buf.write(data[:left])
  445. self._rbuf.write(data[left:])
  446. break
  447. buf.write(data)
  448. buf_len += n
  449. #assert buf_len == buf.tell()
  450. return buf.getvalue()
  451. def readlines(self, sizehint=0):
  452. total = 0
  453. list = []
  454. while True:
  455. line = self.readline()
  456. if not line:
  457. break
  458. list.append(line)
  459. total += len(line)
  460. if sizehint and total >= sizehint:
  461. break
  462. return list
  463. # Iterator protocols
  464. def __iter__(self):
  465. return self
  466. def next(self):
  467. line = self.readline()
  468. if not line:
  469. raise StopIteration
  470. return line
  471. _GLOBAL_DEFAULT_TIMEOUT = object()
  472. def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT,
  473. source_address=None):
  474. """Connect to *address* and return the socket object.
  475. Convenience function. Connect to *address* (a 2-tuple ``(host,
  476. port)``) and return the socket object. Passing the optional
  477. *timeout* parameter will set the timeout on the socket instance
  478. before attempting to connect. If no *timeout* is supplied, the
  479. global default timeout setting returned by :func:`getdefaulttimeout`
  480. is used. If *source_address* is set it must be a tuple of (host, port)
  481. for the socket to bind as a source address before making the connection.
  482. An host of '' or port 0 tells the OS to use the default.
  483. """
  484. host, port = address
  485. err = None
  486. for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  487. af, socktype, proto, canonname, sa = res
  488. sock = None
  489. try:
  490. sock = socket(af, socktype, proto)
  491. if timeout is not _GLOBAL_DEFAULT_TIMEOUT:
  492. sock.settimeout(timeout)
  493. if source_address:
  494. sock.bind(source_address)
  495. sock.connect(sa)
  496. return sock
  497. except error as _:
  498. err = _
  499. if sock is not None:
  500. sock.close()
  501. if err is not None:
  502. raise err
  503. else:
  504. raise error("getaddrinfo returns an empty list")