zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    Ubuntu 拦截并监听 power button 的关机消息
    Android 电池管理系统架构总结 Android power and battery management architecture summaries
    Linux 内核代码风格
    Linux 内核工作队列之work_struct 学习总结
    微信小程序 登录流程规范解读
    微信小程序监听input输入并取值
    koala 编译scss不支持中文(包括中文注释),解决方案如下
    阻止冒泡和阻止默认事件的兼容写法
    使用setTimeout实现setInterval
    css实现视差滚动效果
  • 原文地址:https://www.cnblogs.com/pyhan/p/12346588.html
Copyright © 2011-2022 走看看