gpt4 book ai didi

python - 为什么我在这里收到 ConnectionResetError 错误?

转载 作者:行者123 更新时间:2023-12-02 08:43:37 25 4
gpt4 key购买 nike

我是 Python 3 的新手,正在使用 asyncio。因此,我在以下服务器端代码中遇到了奇怪的行为:

import asyncio


@asyncio.coroutine
def handle_client(reader, writer):
print('Client connected.')
client_connected = True
while client_connected:
print('Waiting for client event.')
line = yield from reader.readline()
if line:
print('Got: {}'.format(line))
if line.decode() == 'echo\n':
print('Sending back echo.')
writer.write(line)
else:
print('Not sending back anything.')
else:
print('Client disconnected.')
client_connected = False


if __name__ == '__main__':
asyncio.async(asyncio.start_server(handle_client, 'localhost', 8888))
asyncio.get_event_loop().run_forever()

当我运行此客户端代码时(编辑:客户端代码手动输入到 IPython session 中,服务器在关闭套接字之前肯定有时间写入)...

import socket
client = socket.create_connection(('localhost', 8888))
client.sendall('echo\n'.encode())
client.close()

...我从服务器收到错误回溯:

C:\Users\Gnar\Anaconda3\python.exe C:/Users/Gnar/Code/echo.py
Client connected.
Waiting for client event.
Got: b'echo\n'
Sending back echo.
Waiting for client event.
Task exception was never retrieved
future: <Task finished coro=<handle_client() done, defined at C:/Users/Gnar/Code/echo.py:4> exception=ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)>
Traceback (most recent call last):
File "C:\Users\Gnar\Anaconda3\lib\asyncio\tasks.py", line 234, in _step
result = coro.throw(exc)
File "C:/Users/Gnar/Code/echo.py", line 10, in handle_client
line = yield from reader.readline()
File "C:\Users\Gnar\Anaconda3\lib\asyncio\streams.py", line 425, in readline
yield from self._wait_for_data('readline')
File "C:\Users\Gnar\Anaconda3\lib\asyncio\streams.py", line 393, in _wait_for_data
yield from self._waiter
File "C:\Users\Gnar\Anaconda3\lib\asyncio\futures.py", line 386, in __iter__
yield self # This tells Task to wait for completion.
File "C:\Users\Gnar\Anaconda3\lib\asyncio\tasks.py", line 287, in _wakeup
value = future.result()
File "C:\Users\Gnar\Anaconda3\lib\asyncio\futures.py", line 275, in result
raise self._exception
File "C:\Users\Gnar\Anaconda3\lib\asyncio\selector_events.py", line 662, in _read_ready
data = self._sock.recv(self.max_size)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

这个问题一定与writer.write有关,因为当我调用以下客户端代码(这使得服务器跳过写入)时,没有错误:

import socket
client = socket.create_connection(('localhost', 8888))
client.sendall('foo\n'.encode())
client.close()

对应的服务器日志:

C:\Users\Gnar\Anaconda3\python.exe C:/Users/Gnar/Code/echo.py
Client connected.
Waiting for client event.
Got: b'foo\n'
Not sending back anything.
Waiting for client event.
Client disconnected.

我错过了什么?我是否错误地使用了 asyncio?

谢谢!

最佳答案

您收到异常是因为您尝试在服务器端将一些数据写回客户端,但客户端在发送 后立即关闭 socket 'echo',而没有实际收到服务器的响应。如果在网络上有未接收到的数据时关闭套接字连接,您将在发送端收到错误,以便您知道远程端可能尚未收到您上次发送的任何内容。

如果您在调用 socket.close() 之前在客户端添加对 socket.recv(1024) 的调用,那么问题就会消失,这样客户端在关闭套接字之前等待服务器的响应。如果您只想优雅地处理异常,即使客户端做了错误的事情,您也可以在服务器端的写入调用周围使用 try/ except .

关于python - 为什么我在这里收到 ConnectionResetError 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29841257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com