WSGI接口是Python应用程序或框架和Web服务器之间的一种接口,详细类容参考:
https://blog.csdn.net/on_1y/article/details/18803563
这里只做一个Demo:
Step1: 定义hello.py
def demo_app1(environ,start_response):
from io import StringIO
stdout = StringIO()
print("Hello world!", file=stdout)
print(file=stdout)
h = sorted(environ.items())
for k,v in h:
print(k,'=',repr(v), file=stdout)
start_response("200 OK", [('Content-Type','text/plain; charset=utf-8')])
return [stdout.getvalue().encode("utf-8")]
Step2:在同目录下,定义server.py
# 从wsgiref模块导入:
from wsgiref.simple_server import make_server, demo_app
from LXF.Web开发.hello import demo_app1
application = demo_app1
# 创建一个服务器,IP地址为空,端口是8000,处理函数是application:
httpd = make_server('127.0.0.1', 8000, application)
print("Serving HTTP on port 8000...")
httpd.handle_request()
Step3: 运行sever.py, 在浏览器中输入 localhost:8000