PageRenderTime 83ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/lib-python/2.7/socket.py

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