Async socket server and ports - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Networking (https://python-forum.io/forum-12.html) +--- Thread: Async socket server and ports (/thread-16411.html) |
Async socket server and ports - Pengwyn - Feb-27-2019 Hi guys, first time posting on the Python forums I am wondering how do asynchronous socket servers go about maintaining so many clients, specifically, how can the server have more than one client in a connected TCP stream if it is on a single port, or is there some automagical stuff happening to assign different ports behind the scenes? for example in the doceumentation at https://docs.python.org/3.7/library/asyncio-stream.html#streams-coroutine-based-api the server is serving on port 8888, however this expects multiple clients to have connections. import asyncio async def handle_echo(reader, writer): data = await reader.read(100) message = data.decode() addr = writer.get_extra_info('peername') print(f"Received {message!r} from {addr!r}") print(f"Send: {message!r}") writer.write(data) await writer.drain() print("Close the connection") writer.close() async def main(): server = await asyncio.start_server( handle_echo, '127.0.0.1', 8888) addr = server.sockets[0].getsockname() print(f'Serving on {addr}') async with server: await server.serve_forever() asyncio.run(main())Thanks RE: Async socket server and ports - DeaD_EyE - Feb-28-2019 With a trick the developers made asynchronous code looking like synchronous code. They use generators with the send function, which allows to delegate everything. It's a combination of:
If you want to dig deeper in asyncio, you should watch some talks on YouTube. Some of them are explaining it very deep. |