zoukankan      html  css  js  c++  java
  • 自定义一个简单的web框架

    from wsgiref.simple_server import make_server

    def book(request):     #视图函数
      return [b'<h1> book !!! </h1>']
    def web(request):
      return [b'<h1> web !!! </h1>']

    def routers():
      urlpatterns = (
        ('/book',book),      #url 映射   
        ('/web',web),
      )
      return urlpatterns

    def application(environ,start_response):     
      start_response("200 OK",[("Content-Type",'text/html')])
      path = environ["PATH_INFO"]
      urlpatterns = routers()
      func = None
      for item in urlpatterns:
        if item[0] == path:
          func = item[1]
          break
        if func:
          return func(environ)
        else:
          return [b"<h1> 404 </h1>"]

    httpd = make_server('127.0.0.1',9000,application)
    print("Serving HTTP on port 9000....")
    httpd.serve_forever()

  • 相关阅读:
    1066 Bash 游戏
    1070 Bash 游戏 V4
    codevs 2928 你缺什么
    分块、线段树练习
    Father Christmas flymouse
    codevs 2494 Vani和Cl2捉迷藏
    codevs 2144 砝码称重2
    国王游戏
    codevs 1664 清凉冷水
    2015ACM/ICPC亚洲区沈阳站 Pagodas
  • 原文地址:https://www.cnblogs.com/fanxuanhui-linux/p/7966113.html
Copyright © 2011-2022 走看看