WSGI接口

Web开发通常需要服务器,例如之前学的Tomcat,这里用python内置的wsgiref来实现,只提供简单的读取和返回页面操作。

WSGI是Web Server Gateway Interface,实现了底层的网络编程部分,不需要开发者处理HTTP请求等底层细节,高效开发业务代码。

# WSGI接口
# environ:一个包含所有HTTP请求信息的dict对象
# start_response:一个发送HTTP响应的函数
def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    body = '<h1>Hello, %s!</h1>' % (environ['PATH_INFO'][1:] or 'web')
    return [body.encode('utf-8')]

def main():
    # 创建一个服务器,IP地址为空,端口是8000,处理函数是application:
    httpd = make_server('', 8000, application)
    print('Serving HTTP on port 8000...')
    # 开始监听HTTP请求:
    httpd.serve_forever()

又看到了熟悉的页面。好像不能局域网访问。

原文地址:https://www.cnblogs.com/faded828x/p/14662558.html