conda+Flask+gunicorn+Nginx开发web-api

7 篇文章 1 订阅

正文

参考https://blog.csdn.net/llwang_10/article/details/80251270

1.各部分作用

1)conda
conda提供了python运行的虚拟环境管理以及包管理,如果服务器上有多个环境需求.使用conda保证环境的干净整洁很重要

2)flask
Flask是一个使用 Python 编写的轻量级 Web 应用框架,用来开发python版的web-api

3)gunicorn
gunicorn是一个Web服务器,它实现了WSGI协议

4)nginx
Nginx是一个提供静态文件访问的web服务,然而,它不能直接执行托管Python应用程序,而gunicorn解决了这个问题。让我们在虚拟环境中安装gunicorn,稍候再配置Nginx和gunicorn之间的交互。

2.环境搭建

2-1.安装conda并创建独立虚拟环境

# 安装过程就略了
# 创建虚拟环境python_web
conda create -n python_web python=3.7.3

2-2.进入虚拟环境并安装flask支持

# 进入虚拟环境
conda activate python_web
# 安装flask-restful支持
pip install flask-restful

ps:基于以上两步就可以单独开发python版的web-api了
参考:http://www.pythondoc.com/Flask-RESTful/quickstart.html#

2-3.api开发例子

编写app.py

from flask import Flask
# 由于api更新,这里做了一些调整
import flask_restful as restful

app = Flask(__name__)
api = restful.Api(app)

class HelloWorld(restful.Resource):
    def get(self):
        return {'hello': 'world'}

api.add_resource(HelloWorld, '/')

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=8000)

单独在虚拟环境中运行下
python3 app.py

* Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 301-920-422

在浏览器中访问
http://127.0.0.1:8000/

ps:可以看到已经可以跑了,这里是启动的是调试服务器,需要部署到gunicorn

2-4.安装 gunicorn搭载web项目

pip install gunicorn

测试过程:
在app.py同目录下创建文件wsgi.py调用app

#wsgi.py,与app.py在同一个目录下
from app import app

if __name__ == "__main__":
    app.run()

在终端运行

gunicorn --bind 127.0.0.1:8000 wsgi:app

当代码更新后需要重启gunicorn,刷新页面

ps -ef|grep gunicorn
kill -9 [pid]

2-5.安装nginx连通gunicorn服务器

1)安装步骤略过
2)配置nginx
location不知道为啥各种配置不成功  
仅有
location /
location ~* ^.+$
location /50x.html
生效

我使用 location = / location ~ 等均无法做到二级目录
这时候只能在根目录里配置转发127.0.0.1:8000, 二级目录的事情交给它来做好了

 location / {
                proxy_pass http://127.0.0.1:8000;
                proxy_redirect     off;
                proxy_set_header   Host                 $http_host;
                proxy_set_header   X-Real-IP            $remote_addr;
                proxy_set_header   X-Forwarded-For      $proxy_add_x_forwarded_for;
                proxy_set_header   X-Forwarded-Proto    $scheme;
        }

修改app.py

from flask import Flask, jsonify, abort, request,render_template
# 由于api更新,这里做了一些调整
import flask_restful as restful

app = Flask(__name__)
api = restful.Api(app)

root = '/monitor'
# 配置页面
@app.route('%s' % root)
def index():
    return render_template('index.html')

test = [
    {
        'id': 1,
        'title': u'论语',
        'auther': u'孔子',
        'price': 18
    },
    {
        'id': 2,
        'title': u'道德经',
        'auther': u'老子',
        'price': 15
    }
]
# 配置restful接口
@app.route('%s/api/v1/test' % root, methods=['GET'])
def get_tasks():
    return jsonify({'test': test})


if __name__ == '__main__':
    app.run(debug=True)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值