Flask应用部署

一、WSGI

  • 全名:Python Web Server Gateway Interface
  • 定义了Web服务器和Web应用程序之间通信的接口规范。

这里写图片描述

  • WSGI应用:是一个接受两个参数的可调用对象。
  • 两个参数:
    1.environ参数是个字典对象 ,包含CGI风格的环境变量。
    2.start_response参数是一个接受两个固定参数和一个可选参数的可调用者。
  • WSGI服务器:为每一个HTTP请求调用WSGI应用。
  • 简单的服务器实现:
    wsgiref—WSGI Utilities and Reference Implementtation(WSGI工具和参考实现)

简单WSGI代码例子:

#wsgi app
def application(environ,start_response):
    response_body="Hellow World"
    header=[('Content-Type','text/html')]
    status='200 OK'

    start_response(status,header)

    return [response_body]

if __name__=="__main__":
    from wsgiref.simple_server import make_server
    httpd=make_server("0.0.0.0",8080,application)
    print "httpd run on :"+str(httpd.server_port)
    httpd.serve_forever()

eg

二、部署方案设计
常用的WSGI服务器:
1. Gunicorn
2. uWSGI
3. CherryPy
4. Tornado
5. Gevent
6. mod_wsgi(Apache)

Web服务器:
Nginx是一款面向性能设计的HTTP服务器,相较于Apache、lighttpd具有占有内存少,稳定性高等优势。

部署方案设计:
这里写图片描述

三、部署工具安装与使用:

  • Virtualenv简介:用于创建独立的Python运行环境
  • 解决问题:
    1.版本问题
    2.依赖问题
    3.权限问题:不用管理员权限就能进行安装。
  • Virtualenv安装:sudo pip install virtualenv
  • 使用:
    1.创建虚拟环境
    2.激活虚拟环境
    3.关闭虚拟环境

猜你喜欢

转载自blog.csdn.net/joker_fei/article/details/70941878