分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

Http协议

发布时间:2023-09-06 01:23责任编辑:赖小花关键词:暂无标签

一.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\r\nAccept:text/html\r\nAccept-Encoding:gzip‘
           deflate\r\nCookie:UM_distinctid=15d274\r\n\r\n‘)    
           request.GET.get(‘name‘)
    3.浏览器发送POST请求数据格式:
            socket = socket.socket()
            socket.connect((ip,端口))
            socket.sendall(b‘POST /index/?name=xxx&age=11 http1.1\r\nAccept:text/html\r\nAccept-Encoding:gzip, deflate\r\nCookie:UM_distinctid=15d274\r\n\r\na1=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协议
            - 请求头和请求体分割:\r\n\r\n
            - 请求体之间:\r\n
            - 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,中间件,路由,视图(数据,模板)
           
        注意:
            渲染工作在Django中执行完成后,字符串返回给浏览器。
            但是:js,css额外再发一次请求仅获取静态文件

Http协议

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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved