参考
https://www.cnblogs.com/kuaizifeng/p/7618117.html
1 def hello_world_app(environ, start_response): 2 status = '200 OK' # HTTP Status 3 headers = [('Content-type', 'text/plain')] # HTTP Headers 4 start_response(status, headers) 5 6 # The returned object is going to be printed 7 return [b"Hello World"] 8 9 def main(): 10 from wsgiref.simple_server import make_server 11 httpd = make_server('', 8000, hello_world_app) 12 print("Serving on port 8000...") 13 14 # Serve until process is killed 15 httpd.serve_forever() 16 17 if __name__ == '__main__': 18 main()
1 # 从wsgiref模块导入: 2 from wsgiref.simple_server import make_server 3 4 # 导入我们自己编写的application函数: 5 def application(environ, start_response): 6 start_response('200 OK', [('Content-Type', 'text/html')]) 7 return [b'<h1>Hello, web!</h1>'] 8 9 # 创建一个服务器,IP地址为空,端口是8000,处理函数是application: 10 httpd = make_server('127.0.0.1', 8000, application) 11 print("Serving HTTP on port 8000...") 12 # 开始监听HTTP请求: 13 httpd.serve_forever()