title: how to handle tcp client socket auto reconnect in python asyncio?
body: <p>I am using python asyncio streams to connect to several socket servers, but when the server is down, my code can't auto reconnect. </p>
  
  <p>What I need is that, when the server is down, my script will try to reconnect every 5 seconds, until connected and start to parse the data again.</p>
  
  <pre><code>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')
  </code></pre>