python bottle服务部署(tornado/ uwsgi
2018-06-27 本文已影响0人
原来不语
本次通过bottle , tornado, uwsgi+ nginx分别来部署bottle项目
【1】通过bottle自带的服务
先安装bottle框架,通过pip3命令,若没有pip3自行安装
sudo pip3 install bottle
创建myapp.py 文件来测试。
from bottle import get,run
@get("/")
def hello():
return "hello"
run(host="0.0.0.0", port=8080)
输入python3 myapp.py然后浏览器里输入地址


通过apache2-utils的ab命令进行压力测试
sudo apt install apache2-utils
ab -n 1000 -c 40 http://192.168.146.128:8080/

使用bottle自带的run比较脆弱
【2】使用 tornado来进行部署
安装tornado
sudo pip3 install tornado
创建myapp.py 文件来测试。
from bottle import get,run
@get("/")
def hello():
return "hello"
run(host="0.0.0.0", port=8080,server="tornado")
添加server="tornado"后可以自动使用tornado来部署bottle

进行同样的压力测试

【3】通过uwsgi+nginx进行部署
编写myapp.py
from bottle import get, default_app
@get("/")
def hello():
return "hello"
application = default_app()
安装uwsgi 和 nginx
sudo apt install uwsgi
sudo apt install nginx
配置uwsgi和nginx
在uwsgi的/etc/uwsgi/apps-available 下创建myapp.ini文件
[uwsgi]
socket = 127.0.0.1:9000
chdir= /home/bo
master = true
plugins= python3
file = myapp.py
uid = www-data
gid= www-data
软连接到上一级目录的/apps-enabled下
ln -s /etc/uwsgi/apps-available/myapp.py /etc/uwsgi/apps-enabled/myapp.py
然后启动uwsgi
sudo service uwsgi start

9000端口已经启动
配置nginx
切换到 /etc/nginx/sites-available/创建配置文件myapp.conf
server{
listen [::]:80;
listen 80;
server_name 192.168.146.128
root /home/bo;
location / {
try_files $uri @uwsgi;
}
location @uwsgi{
include uwsgi_params;
uwsgi_pass 127.0.0.1:9000;
}
}
同样软连接到上一目录下的sites-enabled下
重新启动服务
sudo service nginx restart
sudo service uwsgi restert
查看端口服务
netstat -ntlp

进行测试:
