PageRenderTime 60ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/green380/socket.py

https://bitbucket.org/anacrolix/green380
Python | 484 lines | 382 code | 14 blank | 88 comment | 29 complexity | 88996e02889370cad2ec4dc03ef5eb02 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. socket.getdefaulttimeout() -- get the default timeout value
  22. socket.setdefaulttimeout() -- set the default timeout value
  23. create_connection() -- connects to an address, with an optional timeout and
  24. optional source address.
  25. [*] not available on all platforms!
  26. Special objects:
  27. SocketType -- type object for socket objects
  28. error -- exception raised for I/O errors
  29. has_ipv6 -- boolean value indicating if IPv6 is supported
  30. Integer constants:
  31. AF_INET, AF_UNIX -- socket domains (first argument to socket() call)
  32. SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument)
  33. Many other constants may be defined; these may be used in calls to
  34. the setsockopt() and getsockopt() methods.
  35. """
  36. from errno import *
  37. from ._core import spawn, current_fiber, schedule, Timeout, timeout_multiplex
  38. import green380
  39. import _socket
  40. from _socket import *
  41. import os, sys, io
  42. __all__ = ["getfqdn", "create_connection"]
  43. __all__.extend(os._get_exports_list(_socket))
  44. strerror = os.strerror
  45. _realsocket = socket
  46. # WSA error codes
  47. if sys.platform.lower().startswith("win"):
  48. errorTab = {}
  49. errorTab[10004] = "The operation was interrupted."
  50. errorTab[10009] = "A bad file handle was passed."
  51. errorTab[10013] = "Permission denied."
  52. errorTab[10014] = "A fault occurred on the network??" # WSAEFAULT
  53. errorTab[10022] = "An invalid operation was attempted."
  54. errorTab[10035] = "The socket operation would block"
  55. errorTab[10036] = "A blocking operation is already in progress."
  56. errorTab[10048] = "The network address is in use."
  57. errorTab[10054] = "The connection has been reset."
  58. errorTab[10058] = "The network has been shut down."
  59. errorTab[10060] = "The operation timed out."
  60. errorTab[10061] = "Connection refused."
  61. errorTab[10063] = "The name is too long."
  62. errorTab[10064] = "The host is down."
  63. errorTab[10065] = "The host is unreachable."
  64. __all__.append("errorTab")
  65. class socket(_socket.socket):
  66. """A subclass of _socket.socket adding the makefile() method."""
  67. __slots__ = ["__weakref__", "_io_refs", "_closed", "__timeout"]
  68. def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None):
  69. _socket.socket.__init__(self, family, type, proto, fileno)
  70. self._io_refs = 0
  71. self._closed = False
  72. super().setblocking(False)
  73. self.__timeout = None
  74. def __enter__(self):
  75. return self
  76. def __exit__(self, *args):
  77. if not self._closed:
  78. self.close()
  79. def __repr__(self):
  80. """Wrap __repr__() to reveal the real class name."""
  81. s = _socket.socket.__repr__(self)
  82. if s.startswith("<socket object"):
  83. s = "<%s.%s%s%s" % (self.__class__.__module__,
  84. self.__class__.__name__,
  85. getattr(self, '_closed', False) and " [closed] " or "",
  86. s[7:])
  87. return s
  88. def __getstate__(self):
  89. raise TypeError("Cannot serialize socket object")
  90. def dup(self):
  91. """dup() -> socket object
  92. Return a new socket object connected to the same system resource.
  93. """
  94. fd = dup(self.fileno())
  95. sock = self.__class__(self.family, self.type, self.proto, fileno=fd)
  96. sock.settimeout(self.gettimeout())
  97. return sock
  98. def accept(self):
  99. """accept() -> (socket object, address info)
  100. Wait for an incoming connection. Return a new socket
  101. representing the connection, and the address of the client.
  102. For IP sockets, the address info is a pair (hostaddr, port).
  103. """
  104. yield 'read', self
  105. fd, addr = self._accept()
  106. sock = socket(self.family, self.type, self.proto, fileno=fd)
  107. # Issue #7995: if no default timeout is set and the listening
  108. # socket had a (non-zero) timeout, force the new socket in blocking
  109. # mode to override platform-specific socket flags inheritance.
  110. if getdefaulttimeout() is None and self.gettimeout():
  111. sock.setblocking(True)
  112. return sock, addr
  113. def makefile(self, mode="r", buffering=None, *,
  114. encoding=None, errors=None, newline=None):
  115. """makefile(...) -> an I/O stream connected to the socket
  116. The arguments are as for io.open() after the filename,
  117. except the only mode characters supported are 'r', 'w' and 'b'.
  118. The semantics are similar too. (XXX refactor to share code?)
  119. """
  120. for c in mode:
  121. if c not in {"r", "w", "b"}:
  122. raise ValueError("invalid mode %r (only r, w, b allowed)")
  123. writing = "w" in mode
  124. reading = "r" in mode or not writing
  125. assert reading or writing
  126. binary = "b" in mode
  127. rawmode = ""
  128. if reading:
  129. rawmode += "r"
  130. if writing:
  131. rawmode += "w"
  132. raw = SocketIO(self, rawmode)
  133. self._io_refs += 1
  134. if buffering is None:
  135. buffering = -1
  136. if buffering < 0:
  137. buffering = io.DEFAULT_BUFFER_SIZE
  138. if buffering == 0:
  139. if not binary:
  140. raise ValueError("unbuffered streams must be binary")
  141. return raw
  142. if reading and writing:
  143. buffer = io.BufferedRWPair(raw, raw, buffering)
  144. elif reading:
  145. buffer = io.BufferedReader(raw, buffering)
  146. else:
  147. assert writing
  148. buffer = io.BufferedWriter(raw, buffering)
  149. if binary:
  150. return buffer
  151. text = io.TextIOWrapper(buffer, encoding, errors, newline)
  152. text.mode = mode
  153. return text
  154. def _decref_socketios(self):
  155. if self._io_refs > 0:
  156. self._io_refs -= 1
  157. if self._closed:
  158. self.close()
  159. def _real_close(self, _ss=_socket.socket):
  160. # This function should not reference any globals. See issue #808164.
  161. _ss.close(self)
  162. def close(self):
  163. # This function should not reference any globals. See issue #808164.
  164. self._closed = True
  165. if self._io_refs <= 0:
  166. self._real_close()
  167. def recv_until(self, term):
  168. buf = b''
  169. while not buf.endswith(term):
  170. yield 'read', self
  171. buf1 = super().recv(0x10000, _socket.MSG_PEEK)
  172. if not buf1:
  173. break
  174. index = (buf + buf1).find(term)
  175. buf2 = super().recv(len(buf1) if index == -1 else index - len(buf) + len(term))
  176. buf += buf2
  177. return buf
  178. recv_term = recv_until
  179. def recv_exactly(self, count):
  180. buf = b''
  181. while len(buf) != count:
  182. buf1 = yield from self.recv(count - len(buf))
  183. if not buf1:
  184. break
  185. buf += buf1
  186. return buf
  187. def recv(self, count):
  188. if self._closed:
  189. return
  190. yield 'read', self
  191. return super().recv(count)
  192. def recv_into(self, buf):
  193. yield 'read', self
  194. return super().recv_into(buf)
  195. def sendall(self, buf):
  196. while buf:
  197. sent = yield from self.send(buf)
  198. if not sent:
  199. break
  200. buf = buf[sent:]
  201. def send(self, buf):
  202. yield 'write', self
  203. return super().send(buf)
  204. def settimeout(self, timeout):
  205. self.__timeout = timeout
  206. def setblocking(self, blocking):
  207. self.settimeout(None if blocking else 0)
  208. def connect(self, addr):
  209. ch = green380.Channel()
  210. @green380.spawn
  211. def timeout_fiber():
  212. yield from green380.sleep(self.__timeout)
  213. yield from ch.send(False)
  214. @green380.spawn
  215. def event_fiber():
  216. while True:
  217. yield 'write', self
  218. yield from ch.send(True)
  219. while True:
  220. err = self.getsockopt(SOL_SOCKET, SO_ERROR)
  221. if err:
  222. raise error(err, strerror(err))
  223. err = super().connect_ex(addr)
  224. if not err or err == EISCONN:
  225. break
  226. if err in {EWOULDBLOCK, EINPROGRESS, EALREADY}:
  227. if not (yield from ch.get()):
  228. raise timeout('timed out')
  229. else:
  230. raise error(err, strerror(err))
  231. def fromfd(fd, family, type, proto=0):
  232. """ fromfd(fd, family, type[, proto]) -> socket object
  233. Create a socket object from a duplicate of the given file
  234. descriptor. The remaining arguments are the same as for socket().
  235. """
  236. nfd = dup(fd)
  237. return socket(family, type, proto, nfd)
  238. if hasattr(_socket, "socketpair"):
  239. def socketpair(family=None, type=SOCK_STREAM, proto=0):
  240. """socketpair([family[, type[, proto]]]) -> (socket object, socket object)
  241. Create a pair of socket objects from the sockets returned by the platform
  242. socketpair() function.
  243. The arguments are the same as for socket() except the default family is
  244. AF_UNIX if defined on the platform; otherwise, the default is AF_INET.
  245. """
  246. if family is None:
  247. try:
  248. family = AF_UNIX
  249. except NameError:
  250. family = AF_INET
  251. a, b = _socket.socketpair(family, type, proto)
  252. a = socket(family, type, proto, a.detach())
  253. b = socket(family, type, proto, b.detach())
  254. return a, b
  255. _blocking_errnos = { EAGAIN, EWOULDBLOCK }
  256. class SocketIO(io.RawIOBase):
  257. """Raw I/O implementation for stream sockets.
  258. This class supports the makefile() method on sockets. It provides
  259. the raw I/O interface on top of a socket object.
  260. """
  261. # One might wonder why not let FileIO do the job instead. There are two
  262. # main reasons why FileIO is not adapted:
  263. # - it wouldn't work under Windows (where you can't used read() and
  264. # write() on a socket handle)
  265. # - it wouldn't work with socket timeouts (FileIO would ignore the
  266. # timeout and consider the socket non-blocking)
  267. # XXX More docs
  268. def __init__(self, sock, mode):
  269. if mode not in ("r", "w", "rw", "rb", "wb", "rwb"):
  270. raise ValueError("invalid mode: %r" % mode)
  271. io.RawIOBase.__init__(self)
  272. self._sock = sock
  273. if "b" not in mode:
  274. mode += "b"
  275. self._mode = mode
  276. self._reading = "r" in mode
  277. self._writing = "w" in mode
  278. self._timeout_occurred = False
  279. def readinto(self, b):
  280. """Read up to len(b) bytes into the writable buffer *b* and return
  281. the number of bytes read. If the socket is non-blocking and no bytes
  282. are available, None is returned.
  283. If *b* is non-empty, a 0 return value indicates that the connection
  284. was shutdown at the other end.
  285. """
  286. self._checkClosed()
  287. self._checkReadable()
  288. if self._timeout_occurred:
  289. raise IOError("cannot read from timed out object")
  290. gen = self._sock.recv_into(b)
  291. while True:
  292. try:
  293. next(gen)
  294. except StopIteration as exc:
  295. return exc.value
  296. except timeout:
  297. self._timeout_occurred = True
  298. raise
  299. except InterruptedError:
  300. continue
  301. except error as e:
  302. if e.args[0] in _blocking_errnos:
  303. return None
  304. raise
  305. def write(self, b):
  306. """Write the given bytes or bytearray object *b* to the socket
  307. and return the number of bytes written. This can be less than
  308. len(b) if not all data could be written. If the socket is
  309. non-blocking and no bytes could be written None is returned.
  310. """
  311. self._checkClosed()
  312. self._checkWritable()
  313. try:
  314. return (yield from self._sock.send(b))
  315. except error as e:
  316. # XXX what about EINTR?
  317. if e.args[0] in _blocking_errnos:
  318. return None
  319. raise
  320. def readable(self):
  321. """True if the SocketIO is open for reading.
  322. """
  323. return self._reading and not self.closed
  324. def writable(self):
  325. """True if the SocketIO is open for writing.
  326. """
  327. return self._writing and not self.closed
  328. def fileno(self):
  329. """Return the file descriptor of the underlying socket.
  330. """
  331. self._checkClosed()
  332. return self._sock.fileno()
  333. @property
  334. def name(self):
  335. if not self.closed:
  336. return self.fileno()
  337. else:
  338. return -1
  339. @property
  340. def mode(self):
  341. return self._mode
  342. def close(self):
  343. """Close the SocketIO object. This doesn't close the underlying
  344. socket, except if all references to it have disappeared.
  345. """
  346. if self.closed:
  347. return
  348. io.RawIOBase.close(self)
  349. self._sock._decref_socketios()
  350. self._sock = None
  351. def getfqdn(name=''):
  352. """Get fully qualified domain name from name.
  353. An empty argument is interpreted as meaning the local host.
  354. First the hostname returned by gethostbyaddr() is checked, then
  355. possibly existing aliases. In case no FQDN is available, hostname
  356. from gethostname() is returned.
  357. """
  358. name = name.strip()
  359. if not name or name == '0.0.0.0':
  360. name = gethostname()
  361. try:
  362. hostname, aliases, ipaddrs = gethostbyaddr(name)
  363. except error:
  364. pass
  365. else:
  366. aliases.insert(0, hostname)
  367. for name in aliases:
  368. if '.' in name:
  369. break
  370. else:
  371. name = hostname
  372. return name
  373. _GLOBAL_DEFAULT_TIMEOUT = object()
  374. def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT,
  375. source_address=None):
  376. """Connect to *address* and return the socket object.
  377. Convenience function. Connect to *address* (a 2-tuple ``(host,
  378. port)``) and return the socket object. Passing the optional
  379. *timeout* parameter will set the timeout on the socket instance
  380. before attempting to connect. If no *timeout* is supplied, the
  381. global default timeout setting returned by :func:`getdefaulttimeout`
  382. is used. If *source_address* is set it must be a tuple of (host, port)
  383. for the socket to bind as a source address before making the connection.
  384. An host of '' or port 0 tells the OS to use the default.
  385. """
  386. host, port = address
  387. err = None
  388. for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  389. af, socktype, proto, canonname, sa = res
  390. sock = None
  391. try:
  392. sock = socket(af, socktype, proto)
  393. if timeout is not _GLOBAL_DEFAULT_TIMEOUT:
  394. sock.settimeout(timeout)
  395. if source_address:
  396. sock.bind(source_address)
  397. yield from sock.connect(sa)
  398. return sock
  399. except error as _:
  400. err = _
  401. if sock is not None:
  402. sock.close()
  403. if err is not None:
  404. raise err
  405. else:
  406. raise error("getaddrinfo returns an empty list")