標籤:flask tornado
1、Flask的入口程式為manage.py,代碼如下:
#coding=utf-8#!/usr/bin/python
from somewhere import app #somewhere 表示的包含Flask的執行個體,如app = Flask(__name__)if __name__ == "__main__": app.run(debug=True)
2、在manage.py的同級目錄添加tornado應用程式tornado_server.py來託管manage.py,代碼如下:
#coding=utf-8#!/usr/bin/pythonfrom tornado.wsgi import WSGIContainerfrom tornado.httpserver import HTTPServerfrom tornado.ioloop import IOLoopfrom run import apphttp_server = HTTPServer(WSGIContainer(app))http_server.bind(5000, "0.0.0.0") # 對外開啟訪問http_server.start(1)# http_server.listen(5000) #或者只監聽127.0.0.1的預設連接埠IOLoop.instance().start()
3.如果直接運行python tornado_server.py會正常運行,輸出如下:
* Running on http://127.0.0.1:5000/
* Restarting with reloader
但是在生產環境中得部署到效能好的nginx上,nginx配置如下:
server {
listen 80;
server_name abc.com;
rewrite ^(.*) http://www.abc.com$1 permanent;
}
server{
listen 80;
#listen [::]:80 default_server;
#access_log /var/log/nginx/win2003_access.log main;
#include header_proxy.inc;
server_name www.abc.com;
#root /var/www/abc;
location / {
#index index.html index.htm index.php;
#include uwsgi_params;
#uwsgi_pass unix:/tmp/uwsgi.sock;
proxy_pass http://localhost:5000; #關鍵一點就是這裡,意思是所有對http://www.abc.com:80的訪問都會重新導向到原生5000連接埠上
}
}
別的一些最佳化比如靜態檔案暫沒有讓 nginx代理
配置好後只需要重載一下nginx 就可以生效:
Windows環境將Flask應用程式部署在tornado+nginx的簡單方法