19 loop: asyncio.AbstractEventLoop |
None =
None,
20 timeout=DEFAULT_TIMEOUT,
23 loop = asyncio.get_running_loop()
24 self.
_loop: asyncio.AbstractEventLoop = loop
25 self.
_sock: socket.socket = sock
27 sock.setblocking(
False)
30 return f
'<AsyncioSocket for {self._sock}>'
35 def __exit__(self, *args):
40 return self.
_sock.type
43 def socket(self) -> socket.socket:
46 def fileno(self) -> int:
47 return self.
_sock.fileno()
49 async def connect(self, address, *, timeout=_DefaultTimeout):
50 coro = self.
_loop.sock_connect(self.
_sock, address)
53 async def sendto(self, *args, timeout=_DefaultTimeout):
55 if not _ASYNCIO_HAS_SENDTO:
57 coro = self.
_loop.sock_sendto(self.
_sock, *args)
60 except NotImplementedError:
63 async def sendall(self, data, *, timeout=_DefaultTimeout):
64 coro = self.
_loop.sock_sendall(self.
_sock, data)
67 async def recv(self, size, *, timeout=_DefaultTimeout):
71 async def recvfrom(self, *args, timeout=_DefaultTimeout):
73 if not _ASYNCIO_HAS_SENDTO:
75 coro = self.
_loop.sock_recvfrom(self.
_sock, *args)
78 except NotImplementedError:
81 async def accept(self, *, timeout=_DefaultTimeout):
83 conn, address = await self.
_with_timeout(coro, timeout=timeout)
84 return from_socket(conn), address
86 def bind(self, address):
87 return self.
_sock.bind(address)
89 def listen(self, *args):
90 return self.
_sock.listen(*args)
92 def getsockname(self):
93 return self.
_sock.getsockname()
95 def setsockopt(self, *args, **kwargs):
96 self.
_sock.setsockopt(*args, **kwargs)
101 def has_data(self) -> bool:
102 rlist, _, _ = select.select([self.
_sock], [], [], 0)
105 def can_write(self) -> bool:
106 _, wlist, _ = select.select([], [self.
_sock], [], 0)
109 async def wait_for_data(self, timeout=_DefaultTimeout):
115 async def _with_timeout(self, awaitable, timeout):
117 if timeout
is _DefaultTimeout:
120 return await asyncio.wait_for(awaitable, timeout=timeout)
122 async def _sendto_legacy(self, *args, timeout):
125 return self.
_sock.sendto(*args)
126 except (BlockingIOError, InterruptedError):
128 fut = self.
_loop.create_future()
130 self.
_loop.add_writer(
141 async def _recvfrom_legacy(self, *args, timeout):
144 return self.
_sock.recvfrom(*args)
145 except (BlockingIOError, InterruptedError):
147 fut = self.
_loop.create_future()
149 self.
_loop.add_reader(
162 def __init__(self, loop=None):
164 loop = asyncio.get_running_loop()
167 def from_socket(self, sock, timeout=DEFAULT_TIMEOUT):
168 return from_socket(sock, loop=self.
_loop, timeout=timeout)
170 def socket(self, *args, timeout=DEFAULT_TIMEOUT):
171 sock = socket.socket(*args)
174 async def getaddrinfo(self, *args, timeout=DEFAULT_TIMEOUT, **kwargs):
175 coro = self.
_loop.getaddrinfo(*args, **kwargs)
176 return await asyncio.wait_for(coro, timeout=timeout)
178 def tcp(self, *, timeout=DEFAULT_TIMEOUT):
179 return self.
socket(socket.AF_INET, socket.SOCK_STREAM, timeout=timeout)
181 def udp(self, *, timeout=DEFAULT_TIMEOUT):
182 return self.
socket(socket.AF_INET, socket.SOCK_DGRAM, timeout=timeout)
184 def socketpair(self, *args, timeout=DEFAULT_TIMEOUT, **kwargs):
185 sock1, sock2 = socket.socketpair(*args, **kwargs)
187 sock2, timeout=timeout