Python中WSGI的使用

489次阅读
没有评论

Python中WSGI的使用

1、WSGI是Python的Web开发的基石,有两个存在目的:

描述 Web 服务器如何与 Web 应用程序交互(将客户端请求传给应用程序);

描述 Web 应用程序如何处理请求和如何返回数据给服务器。

2、由于Python内置的标准库里有一个WSGI库wsgiref,我们基于他来写一个体现WSGI目的的例子:

from wsgiref.simple_server import make_server
 
def application(environ, start_response):
    status = '200 OK'
    response_headers = [('Content-type', 'text/html')]
    start_response(status, response_headers)
    body = '<h1>Hello, {name} !!!</h1>'.format(name=environ['PATH_INFO'][1:] or 'WSGI')
    return [body.encode('utf-8')]
 
app = make_server('', 8000, application)
app.serve_forever()
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-05-01发表,共计533字。
新手QQ群:570568346,欢迎进群讨论 Python51学习