Fork me on GitHub

Python -- Web -- WSGI


WSGI:Web Server Gateway Interface

  只要求Web开发者实现一个函数,就可以响应HTTP请求。

 

# hello.py

def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    return [b'<h1>Hello, web!</h1>']

 

# server.py
from wsgiref.simple_server import make_server

from hello import application

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

运行server.py,浏览器中输入IP:8000即可访问

 

posted @ 2015-10-26 16:52  Roronoa__Zoro  阅读(128)  评论(0编辑  收藏  举报