option process
download
yum install python3
yum install pip3
pip3 install requests
pip3 install requests==2.0.0
pip3 install https://github.com/pypa/virtualenv/tarball/1.9.X
pip3 install -E venv requests # 为*venv* 虚拟环境安装 requests
pip3 uninstall requests
pip3 install --upgrade requests
pip3 search requests
pip3 freeze > packageList.txt
pip3 install -r packageList.txt
虚拟环境
# demo.py
pip3 install virtualenv
virtualenv my_app # 创建my_app环境
virtualenv --python=/opt/python-3.3/bin/python my_app # 指定解释器
source my_app/bin/activate # 激活虚拟环境
运行WEB
from flask import Flask, request, jsonify
from gevent import pywsgi
# from settings import APP_PORT
# 创建一个服务
app = Flask(__name__)
@app.before_request
def before_request():
print('Before Request')
@app.after_request
def after_request(response):
print('After Request')
return response
# @app.errorhandler(404)
# def page_not_found(e):
# return 'This page does not exist', 404
@app.errorhandler(Exception)
def page_not_found(e):
return '服务器异常,请稍后重试', 500
@app.route("/")
def hello():
return "Hello World!"
@app.route("/user/<username>")
def username(username):
return f"Hello {username}!"
@app.route("/get/user")
def getUser():
username = request.args.get("username")
return f"Hello {username}!"
# 创建一个接口 指定路由和请求方法 定义处理请求的函数
@app.route(rule='/demo/print', methods=['POST'])
def demo():
body = request.get_json()
response = {
'isSuccess': True,
'data': body.get('name'),
'msg': '成功'
}
return response
if __name__ == '__main__':
# 启动服务 指定主机和端口
app.run(host='127.0.0.1', port=8000)
# server = pywsgi.WSGIServer(('127.0.0.1', 8000), app)
# print('server is running...')
# server.serve_forever()
pip3 install gunicorn
vim gunicorn.conf.py # 新建配置文件
nohup gunicorn -c gunicorn.conf.py --name=demo demo:app &
# gunicorn.conf.py
bind = '0.0.0.0:8888'
workers = 1
threads = 1
reload=True