How to handle tcp client socket automatically reconnect to python asyncio?

I am using python asyncio streams to connect to multiple socket servers, but when the server is down, my code cannot automatically reconnect.

I need that when the server is down my script will try to connect every 5 seconds until it is connected and starts parsing the data again.

import asyncio

server1 = {'host': '192.168.1.51', 'port': 11110}
server2 = {'host': '192.168.1.52', 'port': 11110}


async def tcp_client(host, port, loop):
    print('connect to server {} {}'.format(host, str(port)))
    reader, writer = await asyncio.open_connection(host, port, loop=loop)

    while True:
        data = await reader.read(100)
        print('raw data received: {}'.format(data))

        await asyncio.sleep(0.1)


loop = asyncio.get_event_loop()
try:
    for server in [server1, server2]:
        loop.run_until_complete(tcp_client(server['host'], server['port'], loop))
        print('task added: connect to server {} {}'.format(server['host'], server['port']))
finally:
    loop.close()
    print('loop closed')

      

+3


source to share


1 answer


You can handle reconnection by simply iterating over the operator try/except

.

Additionally, asyncio.wait_for can be used to set a timeout on a read operation.



Consider this working example:

import asyncio

async def tcp_client(host, port):
    reader, writer = await asyncio.open_connection(host, port)
    try:
        while not reader.at_eof():
            data = await asyncio.wait_for(reader.read(100), 3.0)
            print('raw data received: {}'.format(data))
    finally:
        writer.close()

async def tcp_reconnect(host, port):
    server = '{} {}'.format(host, port)
    while True:
        print('Connecting to server {} ...'.format(server))
        try:
            await tcp_client(host, port)
        except ConnectionRefusedError:
            print('Connection to server {} failed!'.format(server))
        except asyncio.TimeoutError:
            print('Connection to server {} timed out!'.format(server))
        else:
            print('Connection to server {} is closed.'.format(server))
        await asyncio.sleep(2.0)

async def main():
    servers = [('localhost', 8888), ('localhost', 9999)]
    coros = [tcp_reconnect(host, port) for host, port in servers]
    await asyncio.gather(*coros)

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
    loop.close()

      

+4


source







All Articles