WSGI+pastedeploy+webob

web server接收HTTP请求,封装环境变量,按照WSGI接口标准调用注册的WSGI Application,返回响应给客户端。

paste.deploy

openstack使用pastedeploy组件来完成WSGI服务器和应用的构建
  • section:[type: name]
    • type = composite
      •  把URL请求分发给对应的app
    • type = app
      • app是具体的WSGI Application
    • type = filter-app
      • 过滤并转发给app
    • type = filter
      • 类似于filter-app,但没有next
    • type = pipeline
      • 由filter+...+filter+app组成
# config.ini 

[composite:main]
# use表明具体的分发方式
# egg:Paste#urlmap表示使用paste包中的urlmap模块
use = egg:Paste#urlmap     
# key=value表示使用urlmap进行分发的参数
/ = home
/wiki = wiki
/blog = blog
/cms = config:cms.ini

[app:home]
# use指定app
# python egg
use = egg:Paste#static

[app:app2]
# config.ini
use = config:myconfig.ini#app_name

[app:app3]
# 直接调用另外一个模块中的app
use = call:myproject:application

[app:app4]
# 另外一个section
use = myotherapp

[app:app5]
# myapp.modulename
paste.app_factory = myapp.modulename:app_factory

[filter-app:blog]
# use指定过滤条件
use = egg:Authentication#auth
# next表示过滤后的app
next = blogapp
roles = admin
htpasswd = /home/me/users.htpasswd

[pipeline:main]
pipeline = filter1 egg:FilterEgg#filter2 filter3 app

[filter:filter1]
...
  • app_factory
def app_factory(loader, global_config, **local_conf)
    return wsgi_app
  • 调用paste.deploy
from paste.deploy import loadapp
wsgi_app = loadapp('config:/path/to/config.ini')

webob

webob对WSGI请求与响应进行封装

  • webob.Request

    • 对WSGI请求的environ参数进行封装

  • webob.Response

    • 包含所有标准WSGI响应要素

  • webob.exc

    • 封装HTTP错误代码

  • webob.dec.wsgify

    • 封装WSGI参数和返回格式

 

  • 标准WSGI格式

def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    return '<h1>Hello</h1>'
  • webob.dec.wsgify封装格式

@webob.dec.wsgify
def application(req):
    return webob.Response('OK')
# RequestClass过滤
@webob.dec.wsgify(RequestClass=MyRequest)

WSGI

  • 标准WSGI格式

def application(environ, start_response):
    # HTTP请求:environ
    # HTTP响应:start_response(HTTP响应码,HTTP Header, 错误信息)
    start_response('200 OK', [('Content-Type', 'text/html')], exc_info=None)
    # 返回Body
    return '<h1>Hello</h1>'

WSGI+pastedeploy+webob

 

# tsroutes.ini

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

[pipeline:show]
pipeline = root

[app:root]
paste.app_factory = tsroutes:Router.app_factory

 

# tsroutes.py

import logging
import os

import webob.dec
import webob.exc

from paste.deploy import loadapp
from wsgiref.simple_server import make_server

import routes.middleware


LOG = logging.getLogger(__name__)


class Controller(object):
    @webob.dec.wsgify
    def __call__(self, req):
        arg_dict = req.environ['wsgiorg.routing_args'][1]
        action = arg_dict.pop('action')

        return webob.Response('OK')

    def getMessage(self, context, user_id):
        return {'Message': 'TestMessage'}


class Router(object):
    def __init__(self):
        self._mapper = routes.Mapper()
        self._mapper.connect('/test/{user_id}',
                             controller=Controller(),
                             action='getMessage',
                             conditions={'method': ['GET']})
        self._router = routes.middleware.RoutesMiddleware(self._dispatch, self._mapper)


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

    @staticmethod
    @webob.dec.wsgify
    def _dispatch(req):
        match = req.environ['wsgiorg.routing_args'][1]

        if not match:
            return webob.exc.HTTPNotFound()

        app = match['controller']
        return app

    @classmethod
    def app_factory(cls, global_config, **local_config):
        return cls()


if __name__ == '__main__':
    configfile = 'tsroutes.ini'
    appname = "main"
    wsgi_app = loadapp("config:%s" % os.path.abspath(configfile), appname)
    httpd = make_server('localhost', 8282, wsgi_app)
    httpd.serve_forever()

测试:localhost:8282/test/1

Nginx是一款高性能的Web服务器,并且也可以作为反向代理服务器和负载均衡器使用。它的特点是占用资源少、稳定性高、并发能力强,可以处理大量的并发连接请求。 WSGIWeb Server Gateway Interface)是一种Web服务器和Python应用程序之间的通信协议。它定义了应用程序和服务器之间的接口规范,使得不同的Python框架(如Django和Flask)可以与Web服务器(如Nginx)进行通信,实现Web应用程序的部署和运行。 PHP 7.0是一种流行的开源脚本语言,常用于Web开发。它具有丰富的功能和扩展库,使得开发人员可以快速构建动态的网站和应用程序。在Nginx中,可以通过FastCGI模块来支持PHP的解析和执行。 结合这三者,我们可以使用Nginx作为Web服务器,通过WSGI协议将请求传递给Python应用程序,同时支持PHP解析和执行。具体的配置步骤如下: 1. 安装Nginx并配置虚拟主机:通过在Nginx的配置文件中添加虚拟主机,将请求定向到正确的域名或IP地址。 2. 安装和配置Python应用程序:确保Python应用程序与WSGI兼容,并安装必要的模块。可以使用Gunicorn作为WSGI容器,它可以与Nginx配合使用。 3. 配置Nginx与Python应用程序的通信:通过在Nginx的配置文件中添加代理规则,将请求传递给Python应用程序。可以使用与WSGI兼容的Gunicorn的地址。 4. 配置Nginx与PHP的通信:通过将PHP解析器与FastCGI模块结合使用,实现Nginx对PHP的支持。可以通过在Nginx的配置文件中添加相应的规则来实现。 这样,我们就可以在同一台服务器上同时支持Python和PHP应用程序。Nginx作为Web服务器和反向代理服务器,通过WSGI协议将请求传递给Python应用程序,并利用FastCGI模块支持PHP解析和执行。整个配置的目的是为了实现高性能和稳定性,以及对多种Web开发技术的支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值