Python中WSGI的使用

Python (150) 2023-06-18 03:37:08

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

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

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

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

fromwsgiref.simple_serverimportmake_server

defapplication(environ,start_response):
status='200OK'
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电脑。

THE END

发表回复