This is a lower level variation of a similar previous question.
The TCP Echo Server example in the Transports and Protocols docs will never timeout an established socket connection.
In order to add a 5 seconds timeout, I simply scheduled a callback with a loop.call_later
call.
import asyncio
class EchoServerProtocol(asyncio.Protocol):
def connection_made(self, transport):
self.transport = transport
# ---> here the timeout is implemented <---
self.timer = asyncio.get_running_loop().call_later(5, transport.close)
print("connection made")
def data_received(self, data):
self.timer.cancel()
self.transport.write(data)
self.transport.close()
print("echo sent")
def connection_lost(self, exc):
self.timer.cancel()
print("connection lost")
async def main():
loop = asyncio.get_running_loop()
server = await loop.create_server(lambda: EchoServerProtocol(), "127.0.0.1", 8888)
async with server:
await server.serve_forever()
asyncio.run(main())
I was wondering if this is can be considered a "canonical" solution, or better methods can be devised.