PageRenderTime 52ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib-python/modified-2.7/socket.py

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