bottle框架学习(八)网站程序部署(bottle+nginx+waitress/tornado)

部署的原因

  • run函数运行的服务器是单进程的服务器(支持开发调式)
  • 提高服务器的并发处理能力

    • 多线程部署

    • 多进程部署

    • 异步框架部署

bottle框架支持的部署框架

bottle框架支持的部署框架很多,常用的部署框架:
waitress
wsgiref
rocket
gevent
tornado
twisted

常见网站项目基本目录结构

-static
—-img 图片文件
—-css 样式表文件
—-js js代码文件
-template HTML模板文件
manage.py

网站部署实例

waitress 部署(多线程框架)

app.run(server=’waitress’,port=8088)

  • 前端用nginx作为后台部署的反向代理
    这里写图片描述
  • 在这里解释一下什么是正向代理和反向代理(知乎大神的回答)
  • 我们常说的代理也就是只正向代理,正向代理的过程,它隐藏了真实的请求客户端服务端不知道真实的客户端是谁,客户端请求的服务都被代理服务器代替来请求,某些科学上网工具扮演的就是典型的正向代理角色。用浏览器访问http://www.google.com 时,被残忍的block,于是你可以在国外搭建一台代理服务器让代理帮我去请求google.com,代理把请求返回的相应结构再返回给我。
    这里写图片描述
  • 反向代理隐藏了真实的服务端,当我们请求www.baidu.com 的时候,就像拨打10086一样,背后可能有成千上万台服务器为我们服务,但具体是哪一台,你不知道,也不需要知道,你只需要知道反向代理服务器是谁就好了,www.baidu.com 就是我们的反向代理服务器,反向代理服务器会帮我们把请求转发到真实的服务器那里去。Nginx就是性能非常好的反向代理服务器,用来做负载均衡。
    这里写图片描述
    这里写图片描述

  • 安装waitress框架

pip install waitress
  • 建立网站主程序

from bottle import route,run,template
import sys

@route('/')
def index():
    return template('index')#在views文件夹下,不需要指定相对路径及后缀

port = '8088'
if len(sys.argv) == 2:
    port = sys.argv[1]
run(server='waitress',port=port,debug=True,reloader=True)

views文件夹下查看index.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>bottle</title>
</head>
<body>
   <h2>项目部署测试主页</h2>
   <img src="0.jpg" />
   <img src="img.gif" />
</body>
  • 运行网站服务器的后台
终端启动三个后台进程来处理动态请求
>python bottle_16.py
Bottle v0.12.13 server starting up (using WaitressServer())...
Listening on http://127.0.0.1:8088/
Hit Ctrl-C to quit.

>python bottle_16.py 8082
Bottle v0.12.13 server starting up (using WaitressServer())...
Listening on http://127.0.0.1:8082/
Hit Ctrl-C to quit.

>python bottle_16.py 8081
Bottle v0.12.13 server starting up (using WaitressServer())...
Listening on http://127.0.0.1:8081/
Hit Ctrl-C to quit.
  • 下载nginx,解压
配置nginx.conf
在http这个节点下面增加
  upstream myserver{
        ip_hash;
        server  localhost:8088;
        server  localhost:8081;
        server  localhost:8082;
    }

在server这个节点中写入
  location / {
            root   html;
            index  index.html index.htm;
            proxy_pass  http://myserver;#myserver对应后端建立的myserver
        }

静态文件映射到指定的位置
location ~ \.(jpg|jpeg|png|gif)$ {
            root  d:/PycharmProjects/python_code_scq/python_bottle/static/img;
        }

        location ~ \.js$ {
            root  d:/PycharmProjects/python_code_scq/python_bottle/static/js;
        }

        location ~ \.css$ {
            root  d:/PycharmProjects/python_code_scq/python_bottle/static/css;
        }
  • 终端操作nginx
#查看配置是否正确
nginx -t
nginx: the configuration file D:\PycharmProjects\python_code_scq\python_bottle\nginx/conf/nginx.conf syntax is
ok
nginx: configuration file D:\PycharmProjects\python_code_scq\python_bottle\nginx/conf/nginx.conf test is succes
sful

#启动
#建议使用第一种,第二种会使你的cmd窗口一直处于执行中,不能进行其他命令操作
start nginx
nginx.exe


#停止
#stop是快速停止nginx,可能并不保存相关信息;quit是完整有序的停止nginx,并保存相关信息。
nginx.exe -s stop
nginx.exe -s quit

#重新载入Nginx
nginx.exe -s reload

#如果出现错误,查看error.log文件
  • 打开浏览器测试
    这里写图片描述
tornado 部署(异步框架)

app.run(server=’tornado’,port=8088)

  • 下载安装tornado框架
pip install tornado
Requirement already satisfied: tornado in d:\anaconda3\lib\site-packages

步骤同上

猜你喜欢

转载自blog.csdn.net/sunchengquan/article/details/79571281