# pragma mypy relaxed # Start a server, on ‹localhost›, on the given port (using # ‹asyncio.start_server›) and have two clients connect to it. The # server takes care of the underlying sockets, so we will not be # creating them manually. Data is, again, transferred as ‹bytes› # object. # The server should return whatever data was sent to it. Clients # should send ‹hello› and ‹world›, respectively, then wait for the # answer from the server and return this answer. Add ‹print› # statements to make sure your server and clients behave as # expected; print data received by the server, sent to the clients # and sent and received by the clients on the client side. Make sure # to close the writing side of sockets once data is exhausted. import asyncio # Server-side handler for connecting clients. Read the message from # the client and echo it back to the client. async def handle_client( reader, writer ): pass # print( "server received & sending", ... ) # Client: connect to the server, send a message, wait for the answer # and return this answer. Assert that the answer matches the message # sent. Sleep for 1 second after sending ‹world›, to ensure message # order. async def client( port: int, msg: str ): pass # print( "client sending", ... ) pass # print( "client received", ... ) # The ‹start› function should start the server on the provided port # and return it. The ‹stop› function should stop the server returned # by ‹start›. async def start( port: int ): pass async def stop( server ) -> None: pass async def test_main() -> None: import sys import random from io import StringIO stdout = sys.stdout out = StringIO() sys.stdout = out port = random.randint( 9000, 13000 ) server = await start( port ) data = await asyncio.gather( client( port, 'hello' ), client( port, 'world' ) ) await stop( server ) assert data == [ 'hello', 'world' ], data sys.stdout = stdout output_ = out.getvalue() output = output_.split('\n') assert 'client sending hello' in output[ 0 : 4 ], output assert 'client sending world' in output[ 0 : 4 ], output assert 'server received & sending hello' in \ output[ 1 : 3 ], output assert 'server received & sending world' in \ output[ 1 : ], output if __name__ == "__main__": asyncio.run( test_main() )