• 第一节:web 框架本质


    1.web请求本质是什么:   

       web请求本质:就是一个socket。      浏览器充当是客户端,python充当一个服务器端
          浏览器请求相当一次tcp请求,请求完成断开连接 ,下次去的时候,还知道是谁不,
            http是无状态的。使用下之前写的socket代码:

    #__author__ = 'wuchao'
    import socket
    def handle_request(client):
        buf = client.recv(1024)
        client.send(b"HTTP/1.1 200 OK
    
    ")
        client.send(b"Hello, Seven")
    def main():
        sock = socket.socket()
        sock.bind(('localhost',8099))
        sock.listen(5)
        while True:
            connection, address = sock.accept()
            handle_request(connection)
            connection.close()
    if __name__ == '__main__':
            main()
    mysocket.py

        运行以上程序,浏览器访问:localhost:8099

       

       上述通过socket来实现了其本质,而对于真实开发中的python web程序来说,一般会分为两部分:服务器程序和应用程序。服务器程序负责对socket服务器进行封装,
        并在请求到来时,对请求的各种数据进行整理。应用程序则负责具体的逻辑处理。为了方便应用程序的开发,就出现了众多的Web框架,例如:Django、Flask、web.py 等。
        不同的框架有不同的开发方式,但是无论如何,开发出的应用程序都要和服务器程序配合,才能为用户提供服务。这样,服务器程序就需要为不同的框架提供不同的支持。
        这样混乱的局面无论对于服务器还是框架,都是不好的。对服务器来说,需要支持各种不同框架,对框架来说,只有支持它的服务器才能被开发出的应用使用。这时候,
        标准化就变得尤为重要。我们可以设立一个标准,只要服务器程序支持这个标准,框架也支持这个标准,那么他们就可以配合使用。一旦标准确定,双方各自实现。这样,
        服务器可以支持更多支持标准的框架,框架也可以使用更多支持标准的服务器。

    WSGI(Web Server Gateway Interface)是一种规范,WSGI不是服务器,不是API,不是Python模块,更不是什么框架,而是一种服务器和客户端交互的接口规范

    更具体的规范说明请搜索“PEP 3333”。

    在WSGI规范下,web组件被分成三类:client, server, and middleware.
    WSGI apps(服从该规范的应用)能够被连接起来(be stacked)处理一个request,这也就引发了中间件这个概念,中间件同时实现c端和s端的接口,c看它是上游s,s看它是下游的c。
    WSGI的s端所做的工作仅仅是接收请求,传给application(做处理),然后将结果response给middleware或client.除此以外的工作都交给中间件或者application来做。
     
    python标准库提供的独立WSGI服务器称为wsgiref。
     1 from wsgiref.simple_server import make_server
     2  
     3  
     4 def RunServer(environ, start_response):
     5     start_response('200 OK', [('Content-Type', 'text/html')])
     6     return [bytes('<h1>返回的结果</h1>', encoding='utf-8'), ]#需要返回byte类型
     7  
     8  
     9 if __name__ == '__main__':
    10     httpd = make_server('', 8000, RunServer)
    11     print("Serving HTTP on port 8000...")
    12     httpd.serve_forever()

    测试案例:

    from wsgiref.simple_server import make_server
    
    
    def RunServer(environ, start_response):
        path_info=environ['PATH_INFO'] #取得用户请求地址
        start_response('200 OK', [('Content-Type', 'text/html')])
        if path_info == '/':
            return [bytes('<html><head><meta charset="utf-8"></head><h1>首页</h1><a href="/list.html">列表页</a></html>', encoding='utf-8'), ]  # 需要返回byte类型
        if path_info == '/list.html':
            return [bytes('<html><head><meta charset="utf-8"><h1>列表页</h1><a href="/show.html">详细页</a></html>', encoding='utf-8'), ]  # 需要返回byte类型
        if path_info == '/show.html':
            return [bytes('<html><head><meta charset="utf-8"><h1>详细页</h1><a href="/">返回首页</a></html>', encoding='utf-8'), ]  # 需要返回byte类型
        else:
            return [bytes('<html><head><meta charset="utf-8"><h1>错误页面</h1><a href="/">返回首页</a></html>', encoding='utf-8'), ]  # 需要返回byte类型
        # for item in environ:
        #     print(item,':',environ[item])
    
    
    
    
    if __name__ == '__main__':
        httpd = make_server('', 8000, RunServer)
        print("Serving HTTP on port 8000...")
        httpd.serve_forever()

    自定义Web框架

     1.创建一个views.py 文件

     1 #__author__ = 'wuchao'
     2 def hand_test():
     3     f = open('templates/test.html', 'rb')
     4     res = f.read()
     5     res = res.replace(b'{{name}}', bytes('黄飞', encoding='utf8'))
     6     return [(res), ]
     7 
     8 def hand_test1():
     9     f = open('templates/test1.html', 'rb')
    10     res = f.read()
    11     return [(res), ]
    12 def hand_js():
    13     f = open('templates/my.js', 'rb')
    14     res = f.read()
    15     return [(res), ]
    16 
    17 def other():
    18     return [bytes('<h1>404</h1>', encoding='utf-8'), ]

        2.创建一个wsgi.py文件

     1 #__author__ = 'wuchao'
     2 from wsgiref.simple_server import make_server
     3 from view import *
     4 urls={
     5     '/test':hand_test,
     6     '/test1':hand_test1,
     7     '/my.js':hand_js
     8 }
     9 def RunServer(environ, start_response):
    10     path_info =environ['PATH_INFO']   #取得页面参数
    11     start_response('200 OK', [('Content-Type', 'text/html')])#必须
    12     if path_info in urls:  #判断页面参数是否在urls里存在
    13         fun=urls[path_info]
    14         return fun()  #调用urls对应的函数
    15     else:
    16         return other()  #如果参数不存在,则调用other函数
    17 if __name__ == '__main__':
    18     httpd = make_server('', 8098, RunServer)
    19     print("Serving HTTP on port 8000...")
    20     httpd.serve_forever()

    3。创建templates模板文件夹:

     

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>test</title>
     6 </head>
     7 <body>
     8     <div style="border: 1px solid red;  200px;height: 200px">
     9         {{name}}昨天晚上妹子都有约,没人理他。
    10     </div>
    11 </body>
    12 </html>

      

      

  • 相关阅读:
    hdfs command
    开机启动
    date
    tabulate
    django前后端分离403 csrf token missing or incorrect
    设计一个程序,程序中有三个类,Triangle,Lader,Circle。
    总结,
    数据库2
    JDBC数据库1
    网络编程2
  • 原文地址:https://www.cnblogs.com/wu-chao/p/8125108.html
Copyright © 2020-2023  润新知