当前位置:   article > 正文

asyncio.Protocol socket 断线重连_asyncio 断网重新连接

asyncio 断网重新连接

asyncio.Protocol socket 断线重连

 原文:https://www.jianshu.com/p/f5bc755d3d8a

  • 场景
    使用asyncio.Protocol进行异步连接时,如何断线重连
  • code
    socket client 放在了单独的一个线程中
    soket_server.py
  1. import asyncio
  2. class EchoServerClientProtocol(asyncio.Protocol):
  3. def connection_made(self, transport):
  4. peername = transport.get_extra_info('peername')
  5. print('Connection from {}'.format(peername))
  6. self.transport = transport
  7. def data_received(self, data):
  8. message = data.decode()
  9. print('Data received: {!r}'.format(message))
  10. message = "Hello Client"
  11. self.transport.write(data)
  12. self.transport.close()
  13. loop = asyncio.get_event_loop()
  14. # Each client connection will create a new protocol instance
  15. coro = loop.create_server(EchoServ
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/91441
推荐阅读
相关标签
  

闽ICP备14008679号