Running a python flask application on windows.

huangapple go评论70阅读模式
英文:

Running a python flask application on windows

问题

我有一个Python Flask应用程序,我之前在Linux上运行它没有问题,但最近需要在Windows上运行它,我遇到了一个问题。在尝试连接服务器时:

从flask导入Flask
从flask_socketio导入SocketIO

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret_key'
socketio = SocketIO(app=app, logging='DEBUG')

@socketio.on_error_default
def error_handler(e):
    print('发生错误:', e)

@app.route('/')
def index():
    return '你好,Socket.IO客户端!'

if __name__ == '__main__':
    socketio.run(app, port=5000)

使用这样的Socket.IO客户端:

导入socketio
导入日志

sio = socketio.Client()
logging.basicConfig(level=logging.DEBUG)

@sio.event
def connect():
    print('连接到服务器')

@sio.event
def disconnect():
    print('与服务器断开连接')

sio.connect('http://localhost:5000')

我得到错误消息:"socketio.exceptions.ConnectionError: 一个或多个命名空间连接失败"。

在Ubuntu上运行完全相同的代码时不会发生这种情况。我正在失去理智,所以这里的任何帮助都将受到非常感激,即使只是验证错误在您的计算机上是否会复现。

Python版本为3.9.13
包版本如下:
- Flask==2.0.1
- flask-restplus==0.13.0
- Flask-SocketIO==5.1.0
- python-engineio==4.2.0

此外,尽管我相当确定它在这里没有引起任何问题,但python-socketio==5.3.0

附加信息:
- 在Windows 10和11上测试过
- 对localhost:5000进行Curl返回消息0{"sid":"vOhTP9YKne00Ixa5AAAC","upgrades":["websocket"],"pingTimeout":20000,"pingInterval":25000},所以我相当确定这是一个客户端的问题
- 关闭防火墙不起作用
- 在不同端口上运行不起作用
- 以管理员身份运行不起作用
- 使用flask run而不是python不起作用
- 在Windows主机机器上运行的Linux Docker容器可以正常工作,没有任何问题
- 我完全没有触碰过网络设置或进行任何端口转发
英文:

I have a python flask application I've been running on linux without issue, but recently it's become a requirement to run it on windows and I've run into a problem. When trying to connect to a server like this:

from flask import Flask
from flask_socketio import SocketIO

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret_key'
socketio = SocketIO(app=app, logging='DEBUG')

@socketio.on_error_default
def error_handler(e):
    print('An error occurred:', e)

app.route('/')
def index():
    return 'Hello, Socket.IO Client!'

if __name__ == '__main__':
    socketio.run(app, port=5000)

with a socketio client like this:

import socketio
import logging

sio = socketio.Client()
logging.basicConfig(level=logging.DEBUG)

@sio.event
def connect():
    print('Connected to server')

@sio.event
def disconnect():
    print('Disconnected from server')

sio.connect('http://localhost:5000')

I get the error "socketio.exceptions.ConnectionError: One or more namespaces failed to connect".

This doesn't happen when running the exact same code on ubuntu. I'm losing my mind rn so any help here would be appreciated beyond words, even if it's just verifying that the error reproduces on your machine.


Python is version 3.9.13
Package versions are as follows:

  • Flask==2.0.1
  • flask-restplus==0.13.0
  • Flask-SocketIO==5.1.0
  • python-engineio==4.2.0

And also, though I'm fairly sure it's not causing any issues here,
python-socketio==5.3.0

Additional Info:

  • Tested for windows 10 & 11
  • Curling localhost:5000 returns the message 0{"sid":"vOhTP9YKne00Ixa5AAAC","upgrades":["websocket"],"pingTimeout":20000,"pingInterval":25000} so I'm fairly sure it's an issue with the client
  • Running without the firewall doesn't help
  • Running on a different port doesn't help
  • Running as admin doesn't help
  • Running using flask run instead of python doesn't help
  • Running on a linux docker container running on a windows host machine works with no issues
  • I haven't touched the network settings at all or done any kind of port forwarding

Full client log:

DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): localhost:5000
DEBUG:urllib3.connectionpool:http://localhost:5000 "GET /socket.io/?transport=polling&EIO=4&t=1688031616.8050513 HTTP/1.1" 200 None
websocket-client package not installed, only polling transport is available
ERROR:engineio.client:websocket-client package not installed, only polling transport is available
DEBUG:urllib3.connectionpool:Resetting dropped connection: localhost
DEBUG:urllib3.connectionpool:Starting new HTTP connection (2): localhost:5000
Traceback (most recent call last):
  File "C:\Users\Matt\source\repos\data-art\hg-backend\Client.py", line 15, in <module>
    sio.connect('http://localhost:5000')
  File "C:\Users\Matt\source\repos\data-art\hg-backend\venv\lib\site-packages\socketio\client.py", line 323, in connect
    raise exceptions.ConnectionError(
socketio.exceptions.ConnectionError: One or more namespaces failed to connect
DEBUG:urllib3.connectionpool:http://localhost:5000 "POST /socket.io/?transport=polling&EIO=4&sid=XDn3Nhf3Wlaepe5fAAAA HTTP/1.1" 200 None
DEBUG:urllib3.connectionpool:http://localhost:5000 "GET /socket.io/?transport=polling&EIO=4&sid=XDn3Nhf3Wlaepe5fAAAA&t=1688031618.8611357 HTTP/1.1" 200 None
Connected to server

Full server log:

 * Serving Flask app 'Server' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [29/Jun/2023 10:40:18] "GET /socket.io/?transport=polling&EIO=4&t=1688031616.8050513 HTTP/1.1" 200 -
127.0.0.1 - - [29/Jun/2023 10:40:20] "POST /socket.io/?transport=polling&EIO=4&sid=XDn3Nhf3Wlaepe5fAAAA HTTP/1.1" 200 -
127.0.0.1 - - [29/Jun/2023 10:40:20] "GET /socket.io/?transport=polling&EIO=4&sid=XDn3Nhf3Wlaepe5fAAAA&t=1688031618.8611357 HTTP/1.1" 200 -
127.0.0.1 - - [29/Jun/2023 10:41:20] "GET /socket.io/?EIO=4&transport=polling HTTP/1.1" 200 -

答案1

得分: 0

尝试过这个吗?

sio.connect('http://localhost:5000', wait_timeout = 10)

英文:

Have you tried this?

sio.connect('http://localhost:5000', wait_timeout = 10)

huangapple
  • 本文由 发表于 2023年6月29日 18:32:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定