python WSGI+Paste Deployment+oslo.config综合运用

关于Paste Deployment介绍请看我之前的博客Python Paste Deployment

关于oslo.config介绍请看我之前博客oslo.config

关于WSGI介绍请看我之前博客WSGI接口

在有了上面的基础后,不多说,直接上代码

server.py

import os
import commands
from paste.deploy import loadapp
from oslo_config import cfg
from wsgiref.simple_server import make_server

server_opts = [
    cfg.StrOpt('bind_host',
               default='0.0.0.0',
               help='IP address to listen on.'),
    cfg.IntOpt('bind_port',
               default=9292,
               help='Port number to listen on.')
]
CONF = cfg.CONF
CONF.register_opts(server_opts)
CONF(default_config_files=['server-config.conf'])


class Show(object):
    """docstring for Show"""
    def __init__(self):
        super(Show, self).__init__()

    def __call__(self, environ, start_response):
        start_response("200 OK", [("Content-type", "text/plain")])
        return ["welcome to use it!"]

    @classmethod
    def factory(cls, global_conf, **kwargs):
        print "in Show.factory", global_conf, kwargs
        return Show()


class Hello(object):
    """docstring for Hello"""
    def __init__(self):
        super(Hello, self).__init__()

    def __call__(self, environ, start_response):
        start_response("200 OK", [("Content-type", "text/plain")])
        return ["Hello, nice to meet you"]

    @classmethod
    def factory(cls, global_conf, **kwargs):
        print "in Hello.factory", global_conf, kwargs
        return Hello()


class Goodbye(object):
    """docstring for Goodbye"""
    def __init__(self):
        super(Goodbye, self).__init__()

    def __call__(self, environ, start_response):
        start_response("200 OK", [("Content-type", "text/plain")])
        return ["Goodbye, may we meet again"]

    @classmethod
    def factory(cls, global_conf, **kwargs):
        print "in Goodbye.factory", global_conf, kwargs
        return Goodbye()


class Free(object):
    """docstring for Free"""
    def __init__(self):
        super(Free, self).__init__()

    def __call__(self, environ, start_response):
        start_response("200 OK", [("Content-type", "text/plain")])
        result = commands.getstatusoutput('free')
        return [str(result[1])]

    @classmethod
    def factory(cls, global_conf, **kwargs):
        print "in Free.factory", global_conf, kwargs
        return Free()


def main():
    configfile = "server-api.ini"
    appname = "myServer"
    wsgi_app = loadapp("config:%s" % os.path.abspath(configfile), appname)
    print CONF.bind_host, CONF.bind_port
    server = make_server(CONF.bind_host, CONF.bind_port, wsgi_app)
    server.serve_forever()

if __name__ == '__main__':
    main()
server-api.ini

[composite:myServer]
use=egg:Paste#urlmap
/:show
/hello:say_hello
/goodbye:say_goodbye
/free:exe_free

[app:say_hello]
description = This is to say hello
paste.app_factory = server:Hello.factory

[app:say_goodbye]
description = This is to say goodbye
paste.app_factory = server:Goodbye.factory

[app:show]
description = This is to show version
paste.app_factory = server:Show.factory

[app:exe_free]
description = This is to execute free
paste.app_factory = server:Free.factory
server-config.ini

[DEFAULT]
bind_host = 
bind_port = 8000
        先启动WSGI服务,然后在浏览器中输入:

python WSGI+Paste Deployment+oslo.config综合运用_第1张图片python WSGI+Paste Deployment+oslo.config综合运用_第2张图片python WSGI+Paste Deployment+oslo.config综合运用_第3张图片python WSGI+Paste Deployment+oslo.config综合运用_第4张图片

        过程很简单,利用WSGI处理相应的请求,利用Paste Deploy把请求分发到不同的应用,利用oslo.config读取server配置.


你可能感兴趣的:(python WSGI+Paste Deployment+oslo.config综合运用)