Http协议与生命周期

一.Http知识:
    1.基于socket
        浏览器(格式一)
        web服务器(格式一)
        MYSQL客户端(格式二)
        MYSQL服务端(格式三)
       
        本质:
           socket=socket.socket
           socket.connect((ip,端口))
           socket.sendall(b'scasa41sa4sd')
    2.浏览器发送GET请求数据格式
           socket=socket.socket
           socket.connect((ip,端口))
           socket.sendall(b'GET/index/?name=xxx&age=21 http1.1 Accept:text/html Accept-Encoding:gzip'
           deflate Cookie:UM_distinctid=15d274 ')    
           request.GET.get('name')
    3.浏览器发送POST请求数据格式:
            socket = socket.socket()
            socket.connect((ip,端口))
            socket.sendall(b'POST /index/?name=xxx&age=11 http1.1 Accept:text/html Accept-Encoding:gzip, deflate Cookie:UM_distinctid=15d274 a1=123&a2=666&a3=xx')
            
            Django加工POST请求的数据,
            判断用户是否传递的是Django可以向request.POST中解析的数据?读取请求头Content-Type: application/x-www-form-urlencoded,那么就去解析request.body中的值,放置到request.POST中
                                      a1=123&a2=666&a3=xx
                                      request.POST.get('name')
                                      request.body   b"a1=123&a2=666&a3=xx"
                                      
            Django加工POST请求的数据:{a1:123,a2:567}
                                      request.POST   空
                                      request.body   b"{a1:123,a2:567}"                                     
    4.Http协议
            - 请求头和请求体分割:
            - 请求体之间:
            - GET无请求体
            - 无状态,短连接:socket请求响应断开
            - 请求头代表的意义
                - user-agent:来源
                - referer: 防盗链
                - content-type:请求体是什么格式?   

二、Django的生命周期
    1.wsgiref:
            函数版本:
                from wsgiref.simple_server import make_server
                def run_server(environ, start_response):
                    start_response('200 OK', [('Content-Type', 'text/html')])
                    return [bytes('<h1>Hello, web!</h1>', encoding='utf-8'), ]
                
                
                if __name__ == '__main__':
                    httpd = make_server('127.0.0.1', 8000, run_server) # 请求一旦到来:run_server(..)
                    httpd.serve_forever()
                
        
            类版本:
                from wsgiref.simple_server import make_server
                
                class WsgiHandler(object):
                
                    def __call__(self,environ, start_response):
                        start_response('200 OK', [('Content-Type', 'text/html')])
                        return [bytes('<h1>Hello, web!</h1>', encoding='utf-8'), ]
                
                
                if __name__ == '__main__':
                    obj =WsgiHandler()
                    httpd = make_server('127.0.0.1', 8000, obj) # 请求一旦到来:obj(..)
                    httpd.serve_forever()
                
                # 类()   -> __init__
                # 类()() -> __call__
            
        生命周期:
            ---wsgi,中间件,路由,视图(数据,模板)

       uwsgi,wsgi什么区别?
            wsgi, web服务网关接口,协议
            uwsgi实现协议
            wsgiref实现协议

      视图:
            接收请求
            返回内容
           
        注意:
            渲染工作在Django中执行完成后,字符串返回给浏览器。
            但是:js,css额外再发一次请求仅获取静态文件

原文地址:https://www.cnblogs.com/mengqingjian/p/7797238.html