首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在连接客户端之后,服务器上出现错误"concurrent.futures._base.CancelledError“

在连接客户端之后,服务器上出现错误"concurrent.futures._base.CancelledError“
EN

Stack Overflow用户
提问于 2020-03-24 22:44:57
回答 2查看 3.4K关注 0票数 0

在将客户端连接到服务器后,我无法解决错误问题。以下是Python 3.7.7的服务器代码:

代码语言:javascript
运行
复制
#!/usr/bin/env python3

# WS server example

import asyncio
import websockets

#======================================================================
#Register
#======================================================================
async def Register(websocket):
    remote_ip = websocket.remote_address[0] #Get client IP
    print ("IP: " + remote_ip)
    message="Hi. Your IP: " +remote_ip
    await websocket.send(message) #Say client IP
    print("==> " + str(message))

#======================================================================
#Handler
#======================================================================    
async def Handler(websocket, path):
    print("Client connect!")
    await Register(websocket)

    #try:
    async for message in websocket:
        print("<== " + str(message))

        await websocket.send(message)
        print("==> " + str(message))

    #except Exception as E:
        #print(str(E))
    #finally:
        #await asyncio.sleep(2)

#======================================================================
#MAIN
#======================================================================
print("Starting!")
start_server = websockets.serve(Handler, "0.0.0.0", 8012)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

连接后,从连接时刻算起的40秒后,会返回一个错误,客户端是否与服务器交换数据并不重要。

代码语言:javascript
运行
复制
Error in connection handler
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/websockets/protocol.py", line 827, in transfer_data
    message = await self.read_message()
  File "/usr/local/lib/python3.7/site-packages/websockets/protocol.py", line 895, in read_message
    frame = await self.read_data_frame(max_size=self.max_size)
  File "/usr/local/lib/python3.7/site-packages/websockets/protocol.py", line 971, in read_data_frame
    frame = await self.read_frame(max_size)
  File "/usr/local/lib/python3.7/site-packages/websockets/protocol.py", line 1051, in read_frame
    extensions=self.extensions,
  File "/usr/local/lib/python3.7/site-packages/websockets/framing.py", line 105, in read
    data = await reader(2)
  File "/usr/local/lib/python3.7/asyncio/streams.py", line 679, in readexactly
    await self._wait_for_data('readexactly')
  File "/usr/local/lib/python3.7/asyncio/streams.py", line 473, in _wait_for_data
    await self._waiter
concurrent.futures._base.CancelledError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/websockets/server.py", line 191, in handler
    await self.ws_handler(self, path)
  File "StartWB3.py", line 26, in Handler
    async for message in websocket:
  File "/usr/local/lib/python3.7/site-packages/websockets/protocol.py", line 439, in __aiter__
    yield await self.recv()
  File "/usr/local/lib/python3.7/site-packages/websockets/protocol.py", line 509, in recv
    await self.ensure_open()
  File "/usr/local/lib/python3.7/site-packages/websockets/protocol.py", line 812, in ensure_open
    raise self.connection_closed_exc()
websockets.exceptions.ConnectionClosedError: code = 1006 (connection closed abnormally [internal]), no reason

可能的问题是什么?提前感谢你的回答。

EN

回答 2

Stack Overflow用户

发布于 2020-05-24 02:06:51

原因是底层套接字已经关闭,websockets正在尝试取消运行Handler()的任务。您可以使用try/except块来捕获错误。

票数 1
EN

Stack Overflow用户

发布于 2020-03-25 16:34:13

根据经验,问题不在Python的WebSocket包中,而在C# WebSocket4Net:Why is WebSocket4Net closing the connection with 1011 error code

开始使用WebSocketSharp库,问题消失了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60833358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档