标签:-o 过程 absolute nes path scroll col data- cte
20170901 基于wsgi的web框架二
from wsgiref.simple_server import make_server
import time
def f1(environ):
return [b"<h1>hello book</h1>"]
def f2(environ):
return [b"<h1>web</h1>"]
def default(environ):
return [b"<h1>hello world</h1>"]
def login(environ):
return [b‘<h1>hello , login!</h>‘]
def current_time(environ):
f = open("current_time.html", "rb")
data = f.read()
cur_time = time.ctime(time.time())
data = str(data, "utf8").replace("!cur_time!", str(cur_time))
return [data.encode("utf8")]
def routers():
urlpatterns = {
(‘/current_time‘, current_time),
(‘/book‘, f1),
(‘/web‘, f2),
(‘/login‘, login),
}
return urlpatterns
def application(environ, start_response):
start_response(‘200 OK‘, [(‘Content-Type‘, ‘text/html‘)])
path = environ["PATH_INFO"] # path==‘/book‘
urlpatterns = routers()
func = None
for item in urlpatterns:
if item[0] == path:
func = item[1]
break
if func:
return func(environ)
# 封装了socket对象以及准备过程(bind, listen)
httpd = make_server(‘‘, 8080, application)
print(‘Serving HTTP on port 8080...‘)
# 开始监听HTTP请求:
httpd.serve_forever()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>current_time:!cur_time!</h1>
</body>
</html>
标签:-o 过程 absolute nes path scroll col data- cte
原文地址:http://www.cnblogs.com/gengwenhao/p/7466306.html