一、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()
二、部署方案设计
常用的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.关闭虚拟环境