1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
# Test network loopback behaviour
try:
import asyncio
except ImportError:
print("SKIP")
raise SystemExit
async def client(host, port):
print(f"client open_connection to {host}:{port}")
reader, writer = await asyncio.open_connection(host, port)
data_in = b"A" * 100
print("client writing")
writer.write(data_in)
await writer.drain()
await asyncio.sleep(0.1)
print("client reading")
data = await reader.readexactly(100)
print(f"client got {len(data)} bytes")
assert data_in == data
print("client closing")
writer.close()
await writer.wait_closed()
print("client closed")
async def echo_handler(reader, writer):
print("handler reading")
await asyncio.sleep(0.1)
data = await reader.readexactly(100)
print(f"handler got {len(data)} bytes")
print("handler writing")
writer.write(data)
await writer.drain()
print("handler closing")
writer.close()
await writer.wait_closed()
print("handler closed")
async def test(host, port):
print(f"create server on {host}:{port}")
server = await asyncio.start_server(echo_handler, host, port)
async with server:
print("server started")
await client("127.0.0.1", 8080)
print("server closed")
asyncio.run(test("0.0.0.0", 8080))
|