trsock.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import socket
  2. import warnings
  3. class TransportSocket:
  4. """A socket-like wrapper for exposing real transport sockets.
  5. These objects can be safely returned by APIs like
  6. `transport.get_extra_info('socket')`. All potentially disruptive
  7. operations (like "socket.close()") are banned.
  8. """
  9. __slots__ = ('_sock',)
  10. def __init__(self, sock: socket.socket):
  11. self._sock = sock
  12. def _na(self, what):
  13. warnings.warn(
  14. f"Using {what} on sockets returned from get_extra_info('socket') "
  15. f"will be prohibited in asyncio 3.9. Please report your use case "
  16. f"to bugs.python.org.",
  17. DeprecationWarning, source=self)
  18. @property
  19. def family(self):
  20. return self._sock.family
  21. @property
  22. def type(self):
  23. return self._sock.type
  24. @property
  25. def proto(self):
  26. return self._sock.proto
  27. def __repr__(self):
  28. s = (
  29. f"<asyncio.TransportSocket fd={self.fileno()}, "
  30. f"family={self.family!s}, type={self.type!s}, "
  31. f"proto={self.proto}"
  32. )
  33. if self.fileno() != -1:
  34. try:
  35. laddr = self.getsockname()
  36. if laddr:
  37. s = f"{s}, laddr={laddr}"
  38. except socket.error:
  39. pass
  40. try:
  41. raddr = self.getpeername()
  42. if raddr:
  43. s = f"{s}, raddr={raddr}"
  44. except socket.error:
  45. pass
  46. return f"{s}>"
  47. def __getstate__(self):
  48. raise TypeError("Cannot serialize asyncio.TransportSocket object")
  49. def fileno(self):
  50. return self._sock.fileno()
  51. def dup(self):
  52. return self._sock.dup()
  53. def get_inheritable(self):
  54. return self._sock.get_inheritable()
  55. def shutdown(self, how):
  56. # asyncio doesn't currently provide a high-level transport API
  57. # to shutdown the connection.
  58. self._sock.shutdown(how)
  59. def getsockopt(self, *args, **kwargs):
  60. return self._sock.getsockopt(*args, **kwargs)
  61. def setsockopt(self, *args, **kwargs):
  62. self._sock.setsockopt(*args, **kwargs)
  63. def getpeername(self):
  64. return self._sock.getpeername()
  65. def getsockname(self):
  66. return self._sock.getsockname()
  67. def getsockbyname(self):
  68. return self._sock.getsockbyname()
  69. def accept(self):
  70. self._na('accept() method')
  71. return self._sock.accept()
  72. def connect(self, *args, **kwargs):
  73. self._na('connect() method')
  74. return self._sock.connect(*args, **kwargs)
  75. def connect_ex(self, *args, **kwargs):
  76. self._na('connect_ex() method')
  77. return self._sock.connect_ex(*args, **kwargs)
  78. def bind(self, *args, **kwargs):
  79. self._na('bind() method')
  80. return self._sock.bind(*args, **kwargs)
  81. def ioctl(self, *args, **kwargs):
  82. self._na('ioctl() method')
  83. return self._sock.ioctl(*args, **kwargs)
  84. def listen(self, *args, **kwargs):
  85. self._na('listen() method')
  86. return self._sock.listen(*args, **kwargs)
  87. def makefile(self):
  88. self._na('makefile() method')
  89. return self._sock.makefile()
  90. def sendfile(self, *args, **kwargs):
  91. self._na('sendfile() method')
  92. return self._sock.sendfile(*args, **kwargs)
  93. def close(self):
  94. self._na('close() method')
  95. return self._sock.close()
  96. def detach(self):
  97. self._na('detach() method')
  98. return self._sock.detach()
  99. def sendmsg_afalg(self, *args, **kwargs):
  100. self._na('sendmsg_afalg() method')
  101. return self._sock.sendmsg_afalg(*args, **kwargs)
  102. def sendmsg(self, *args, **kwargs):
  103. self._na('sendmsg() method')
  104. return self._sock.sendmsg(*args, **kwargs)
  105. def sendto(self, *args, **kwargs):
  106. self._na('sendto() method')
  107. return self._sock.sendto(*args, **kwargs)
  108. def send(self, *args, **kwargs):
  109. self._na('send() method')
  110. return self._sock.send(*args, **kwargs)
  111. def sendall(self, *args, **kwargs):
  112. self._na('sendall() method')
  113. return self._sock.sendall(*args, **kwargs)
  114. def set_inheritable(self, *args, **kwargs):
  115. self._na('set_inheritable() method')
  116. return self._sock.set_inheritable(*args, **kwargs)
  117. def share(self, process_id):
  118. self._na('share() method')
  119. return self._sock.share(process_id)
  120. def recv_into(self, *args, **kwargs):
  121. self._na('recv_into() method')
  122. return self._sock.recv_into(*args, **kwargs)
  123. def recvfrom_into(self, *args, **kwargs):
  124. self._na('recvfrom_into() method')
  125. return self._sock.recvfrom_into(*args, **kwargs)
  126. def recvmsg_into(self, *args, **kwargs):
  127. self._na('recvmsg_into() method')
  128. return self._sock.recvmsg_into(*args, **kwargs)
  129. def recvmsg(self, *args, **kwargs):
  130. self._na('recvmsg() method')
  131. return self._sock.recvmsg(*args, **kwargs)
  132. def recvfrom(self, *args, **kwargs):
  133. self._na('recvfrom() method')
  134. return self._sock.recvfrom(*args, **kwargs)
  135. def recv(self, *args, **kwargs):
  136. self._na('recv() method')
  137. return self._sock.recv(*args, **kwargs)
  138. def settimeout(self, value):
  139. if value == 0:
  140. return
  141. raise ValueError(
  142. 'settimeout(): only 0 timeout is allowed on transport sockets')
  143. def gettimeout(self):
  144. return 0
  145. def setblocking(self, flag):
  146. if not flag:
  147. return
  148. raise ValueError(
  149. 'setblocking(): transport sockets cannot be blocking')
  150. def __enter__(self):
  151. self._na('context manager protocol')
  152. return self._sock.__enter__()
  153. def __exit__(self, *err):
  154. self._na('context manager protocol')
  155. return self._sock.__exit__(*err)