Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Async socket server and ports
#1
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/asyn...-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
Quote
#2
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:
  • DefaultSelector, which uses select, epoll and other os related selectors for sockets
  • Generators (await was before yield from, async was previously a normal generator with a yield statement used with send from outside)
  • EventLoop, which is asyncio.run_forever()

If you want to dig deeper in asyncio, you should watch some talks on YouTube.
Some of them are explaining it very deep.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Server and Network (socket) [WinError 10053] Timxxx 1 225 Aug-23-2019, 10:03 AM
Last Post: iMuny
  Server and Network (socket) [WinError 10053] SheeppOSU 2 781 Apr-13-2019, 09:23 PM
Last Post: SheeppOSU
  Multi connection socket server help! MuntyScruntfundle 0 365 Feb-19-2019, 12:03 PM
Last Post: MuntyScruntfundle
  Send data BMP180 between client and server trought module socket smalhao 0 682 Jul-30-2018, 12:56 PM
Last Post: smalhao
  Can't see my UPnP Opened Ports BaiYouLing4 0 966 Aug-04-2017, 01:33 AM
Last Post: BaiYouLing4
  socket server with SSL accepting unencrypted data kopite 0 1,199 Apr-20-2017, 08:31 AM
Last Post: kopite
  socket server help abrew132 1 1,651 Apr-20-2017, 03:02 AM
Last Post: Skaperen

Forum Jump:


Users browsing this thread: 1 Guest(s)