1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| from wsgiref.simple_server import make_server from jinja2 import Template
def index(): with open('index.html', 'r', encoding='utf-8') as f: content = f.read() print(content) return [content.encode('utf-8')]
def login(): with open('login.html', 'r', encoding='utf-8') as f: content = f.read() return [content.encode('utf-8')]
def template(): with open('template.html', 'r', encoding='utf-8') as f: content = f.read() template = Template(content) data = template.render(name='犬夜叉', user_list=['桔梗', '日暮戈薇']) return [data.encode('utf-8')]
def routers():
urlpatterns = ( ('/index/', index), ('/login/', login), ('/template/', template), )
return urlpatterns
def RunServer(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) url = environ['PATH_INFO'] urlpatterns = routers() func = None for item in urlpatterns: if item[0] == url: func = item[1] break if func: return func() else: return ['404 not found'.encode('utf-8')]
if __name__ == '__main__': httpd = make_server('127.0.0.1', 8000, RunServer) print("Serving HTTP on port 8000...") httpd.serve_forever()
|