我想在python (python 3.9.9)中使用asyncio在一个脚本中运行两个简单的TCP服务器。显然,每个服务器运行在不同的端口上。我在使用正确的语法来创建和同时启动两个服务器时遇到了麻烦。下面是到目前为止我所拥有的,但是我一直得到RuntimeError:这个事件循环已经在运行。
我已经看到了使用函数和循环来实现这一点的方法。start_server,但我需要使用asyncio。协议类,这样我就可以访问conection_made和connection_lost的回调。我不知道是否需要循环。create_server或其他语法,但我所做的显然是错误的。那么如何用asyncio。protocol实现这个呢?
注意:这两个服务器都意味着永远运行。客户端可以连接和断开,但服务器应该永远运行。
###############################################################################
# echoserver.py
# Simple code to run EchoServer on two ports simultaneously
###############################################################################
import asyncio
FIRST_PROCESS_PORT =14401
SECOND_PROCESS_PORT =14402
class MyLogger(object):
def info(self,s):
print(s)
logger = MyLogger()
#
# EchoServer class
#
class EchoServer(asyncio.Protocol):
def __init__(self):
self.client_info=None
# Begin asyncio.Protocol overrides
def connection_made(self,transport):
self.transport = transport
self.client_info = self.transport.get_extra_info('peername')
logger.info('connection_made from: %s' % self.client_info)
def connection_lost(self, reason):
logger.info( 'connection_lost: %s | %s' % (self.client_info, reason))
def data_received(self,data):
print("Received: %s" % str(data))
self.transport.write(data)
async def main():
global server1
global server2
loop = asyncio.get_running_loop()
factory1 = await loop.create_server(lambda: EchoServer(),"localhost",FIRST_PROCESS_PORT)
factory2 = await loop.create_server(lambda: EchoServer(),"localhost",SECOND_PROCESS_PORT)
server1 = await loop.run_until_complete(factory1)
server2 = await loop.run_until_complete(factory2)
async with server1, server2:
await asyncio.gather(server1.run_forever(),server2.run_forever())
print("Main done");
###################
# main code here
###################
if __name__ == "__main__":
asyncio.run(main())
print("Done");
我做了一些简单的重新安排,使服务器在两个端口上工作:
###############################################################################
# echoserver.py
# Simple code to run EchoServer on two ports simultaneously
###############################################################################
import asyncio
FIRST_PROCESS_PORT = 14401
SECOND_PROCESS_PORT = 14402
class MyLogger(object):
def info(self, s):
print(s)
logger = MyLogger()
class EchoServer(asyncio.Protocol):
def __init__(self):
self.client_info = None
# Begin asyncio.Protocol overrides
def connection_made(self, transport):
self.transport = transport
self.client_info = self.transport.get_extra_info("peername")
logger.info(f"connection_made from: {self.client_info}")
def connection_lost(self, reason):
logger.info("connection_lost: %s | %s" % (self.client_info, reason))
def data_received(self, data):
print("Received: %s" % str(data))
self.transport.write(data)
async def main():
loop = asyncio.get_running_loop()
server1 = await loop.create_server(
EchoServer, "0.0.0.0", FIRST_PROCESS_PORT
)
server2 = await loop.create_server(
EchoServer, "0.0.0.0", SECOND_PROCESS_PORT
)
# serve forever
async with server1, server2:
while True:
await asyncio.sleep(3600)
if __name__ == "__main__":
asyncio.run(main())
这将在所有接口(0.0.0.0
)上创建两个端口,但您可以将其更改为localhost
。
在其他终端上运行:
echo "Hello World" | nc -N <your IP address> 14401
echo "Hello World" | nc -N <your IP address> 14402
服务器响应:
connection_made from: ('XXX', 41760)
Received: b'Hello Worldn'
connection_lost: ('XXX', 41760) | None
connection_made from: ('XXX', 32872)
Received: b'Hello Worldn'
connection_lost: ('XXX', 32872) | None