使用websocket-client连接时出现WinError 10057

时间:2019-10-18 18:43:41

标签: python python-3.x flask websocket flask-socketio

我正在制作Flask Web应用程序,并且正在使用Flask-Socketio。由于各种原因,我还需要使用websocket-client软件包。一切都按预期进行,但是当我尝试在不同网络上的另一台计算机上运行该应用程序时,出现以下错误:

"""
Traceback (most recent call last):
  File "[Path to venv]\venv\lib\site-packages\flask\app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "[Path to venv]\venv\lib\site-packages\flask\app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "[Path to venv]\venv\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "[Path to venv]\venv\lib\site-packages\flask\app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "[Path to venv]\venv\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "[Path to venv]\venv\lib\site-packages\flask\app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "[Path to venv]\venv\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "[Path to venv]\venv\lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "[Path to venv]\venv\lib\site-packages\flask\app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "[Path to app]\app\views.py", line 7, in index
    sio.connect("http://localhost:80/", transports=['websocket', 'polling'])
  File "[Path to venv]\venv\lib\site-packages\socketio\client.py", line 262, in connect
    engineio_path=socketio_path)
  File "[Path to venv]\venv\lib\site-packages\engineio\client.py", line 170, in connect
    url, headers, engineio_path)
  File "[Path to venv]\venv\lib\site-packages\engineio\client.py", line 346, in _connect_websocket
    cookie=cookies)
  File "[Path to venv]\venv\lib\site-packages\websocket\_core.py", line 514, in create_connection
    websock.connect(url, **options)
  File "[Path to venv]\venv\lib\site-packages\websocket\_core.py", line 223, in connect
    options.pop('socket', None))
  File "[Path to venv]\venv\lib\site-packages\websocket\_http.py", line 120, in connect
    sock = _open_socket(addrinfo_list, options.sockopt, options.timeout)
  File "[Path to venv]\venv\lib\site-packages\websocket\_http.py", line 189, in _open_socket
    raise error
  File "[Path to venv]\venv\lib\site-packages\websocket\_http.py", line 172, in _open_socket
    sock.connect(address)
OSError: [WinError 10057] A request to send or receive data was disallowed because 
the socket is not connected and (when sending on a datagram socket using a sendto call) 
no address was supplied
"""

我已将代码尽可能地简化为以下代码,这些代码仍可在我的计算机上运行,​​但在另一端却给出相同的错误:

|start.py
|app
    |__init__.py
    |views.py
    |templates
        |index.html
# __init__.py

from flask import Flask
from flask_socketio import SocketIO
from gevent import monkey

monkey.patch_all()

APP = Flask(__name__)
SOCKETIO = SocketIO(APP, engineio_logger=True, logger=True)

from . import views
# views.py

from app import APP
from socketio import Client
from flask import render_template

@APP.route('/', methods=['GET', 'POST'])
def index():
    sio = Client()
    sio.connect("http://localhost:80", transports=['websocket', 'polling']) # Error being caused here
    return render_template('index.html')
# start.py

from app import APP, SOCKETIO

if __name__ == "__main__":
    SOCKETIO.run(APP, debug=True, port=80, host='0.0.0.0')

index.html只是基本的“ Hello World” html页面。

什么样的东西可能会在一个计算机/网络上给我带来这个错误,而在另一台计算机/网络上却给我这个错误,尤其是仅在localhost:80上运行它?我真的不知道该怎么办。

编辑:向错误添加了追溯数据

编辑2:在我的实际代码中,websocket.Client正在Celery任务中运行。我之所以没有将其包含在此处,是因为该错误可以重现,而不会引起太多复杂性。

1 个答案:

答案 0 :(得分:6)

问题是,当您尝试同时运行-( Flask-webapp websocket-client )-$(document).on('click', '.phone-link', function(ev) { ev.preventDefault(); var $this = $(this); $this.text(formatPhone( $this.attr('href') )); }); ,其中both of them将运行。


更新

这里需要注意一些事情:

  • 如果要更换机器,请确保系统软件已经是最新的。
  • 检查您的系统不在防火墙后面,但是当您在同一台计算机上运行Flask webapp和websocket-client时,则无需执行此步骤。
  • 尝试在更新操作系统后重新启动计算机。
  • 使用only one代替multiprocessing,因为它们在这里不起作用,因为著名的Python-Global-Interpreter or GIL。根据哪个Threads 执行的开发人员看不到GIL的影响 单线程程序,但是它可能是-性能瓶颈-在 CPU绑定和多线程代码。
  • 然后检查:How to run flask on Port:80Only one thread can be in a state of execution at any point in time. 端口:5000 For development purposes

  • 为了执行is mostly recommended.,请访问flask-socketio文档,其中有一些很好的技巧来处理异常。

您可以按照以下步骤进行操作:

"Error Handling"
  • 最后,要处理@socketio.on_error_default # handles all namespaces without an explicit error handler def default_error_handler(e): pass ,您还可以使用以下代码来处理最终错误:套接字超时:

    socket.timeout

    socket.error除外为socketerror:     print(“ Error:”,socketerror)

  • 使用try: socketio.run(app,... 是更好的方法。出于postman的目的,请尝试使用learning and testing,这是发送POSTMAN的快速简便的平台,RESTSOAP的请求直接在Postman中进行。访问参考:getting-started-postman

这是一个示例程序:

GraphQL