Python中实现WSGI的框架

2025-12-01 0 31,736

1、说明

Application类对WSGI又做了一层简单的封装,由于上面说过WSGI函数返回的是一个可以迭代对象,所以需要实现一个__iter__方法,里面控制了客户端的请求路由并且返回不同的输出。

2、实例

from wsgiref.simple_server import make_server
 
class Application(object):
 
    def __init__(self, environ, start_response):
        self.start_response = start_response
        self.path = environ['PATH_INFO']
 
    def __iter__(self):
        if self.path == '/':
            status = '200 OK'
            response_headers = [('Content-type', 'text/html')]
            self.start_response(status, esponse_headers)
            yield '

Hello,World!

'.encode('utf-8')           elif self.path == '/wsgi':             status = '200 OK'             response_headers = [('Content-type', 'text/html')]             self.start_response(status, response_headers)             yield '

Hello,WSGI!

'.encode('utf-8')           else:             status = '404 NOT FOUND'             response_headers = [('Content-type', 'text/html')]             self.start_response(status, response_headers)             yield '

404 NOT FOUND

'.encode('utf-8')   if __name__ == "__main__":     app = make_server('127.0.0.1', 8000, Application)     print('Serving HTTP on port 8000...')     app.serve_forever()

以上就是Python中实现WSGI的框架,希望对大家有所帮助。更多Python学习推荐:python教学

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:以上部本文内容由互联网用户自发贡献,本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。投诉邮箱:3758217903@qq.com

ZhiUp资源网 python基础 Python中实现WSGI的框架 https://www.zhiup.top/2057.html

相关