为什么 telnet 刚连上服务端就退出了?
import asynciofrom asyncio.base_events import Server
async def handle_client(reader, writer):
while True:
data = await reader.readline()
if not data:
break
writer.write(data)
writer.close()
async def main():
server: Server = await asyncio.get_running_loop().create_server(
handle_client, '127.0.0.1', 8888)
async with server:
await server.serve_forever()
asyncio.run(main())
用 python 的 asyncio 写了一个 demo
但是我用 telnet 连接这个 demo 的时候,会直接退出,不知道为什么?
╰─➤ telnet 127.0.0.1 8888 1 ↵Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
回答:
async def handle_client(reader, writer): welcome_message = "Welcome to the server!\n"
writer.write(welcome_message.encode())
await writer.drain()
while True:
data = await reader.read(100)
if not data:
break
writer.write(data)
await writer.drain()
以上是 为什么 telnet 刚连上服务端就退出了? 的全部内容, 来源链接: utcz.com/p/938875.html