Flask打包服务器部署全攻略

首先安装pyinstaller

[root@iz2ze68jgcyvh5yd31e8p5z ~]# pip install pyinstaller
Collecting pyinstaller
  Downloading http://mirrors.aliyun.com/pypi/packages/e2/c9/0b44b2ea87ba36395483a672fddd07e6a9cb2b8d3c4a28d7ae76c7e7e1e5/PyInstaller-3.5.tar.gz (3.5MB)
    100% |████████████████████████████████| 3.5MB 84.3MB/s 
Requirement already satisfied: setuptools in /usr/lib/python2.7/site-packages (from pyinstaller)
Collecting altgraph (from pyinstaller)
  Downloading http://mirrors.aliyun.com/pypi/packages/0a/cc/646187eac4b797069e2e6b736f14cdef85dbe405c9bfc7803ef36e4f62ef/altgraph-0.16.1-py2.py3-none-any.whl
Collecting dis3 (from pyinstaller)
  Downloading http://mirrors.aliyun.com/pypi/packages/9c/5c/4a4a2802f10f558018413990a58fd3dd7ed1eb48e6de7266334c2489bad6/dis3-0.1.3-py2-none-any.whl
Installing collected packages: altgraph, dis3, pyinstaller
  Running setup.py install for pyinstaller ... done
Successfully installed altgraph-0.16.1 dis3-0.1.3 pyinstaller-3.5

 

如果安装报错或者运行不起来

解决方法:手动安装

去官网下载pyinstaller安装包:https://pypi.org/project/PyInstaller/#files

然后解压,用cmd进入到解压文件夹(一定要把cmd导入到解压后的文件夹)

 

解压完成后进入文件夹执行:

tar -xzvf xx.tar.bz2 
python setup.py install


 

建立一个flask项目

缺什么就pip install 比如flask tornado gevent什么的

web.py 

# from gevent import monkey
# from gevent import pywsgi
# monkey.patch_all()
from flask import *
app=Flask(__name__)

@app.route('/')
def index():
    return '

tornado server发布成功!

' if __name__ == '__main__': # app.debug = True # server = pywsgi.WSGIServer( ('127.0.0.1', 5000 ), app ) # server.serve_forever() app.run(host='0.0.0.0',port=5000, debug=False)

home.py

# coding=utf-8
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from web import app

if __name__ == '__main__':
    http_server = HTTPServer(WSGIContainer(app))
    http_server.listen(8000)
    IOLoop.instance().start()

两个入口都可以打包 打包web是原生 home是tornado

pyinstaller -F home.py

dist目录就生成了一个home.exe 在linux下就是home.bin 直接运行就OK拉 比docker部署还方便

你可能感兴趣的:(Python应用)