Flask SocketIO&uWSGI导致“TypeError:”SocketIO“对象不可调用”

2024-04-25 22:10:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在构建一个应用程序,它使用Flask和Flask SocketIO的组合来处理HTTP和WebSocket流量的组合。在服务器上部署应用程序并添加uWSGI和Nginx作为网关后,wsgi脚本在日志中显示以下错误:

TypeError: 'SocketIO' object is not callable
[pid: 4262|app: 0|req: 3/8] XXX.XXX.XXX.XXX () {46 vars in 912 bytes} [Tue Jul 11 14:57:25 2017] GET / => generated 0 bytes in 0 msecs (HTTP/2.0 500) 0 

uWSGI documentation中所述,我在配置文件中将set http-websockets标志添加到true。Flask SocketIO文档声明他们支持uWSGI:

The other alternative is to use the uWSGI web server, which comes with WebSocket functionality. The use of gevent is also a performant option, but slightly lower than eventlet.

有人知道我做错了什么吗?我使用的是Nginx的最新版本。提前谢谢你的帮助。我的设置:

烧瓶服务器(mymod.服务器)在

^{pr2}$

WSGI脚本(项目的根目录)

from mymod.server import io

if __name__ == "__main__":
    io.run(host="127.0.0.1", port=8080)

WSGI配置

# Configuration file for Nginx
[uwsgi]
module = wsgi:io
master = true
processes = 5
buffer-size=32768
http-websockets = true
socket = server.sock
chmod-socket = 666
vacuum = true
die-on-term = true
logto = /var/log/uwsgi/%n.log

Nginx配置

server {
    listen 443 http2 ssl;
    listen [::]:443 http2 ssl;

    server_name _;
    server_tokens off;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/user/project/server.sock;
    }

    location /socket.io/ {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass http://unix:/home/user/project/server.sock;
    }
}

如何启动服务器

$ uwsgi --ini config.wsgi.ini

Tags: io服务器truehttpflaskwsgiserveris
1条回答
网友
1楼 · 发布于 2024-04-25 22:10:20

原来Flask SocketIO只是安装到原始的app对象上,替换了以下内容:

[uwsgi]
module = wsgi:io

收件人:

^{pr2}$

可能会奏效,但据我所知,uwsgi对SocketIO的支持仍然很糟糕,这使得gunicorn成为一个更好的选择。在

相关问题 更多 >