diff options
author | Alex Grönholm <alex.gronholm@nextday.fi> | 2022-03-13 16:42:29 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-13 16:42:29 (GMT) |
commit | 9f04ee569cebb8b4c6f04bea95d91a19c5403806 (patch) | |
tree | abd4626ee19d394f35109b4cd2830d50a4a4f9d9 /Lib/test/test_asyncio/utils.py | |
parent | 7e473e94a52024ac821dd2f206290423e4987ead (diff) | |
download | cpython-9f04ee569cebb8b4c6f04bea95d91a19c5403806.zip cpython-9f04ee569cebb8b4c6f04bea95d91a19c5403806.tar.gz cpython-9f04ee569cebb8b4c6f04bea95d91a19c5403806.tar.bz2 |
bpo-46805: Add low level UDP socket functions to asyncio (GH-31455)
Diffstat (limited to 'Lib/test/test_asyncio/utils.py')
-rw-r--r-- | Lib/test/test_asyncio/utils.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/Lib/test/test_asyncio/utils.py b/Lib/test/test_asyncio/utils.py index 0b9cde6..c32494d 100644 --- a/Lib/test/test_asyncio/utils.py +++ b/Lib/test/test_asyncio/utils.py @@ -281,6 +281,31 @@ def run_test_server(*, host='127.0.0.1', port=0, use_ssl=False): server_ssl_cls=SSLWSGIServer) +def echo_datagrams(sock): + while True: + data, addr = sock.recvfrom(4096) + if data == b'STOP': + sock.close() + break + else: + sock.sendto(data, addr) + + +@contextlib.contextmanager +def run_udp_echo_server(*, host='127.0.0.1', port=0): + addr_info = socket.getaddrinfo(host, port, type=socket.SOCK_DGRAM) + family, type, proto, _, sockaddr = addr_info[0] + sock = socket.socket(family, type, proto) + sock.bind((host, port)) + thread = threading.Thread(target=lambda: echo_datagrams(sock)) + thread.start() + try: + yield sock.getsockname() + finally: + sock.sendto(b'STOP', sock.getsockname()) + thread.join() + + def make_test_protocol(base): dct = {} for name in dir(base): |