
1、WSGI是Python的Web开发的基石,有两个存在目的:
描述 Web 服务器如何与 Web 应用程序交互(将客户端请求传给应用程序);
描述 Web 应用程序如何处理请求和如何返回数据给服务器。
2、由于Python内置的标准库里有一个WSGI库wsgiref,我们基于他来写一个体现WSGI目的的例子:
1 2 3 4 5 6 7 8 9 10 11 | 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()
|
以上就是Python中WSGI的使用,希望对大家有所帮助。更多Python学习推荐:python教学
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。