I'm learning about python 3 asyncio library, and I've run into a small issue. I'm trying to adapt the EchoServer example from the python docs to prompt for user input rather than just echo what the client sends.
I thought it would be as easy as just adding a call to input(), but of course input() will block until there is user input which causes the problems.
Ideally I would like to continue receiving data from the client even when the server has nothing to "say". Somewhat like a chat client where each connection is chatting with the server. I'd like to be able to switch to-and-from each individual connection and send input as needed from stdin. Almost like a P2P chat client.
Consider the following modified EchoServer code:
import asyncio class EchoServerClientProtocol(asyncio.Protocol): def connection_made(self, transport): peername = transport.get_extra_info('peername') print('Connection from {}'.format(peername)) self.transport = transport def data_received(self, data): message = data.decode() print('Data received: {!r}'.format(message)) reply = input() print('Send: {!r}'.format(reply)) self.transport.write(reply.encode()) #print('Close the client socket') #self.transport.close() loop = asyncio.get_event_loop() # Each client connection will create a new protocol instance coro = loop.create_server(EchoServerClientProtocol, '127.0.0.1', 8888) server = loop.run_until_complete(coro) # Serve requests until CTRL+c is pressed print('Serving on {}'.format(server.sockets[0].getsockname())) try: loop.run_forever() except KeyboardInterrupt: pass # Close the server server.close() loop.run_until_complete(server.wait_closed()) loop.close() How would I go about getting input form stdin on the server side and specify which connection to send it to while still received inputs from the connected clients?