asyncio.Protocol socket 断线重连
原文:https://www.jianshu.com/p/f5bc755d3d8a
- 场景
使用asyncio.Protocol进行异步连接时,如何断线重连 - code
socket client 放在了单独的一个线程中
soket_server.py
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))
message = "Hello Client"
self.transport.write(data)
self.transport.close()
loop = asyncio.get_event_loop()
# Each client connection will create a new protocol instance
coro = loop.create_server(EchoServ