• Python学习第二十七课——写一个和Django框架的自己的框架


    MyWeb框架:

    from wsgiref.simple_server import make_server
    
    
    def application(environ, start_response):
    
        print(environ)
        start_response('200 OK', [('Content-Type', 'text/html')])
    
        return [b'<h1> Hello,web! </h1>']
    
    
    httpd = make_server('',8080,application)
    
    print('Serving HTTP on port 8080....')
    
    httpd.serve_forever()

    MyWeb框架第一次修改:

    index1.html:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    
    <h1>hello mei</h1>
    
    </body>
    </html>

    index2.html:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <h1>hello han</h1>
    
    </body>
    </html>

    MyWeb框架第一次修改.py :

    from wsgiref.simple_server import make_server
    
    
    def mei():
        f = open("index1.html", "rb")
        data = f.read()
        return data
    
    
    def han():
        f = open("index2.html", "rb")
        data = f.read()
        return data
    
    
    def application(environ, start_response):
        # print(environ) #自动生成一个打的字典
    
        print('path', environ["PATH_INFO"])  # environ字典中
        path = environ["PATH_INFO"]
        start_response('200 OK', [('Content-Type', 'text/html')])  # 设置发送的文件类型
        if path == "/han":
            return [han()]
        elif path == "/mei":
            return [mei()]
        else:
            return [b'404']
    
    
    httpd = make_server('', 8080, application)
    
    print('Serving HTTP on port 8080....')
    
    httpd.serve_forever()

    MyWeb框架第二次修改:

    login.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <form action="http://localhost:8080/login" method="get">
        <p>用户名 <input type="text" name="user" id="user"></p>
        <p>密码 <input type="text" name="pwd" id="pwd"></p>
        <p><input type="submit">提交</p>
    </form>
    
    
    </body>
    </html>

    show_time.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <h1>时间:{{time}}</h1>
    
    </body>
    </html>

    MyWeb框架第二次修改.py:

    from wsgiref.simple_server import make_server
    
    import time
    
    
    def mei(req):
        f = open("index1.html", "rb")
        data = f.read()
        return data
    
    
    def han(req):
        f = open("index2.html", "rb")
        data = f.read()
        return data
    
    
    def login(req):
        print(req["QUERY_STRING"])
        return b"welcome!"
    
    
    def signup(req):
        pass
    
    
    def show_time(req):
        times = time.ctime()
        return ("<h1> time: %s </h1>" % str(times)).encode("utf8") # 打印时间
    
        #这种方法不建议使用
        # f=open("show_time.html","rb")
        # data=f.read()
        # data=data.decode("utf8")
        # data=data.replace("{{time}}",str(times))
        # return data.encode("utf8")
    
    def router():
        url_patterns = [
            ("/mei", mei),
            ("/signup", signup),
            ("/han", han),
            ("/login", login),
            ("/show_time", show_time),
    
        ]
        return url_patterns  # 返回字典
    
    
    def application(environ, start_response):
        # print(environ) #自动生成一个打的字典
    
        print('path', environ["PATH_INFO"])  # environ字典中
        path = environ["PATH_INFO"]
        start_response('200 OK', [('Content-Type', 'text/html')])  # 设置发送的文件类型
    
        url_patterns = router()
        func = None
        for item in url_patterns:
            if item[0] == path:  # 先判断是否为path
                func = item[1]  # 执行其对应方法
                break
        if func:
            return [func(environ)]
        else:
            return [b"404"]
    
    
    httpd = make_server('', 8080, application)
    
    print('Serving HTTP on port 8080....')
    
    httpd.serve_forever()
  • 相关阅读:
    支付宝开发平台之第三方授权登录与获取用户信息
    Struts2之访问路径
    Struts2之命名空间与Action的三种创建方式
    vs和vim
    linux下的各个目录(待填)
    linux-CentOS初学terminal命令(3)rm、chmod、mkdir、who、w、id、systemctl、
    gcc和vs在c的一些区别
    利用putty在window下控制linux的terminal、ftp传输
    linux-CentOS初学terminal命令(2)vi、gcc、g++、./、mv、cp、ifconfig
    linux初学terminal命令(1)ls、cd、su、man、pwd、useradd、passwd、cat、Ctrl+C、Ctrl+Z、Ctrl+L
  • 原文地址:https://www.cnblogs.com/pyhan/p/12346588.html
Copyright © 2020-2023  润新知