wsgi+webob+routes学习笔记-初学篇(上)

来源:互联网 发布:分润管理系统源码 编辑:程序博客网 时间:2024/04/29 10:36

1      初识WSGI:

Wsgi是什么?Python Web服务网关接口? 可以先看下它不是什么:What WSGI¹ is not: a server, a python module, aframework, an API or any kind of software.它仅仅是一个web server和application之间的一个接口定义(http://webpython.codepoint.net/wsgi_tutorial),除了在PEP 3333之中,哪也不存在。Wsgi个人理解就是通过wsgi server收到web请求(http),然后把这个请求转给用户自定义的application去处理,并将执行结果返回。

2      Openstack中的wsgi

Openstack中使用了evenlet库提供的wsgi实现,evenlet是python的一个网络编程库(http://eventlet.net/),提供了许多有用的实现,包括协程,wsgi等,是网络并发编程的利器

以cinder为例,在cinder的cinder/wsgi.py中就import evenlet.wsgi,并在其基础上进一步抽象和包装,形成了wsgi.py中server类。

3     一个简单的wsgi服务

如何启动一个wsgi的server,注册application,并能响应http请求?先来看一个很简单的wsgi应用:

__author__= 'sxmatch'

"""the most simplest server of wsgi """

importwebob

importeventlet

fromeventlet import wsgi

fromwebob import Request

defmyapp(env, start_response):

    status = "200 OK"

    response_headers = [('Content-Type','text/plain')]

    start_response(status, response_headers)

    return ['Hello, World! I am sxmatch\r\n']

 

wsgi.server(eventlet.listen(('192.168.82.191',8090)), myapp)

该程序可以直接在服务器上跑起来


原创粉丝点击