python自动化测试(五)--nginx+uwsgi+flask搭建web服务器

1.web服务基本流程

1)client
首先客户端请求服务资源,
2)nginx
nginx作为直接对外的服务接口,接收到客户端发送过来的http请求,会解包、分析,
如果是静态文件请求就根据nginx配置的静态文件目录,返回请求的资源,
如果是动态的请求,nginx就通过配置文件,将请求传递给uWSGI;
3)uWSGI
uWSGI 将接收到的包进行处理,并转发给wsgi,
4)wsgi
wsgi根据请求调用django, flask工程的某个文件或函数,
5)web application
处理完后django,flask将返回值交给wsgi,
6)wsgi
wsgi将返回值进行打包,转发给uWSGI,
7)uWSGI
uWSGI接收后转发给nginx,
8)nginx
nginx最终将返回值返回给客户端(如浏览器)。

2.nginx配制

  • flask_nginx.conf
  • sudo ln -s /home/wyz/flask_nginx.conf /etc/nginx/conf.d/
  • service nginx start/stop/restart
  • ps -ef | grep nginx
 server {
        listen       80;         //默认的web访问端口
        server_name  xxxxxx;     //服务器名
        #charset koi8-r;
        access_log  /home/wyz/flask/logs/access.log;    //服务器接收的请求日志,logs目录若不存在需要创建,否则nginx报错
        error_log  /home/wyz/flask/logs/error.log;         //错误日志

        location / {
            include        uwsgi_params;     //这里是导入的uwsgi配置
            uwsgi_pass     127.0.0.1:5051;   //需要和uwsgi的配置文件里socket项的地址
                                             //相同,否则无法让uwsgi接收到请求。
            uwsgi_param UWSGI_CHDIR  /home/wyz/flask;     //项目根目录
            uwsgi_param UWSGI_PYTHON /home/wyz/flask/env36 //python虚拟环境
            uwsgi_param UWSGI_SCRIPT manage:app;     //启动项目的主程序(在本地上运行
                                                     //这个主程序可以在flask内置的
                                                     //服务器上访问你的项目)
}
}

3.uWSGI配制

  • flask_uwsgi.ini
  • uwsgi --ini /home/wyz/flask/flask_uwsgi.ini
  • ps -ef | grep uwsgi
[uwsgi]
socket = 127.0.0.1:5051
#http = 127.0.0.1:5051
pythonpath = /home/wyz/flask
module = manage
wsgi-file = /home/wyz/flask/manage.py
callable = app
master = true
processes = 4
#threads = 2
daemonize = /home/wyz/flask/server.log

4.flask web应用

实现接收上传文件,并存放在upload目录下

from werkzeug.utils import secure_filename
from flask import Flask,render_template,jsonify,request
import time
import os
import base64
 
app = Flask(__name__)
UPLOAD_FOLDER='upload'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
basedir = os.path.abspath(os.path.dirname(__file__))
ALLOWED_EXTENSIONS = set(['txt','png','jpg','xls','JPG','PNG','xlsx','gif','GIF'])
 
# 用于判断文件后缀
def allowed_file(filename):
    return '.' in filename and filename.rsplit('.',1)[1] in ALLOWED_EXTENSIONS
 
# 用于测试上传,稍后用到
@app.route('/test/upload')
def upload_test():
    return render_template('upload.html')
 
# 上传文件
@app.route('/api/upload',methods=['POST'],strict_slashes=False)
def api_upload():
    file_dir=os.path.join(basedir,app.config['UPLOAD_FOLDER'])
    if not os.path.exists(file_dir):
        os.makedirs(file_dir)
    f=request.files['myfile']  # 从表单的file字段获取文件,myfile为该表单的name值
    if f and allowed_file(f.filename):  # 判断是否是允许上传的文件类型
        fname=secure_filename(f.filename)
        print fname
        ext = fname.rsplit('.',1)[1]  # 获取文件后缀
        unix_time = int(time.time())
        new_filename=str(unix_time)+'.'+ext  # 修改了上传的文件名
        f.save(os.path.join(file_dir,new_filename))  #保存文件到upload目录
        token = base64.b64encode(new_filename)
        print token
 
        return jsonify({"errno":0,"errmsg":"上传成功","token":token})
    else:
        return jsonify({"errno":1001,"errmsg":"上传失败"})
 
if __name__ == '__main__':
    app.run(debug=True)

你可能感兴趣的:(python自动化测试(五)--nginx+uwsgi+flask搭建web服务器)