zoukankan      html  css  js  c++  java
  • 手写一个类django框架

    用与django相似结构写一个web框架。

    启动文件代码:

     1 from wsgiref.simple_server import make_server  #导入模块
     2 from views import *
     3 import urls
     4 
     5 def routers():   #这个函数是个元组
     6     URLpattern=urls.URLpattern
     7     return URLpattern  #这个函数执行后返回这个元组
     8 
     9 def application(environ,start_response):
    10     print("ok1")
    11     path=environ.get("PATH_INFO")
    12     print("path",path)
    13     start_response('200 OK',[('Content-Type','text/html')])
    14     urlpattern=routers()  #讲函数的返回值元组赋值
    15     func=None
    16 
    17     for item in urlpattern:  #遍历这个元组
    18         if path==item[0]:   #item[0]就是#路径后面的斜杠内容
    19             func=item[1]   #item[1]就是对应的函数名
    20             break
    21     if func:  #如果路径内容存在函数就存在
    22         return func(environ)  #执行这个函数
    23     else:
    24         print("ok5")
    25         return [b"404"]  #如果不存在就返回404
    26 
    27 if __name__=='__main__':
    28     print("ok0")
    29     t=make_server("",9700,application)
    30     print("ok22")
    31     t.serve_forever()

    urls.py文件代码:

    1 from views import *
    2 URLpattern = (
    3     ("/login", login),
    4     ("/alex", foo1),
    5     ("/egon", foo2),
    6     ("/auth", auth)
    7 )

    views.py文件代码:

     1 def foo1(request):  # 定义函数
     2     f=open("templates/alex.html","rb")  #打开html 以二进制的模式
     3     data=f.read()  #读到data里
     4     f.close()  #关闭
     5     return [data]  #返回这个data
     6 
     7 def foo2(request):
     8     f=open("templates/egon.html","rb")
     9     data=f.read()
    10     f.close()
    11     return [data]
    12 
    13 def login(request):
    14     f=open("templates/login.html","rb")
    15     data=f.read()
    16     f.close()
    17     return [data]
    18 
    19 def auth(request):
    20     print("+++",request)
    21     user_union,pwd_union=request.get("QUERY_STRING").split("&")
    22     _,user=user_union.split("=")
    23     _,pwd=pwd_union.split("=")
    24 
    25     if user=='Yuan' and pwd=="123":
    26         return [b"login,welcome"]
    27     else:
    28         return [b"user or pwd is wriong"]

    templates目录下的html文件:

    alex.html

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <meta http-equiv="x-ua-compatible" content="IE=edge">
     6     <meta name="viewport" content="width=device-width, initial-scale=1">
     7     <title>Title</title>
     8 </head>
     9 <body>
    10 <div>alex</div>
    11 </body>
    12 </html>

    login.html

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Title</title>
     6 </head>
     7 <body>
     8 <h2>登录页面</h2>
     9 <form action="http://127.0.0.1:9700/auth">
    10     <p>姓名:<input type="text" name="user"></p>
    11     <p>密码:<input type="password" name="pwd"></p>
    12     <p>
    13         <input type="submit">
    14     </p>
    15 </form>
    16 </body>
    17 </html>

    下面如图,是目录结构

     访问ip+prot+路径 即为相应的html,功能简单,只是为了熟悉django

  • 相关阅读:
    [Codeforces Round #498 (Div. 3)] -F. Xor-Paths (折半搜索)
    Best Reward [HDU
    [Educational Codeforces Round 72] A. Creating a Character (简单数学)
    [Codeforces Round #624 (Div. 3)] -E. Construct the Binary Tree (构造二叉树)
    [Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics)] -D. Present(异或性质,按位拆分,双指针)
    [Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics)] -D. Present(异或性质,按位拆分,树桩数组)
    [Educational Codeforces Round 83 ] E. Array Shrinking
    [AtCoder Beginner Contest 158]
    [CodeCraft-20 (Div. 2)]- E. Team Building (状压DP)
    HDU 3308 LCIS (线段树区间合并)
  • 原文地址:https://www.cnblogs.com/ArmoredTitan/p/7412573.html
Copyright © 2011-2022 走看看