High-performance WebSockets for your Flask apps powered by uWSGI. Low-level uWSGI WebSocket APIaccess and flexible high-level abstractions for building complex WebSocketapplications with Flask. Supports several different concurrency modelsincluding Gevent. Inspired by Flask-Sockets.
from flask import Flask
from flask_uwsgi_websocket import GeventWebSocket
app = Flask(__name__)
websocket = GeventWebSocket(app)
@websocket.route('/echo')
def echo(ws):
while True:
msg = ws.receive()
ws.send(msg)
if __name__ == '__main__':
app.run(gevent=100)
Preferred method of installation is via pip:
$ pip install Flask-uWSGI-WebSocket
Of course you'll also need uWSGI (with SSL support, at minimum). It can also beinstalled with pip:
$ pip install uwsgi
If that fails or you need to enable the asyncio plugin, read on.
On some versions of Mac OS X, OpenSSL headers are no longer included. If youuse Homebrew, install OpenSSL and ensure they are available:
$ brew install openssl && brew link openssl --force
This should ensure pip can install uWSGI:
$ LDFLAGS="-L/usr/local/lib" pip install uwsgi --no-use-wheel
If you plan to use the asyncio plugin, you'll need to ensure that it's enabledwhen uWSGI is compiled. You can use UWSGI_PROFILE
to do this. With Homebrew Python 3.5 installed:
$ LDFLAGS="-L/usr/local/lib" CFLAGS="-I/usr/local/include/python3.5m" UWSGI_PROFLILE="asyncio" pip3 install uwsgi --no-use-wheel
If your Linux distribution includes uWSGI with specific plugins, that is manytimes your best bet. If that fails or you'd prefer to compile uWSGI yourself,you'll need to ensure that the requisite build tools, OpenSSL headers, etc areinstalled:
$ apt-get install build-essential libssl-dev python3-dev python3-venv
According to the uWSGI asyncio docs, UWSGI_PROFILE
and greenlet.h
location should be specified.
If you are installing uWSGI into a virtualenv, the process is:
$ python3 -m venv pyvenv $ . pyvenv/bin/activate (pyvenv)$ pip install greenlet
Now, greenlet.h
should be available at $VIRTUAL_ENV/include/site/python3.5
. To build with pip:
$ mkdir -p $VIRTUAL_ENV/include/site/python3.5/greenlet $ ln -s ../greenlet.h $VIRTUAL_ENV/include/site/python3.5/greenlet/ $ CFLAGS="-I$VIRTUAL_ENV/include/site/python3.5" UWSGI_PROFILE="asyncio" pip install uwsgi --no-use-wheel
You can use uWSGI's built-in HTTP router to get up and running quickly:
$ uwsgi --master --http :8080 --http-websockets --wsgi echo:app
...which is what app.run
does after wrapping your Flask app:
app.run(debug=True, host='localhost', port=8080, master=true, processes=8)
uWSGI supports several concurrency models, in particular it has nice supportfor Gevent. If you want to use Gevent, importflask_uwsgi_websocket.GeventWebSocket
and configure uWSGI to use thegevent loop engine:
$ uwsgi --master --http :8080 --http-websockets --gevent 100 --wsgi echo:app
...or:
app.run(debug=True, gevent=100)
Note that you cannot use multiple threads with gevent loop engine.
To enable asyncio instead:
$ uwsgi --master --http :5000 --http-websockets --asyncio 100 --greenlet --wsgi chat:app
...or:
app.run(debug=True, asyncio=100, greenlet=True)
For production you'll probably want to run uWSGI behind Haproxy or Nginx,instead of using the built-int HTTP router. Explore the uWSGI documentation to learn moreabout the various concurrency and deployment options.
It's possible to take advantage of Flask's interactive debugger by installingWerkzeug's DebuggedApplication
middleware:
from werkzeug.debug import DebuggedApplication app.wsgi_app = DebuggedApplication(app.wsgi_app, True)
...and running uWSGI with only a single worker:
$ uwsgi --master --http :8080 --http-websockets --wsgi-file --workers 1 --threads 8 app.py
If you use app.run(debug=True)
or export FLASK_UWSGI_DEBUG
,Flask-uWSGI-Websocket will do this automatically for you.
There are several examples available here.
WebSocket
Applies WebSocketMiddleware
to your Flask App, allowing you to decorateroutes with the route
method, turning them into WebSocket handlers.
Additionally monkey-patches app.run
, to run your app directly in uWSGI.
route(url)
run(debug, host, port, **kwargs)
**kwargs
are passed to uWSGI as command line arguments.
WebSocketMiddleware
WebSocket Middleware which automatically performs WebSocket handshake andpasses WebSocketClient
instances to your route.
WebSocketClient
Exposes the uWSGI WebSocket API.
recv()
(alias WebSocket.receive()
)
recv_nb()
send(msg)
send_binary(msg)
recv_nb()
send_from_sharedarea(id, pos)
send_binary_from_sharedarea(id, pos)
GeventWebSocket
Fancier WebSocket abstraction that takes advantage of Gevent loop engine.Requires uWSGI to be run with --uwsgi
option.
GeventWebSocketMiddleware
Automatically performs WebSocket handshake and passes aGeventWebSocketClient
instance to your route.
GeventWebSocketClient
WebSocket client abstraction with fully non-blocking methods.
receive()
send(msg)
close()
connected
AsyncioWebSocket
Fancier WebSocket abstraction that takes advantage of Asyncio loop engine.Requires uWSGI to be run with --asyncio
and --greenlet
option.
AsyncioWebSocketMiddleware
Automatically performs WebSocket handshake and passes a AsyncioWebSocketClient
instance to your route.
AsyncioWebSocketClient
WebSocket client abstraction with asyncio coroutines.
coroutine a_recv()
(alias receive()
, recv()
)
coroutine a_send(msg)
(alias send()
)
recv_nb()
(should be useless)
send_nb()
(should be useless)
close()
connected
Normally websocket routes happen outside of the normal request context. You canget a request context in your websocket handler by usingapp.request_context
:
app = Flask(__name__) ws = GeventWebSocket(app) @ws.route('/websocket') def websocket(ws): with app.request_context(ws.environ): print request.args
本文介绍Flask里面uwsgi配置使用websocket功能 1、创建websocket from flask_uwsgi_websocket import WebSocket app = create_app(DevelopConfig) try: sockets = WebSocket(app) except Exception as e: logging.error("E
使用uWSGI 和 Nginx搭建服务器发布Flask应用的完美解决方案之总结 说明:网上关于flask使用uWSGI 和 Nginx搭建配置的文章少,而且真正有价值的不多,所以本人亲自测试总结一下,结合centos7系统整理 首先我们必须要知道uWSGI和Nginx的作用,这点非常重要! uWSGI----真正跑flask应用的服务器(和Apache类似,必须有他才能访问) Nginx---
内容如题 软件环境如下: 系统版本: Ubuntu 16.04 Server版 python版本: 3.5.2 (Ubuntu自带) nginx 下载 apt install nginx uwsgi 下载 pip install uwsgi 程序根目录 /home/uma/peach_v3.1.1/peach2Produce/ 配置方法: uwsgi配
下面是案例,是我自己用来测试使用的,可以直接运行的。详细的使用请看官网 websocket主要应用于客户端和服务端双向通信的。 前端 使用socket.io.min.js是node.js的一个websocket库,首先创建socket. emit是向后端发送消息, message是该条消息的名称,后面是发送消息的数据。on是注册接受消息的事件,获取后端传过来的数据. namespace是指一类的消
概述 在Python开发的web应用中,我们通常能够看到flask、uWSGI、Nginx出现在一起,他们之间的关系是什么?为什么总是被应用在一起? 三者共同使用为了实现一个目的:客户端向服务端发送数据请求,服务端根据应用代码逻辑返回客户端需要的数据。 说明: (1)既然客户端可以请求服务端的数据,那么客户端与服务端肯定是可以通信的,通信是由谁来完成的呢?就是web server:Ngin
环境 CentOS 名词解释 flask:Flask是一个Python实现的Web开发微服务框架,相关资料,http://docs.jinkan.org/docs/flask/ uWSGI:uWSGI是一个web服务器,它实现了WSGI协议、uwsgi、http协议等 部署释疑 单独Flask也可以启动并提供web服务,但是Flask毕竟只是一个web框架,它的web服务只能用于开发环境,不能用于
一般配置ini,保存为uwsgi.ini 第一行配置中,单个uwsgi部署需要http,而不是官网上的scoket [uwsgi] http=:5000 wsgi-file=/home/urun/web/cluster_manager/run.py callable=app processes=4 threads=2 开始执行: uwsgi uwsgi.ini 停止uwsgi: pkill -f
1、安装 uWSGI 1.1)通过 pip 安装 pip install uwsgi 1.2)通过网络安装 curl http://uwsgi.it/install | bash -s default /tmp/uwsgi 1.3)通过源码安装 wget http://projects.unbit.it/downloads/uwsgi-latest.tar.gz tar zxvf uwsgi-la
背景介绍 flask是单进程的服务,在真正生产环境中使用uwsgi进行服务部署,这样可以支持高并发服务。 概念 在部署之前,我们得先了解几个概念 wsgi web应用程序之间的接口。它的作用就像是桥梁,连接在web服务器和web应用框架之间。 uwsgi 是一种传输协议,用于定义传输信息的类型。 uWSGI 是实现了uwsgi协议WSGI的web服务器。 uwsgi配置 [uwsgi] #服务端口
python代码 from flask import Flask from flask_socketio import SocketIO, emit import threading app = Flask(__name__) Socket = SocketIO(app, cors_allowed_origins="*") video_lock = threading.Lock() run_t
官网 https://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html 配置文件启动 安装库 # 安装 pip3 install uwsgi ### uwsgi --python-version ## 命令行启动 uwsgi --http :5000 --wsgi-file app.py --callable app uwsgi
简易后端 # 不需要记住 但是要知道是什么模块驱使Flask 支持Websocket # gevent-websocket # 以下代码 和 gevent-websocket 使用方式 请记录在案 from geventwebsocket.handler import WebSocketHandler from geventwebsocket.server import WSGIServer fr
flask项目实现websocket 1.from flask_socketio import SocketIO 2.app = Flask(__name__) 3.socketio = SocketIO(app) 4.启动flask项目时候要用如下方式启动 socketio.run(app, host='', port=) 5.在启动项目的同一文件下,添加如下代码 WS = [] @soc
Supported tags and respective Dockerfile links python3.9, latest (Dockerfile) python3.8, (Dockerfile) python3.7, (Dockerfile) python3.6 (Dockerfile) Discouraged tags python3.8-alpine (Dockerfile) To l
问题内容: 我在烧瓶上创建了一个端点,该端点从数据库查询(远程数据库)生成电子表格,然后将其作为下载内容发送到浏览器中。Flask不会抛出任何错误。Uwsgi没有抱怨。 但是当我检查nginx的error.log时,我看到了很多 2014/12/10 05:06:24 [错误] 14084#0:* 239436上游过早关闭连接,同时从上游读取响应头,客户端:34.34.34.34,服务器:me.c
我的应用程序有以下堆栈: Nginx(1.4.6) UWSGI(1.9.17.1-Debian(64bit)) 烧瓶 Python 3.4 NGINX重启后的问题在一段时间内(几分钟)都能正常工作。在此期间之后,我收到一个“504网关超时”错误。 NGINX日志: *13从上游读取响应标头时上游超时(110:连接超时),客户端:86.123.39.44,服务器:app.fc.com,请求:“get
到目前为止,我在Ubuntu18.04远程服务器上使用uwsgi和nginx运行了一个Flask应用程序。这个应用程序是由我的网站监听端口5002的子域服务的。我想添加一个新的Flask应用程序来监听端口5003,但我一定是做了一些错误的配置,然后一切都失控了。现在这两个应用程序都不起作用了。 app1的服务器块 null
我正在研究一个烧瓶API,它工作得很好。我现在试图用uWSGI替换Flask开发服务器,但一切都崩溃了。我试图解决这个问题,因为2天,通过教程和搜索这里,但找不到一个解决问题的方法。这是代码:app.py 当我用运行这个函数时,它工作得很好。 现在我只是尝试使用UWSGI获得相同的结果: 命令行消息看起来很好: 命令行消息 我试过所有想到的事情,但都没能解决这个问题。我做的一些事情是: > 包括以
问题内容: 我打电话给我的flask文件。 并且已经将它与uWSGI和nginx一起部署(我遵循了这些说明) 但是,当我收到错误消息时,在浏览器或uWSGI日志中没有任何调试信息。 有任何想法吗? flask_file_name.py: 问题答案: 不能将Flask的debug选项与一起使用,因为它不能在分叉环境中使用。 你会看到502,因为flask / werkzeug没有将任何数据发送到We