wsgi

作者: 青铜搬砖工 | 来源:发表于2019-03-04 09:39 被阅读0次
import eventlet
from eventlet import wsgi
from paste.deploy import loadapp
import routes
import routes.middleware as middleware
import webob.dec
import webob.exc


class Resource(object):
    def __init__(self, controller):
        self.controller = controller()

    @webob.dec.wsgify
    def __call__(self, req):
        match = req.environ['wsgiorg.routing_args'][1]
        action = match['action']
        if hasattr(self.controller, action):
            method = getattr(self.controller, action)
            return method(req)
        return webob.exc.HTTPNotFound()


class CatController(object):

    def index(self, req):
        return 'List cats.'

    def create(self, req):
        return 'create cat.'

    def delete(self, req):
        return 'delete cat.'

    def update(self, req):
        return 'update cat.'


class DogController(object):

    def index(self, req):
        return 'List dogs.'

    def create(self, req):
        return 'create dog.'

    def delete(self, req):
        return 'delete dog.'

    def update(self, req):
        return 'update dog.'


class AnimalApplication(object):
    def __init__(self):
        self.mapper = routes.Mapper()
        self.mapper.resource('cat', 'cats', controller=Resource(CatController))
        self.mapper.resource('dog', 'dogs', controller=Resource(DogController))
        self.router = middleware.RoutesMiddleware(self.dispatch, self.mapper)

    @webob.dec.wsgify
    def __call__(self, req):
        return self.router

    @classmethod
    def factory(cls, global_conf, **local_conf):
        return cls()

    @staticmethod
    @webob.dec.wsgify
    def dispatch(req):
        match = req.environ['wsgiorg.routing_args'][1]
        return match['controller'] if match else webob.exc.HTTPNotFound()


class IPBlacklistMiddleware(object):
    def __init__(self, application):
        self.application = application

    def __call__(self, environ, start_response):
        ip_addr = environ.get('HTTP_HOST').split(':')[0]
        if ip_addr not in ('127.0.0.1','192.168.0.51'):
            start_response('403 Forbidden', [('Content-Type', 'text/plain')])
            return ['Forbidden']

        return self.application(environ, start_response)

    @classmethod
    def factory(cls, global_conf, **local_conf):
        def _factory(application):
            return cls(application)

        return _factory


if '__main__' == __name__:
    application = loadapp('config:D:/asynchronous/animal.ini')
    server = eventlet.spawn(wsgi.server,
                            eventlet.listen(('', 8080)), application)
    server.wait()

配置文件

[composite:main]
use = egg:Paste#urlmap
/ = animal_pipeline

[pipeline:animal_pipeline]
pipeline = ip_blacklist animal

[filter:ip_blacklist]
paste.filter_factory = animals:IPBlacklistMiddleware.factory

[app:animal]
paste.app_factory = animals:AnimalApplication.factory
  1. wsgi就是一个规范,规范应用的接收参数必须是一个http的环境变量与一个返回http状态的回掉函数
    return一个可迭代的对象
    1.pastedeploy 首先要有一个.ini的配置文件,配置文件里面包括app的入口与中间件 app的执行过程
    如果想定义中间件必须定义一个管道,也就是http请求要经过的一个顺序,比如 管道定义为 ip白名单黑名单
    处理http请求应用
    2.中间件与应用的本质是一样的对于wsgi来说都是应用。
    3.应用使用router.mapper来建立restful的映射关系,主要通过http请求的环境变量里的req.environ['wsgiorg.routing_args'][1]的
    action变量来映射restful的处理方法。
    4.可以使用eventlet,协程来增加web的并发能力

相关文章

  • python wsgi+Odoo 的启动

    参考:WSGI初探Odoo web 机制浅析python的 WSGI 简介python wsgi 简介 wsgi的...

  • wsgi&uwsgi

    WSGI协议 WSGI, aka Web Server Gateway Interface, WSGI 不是服务器...

  • wsgi简介

    wsgi是什么? WSGI:Web Server Gateway Interface。WSGI接口定义非常简单,它...

  • WSGI简介

    结合案例Python部署 & 示例代码wsgi-demo All in One WSGI WSGI = Pytho...

  • gunicorn使用

    WSGI 了解gunicorn之前先了解WSGI WSGI是Python Web Server Gateway I...

  • flask的deamon简单分析

    代码样例 分析所谓的wsgi应用,wsgi应用一定要有call函数。这样最后才能被wsgi调用,并将wsgi应用处...

  • 关于网络的记录

    WSGI等 WSGI是一种通信协议。WSGI将Web组件分成了三类:Web 服务器(WSGI Server)、We...

  • flask源码分析

    wsgi协议 关于wsgi协议就不赘述了,以下是最简单的符合wsgi的应用 app attriabute app....

  • Flask流程

    回顾一下Flask的流程: WSGI Server 到 WSGI App 图中可以看到HTTP请求都是通过WSGI...

  • WSGI

    settings.py中 WSGI_APPLICATION = 'WebCsdn.wsgi.application...

网友评论

      本文标题:wsgi

      本文链接:https://www.haomeiwen.com/subject/udukuqtx.html