其他分享
首页 > 其他分享> > 黏包现象和HTTP协议

黏包现象和HTTP协议

作者:互联网

黏包现象和HTTP协议

黏包现象

什么是黏包

解决方案

import socket
import struct


def main():
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    client.connect(('127.0.0.1', 7890))

    # 1.先接收报头
    header = client.recv(4)
    # 2.从报头中解析出数据长度
    data_size = struct.unpack('i', header)[0]

    # 3.接收真实的数据
    recv_size = 0
    total_data = b''
    while recv_size < data_size:
        data_recv = client.recv(1024)
        total_data += data_recv
        recv_size += len(data_recv)

    print(total_data.decode('utf-8'))


if __name__ == '__main__':
    main()


import socket
import struct

def main():
    # 创建套接字
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    server.bind(('127.0.0.1', 7890))

    server.listen(128)

    new_socket, addr = server.accept()

    content = """
    Windows IP 配置
    """

    res = struct.pack('i', len(content))
    new_socket.send(res)

    new_socket.send(content.encode('utf-8'))


if __name__ == '__main__':
    main()

HTTP协议

HTTP协议概念

URL的概念

查看HTTP通信过程

HTTP请求报文分析

GET / HTTP/1.1  # GET请求方式 请求资源路径 HTTP协议版本
---- 请求头 -----
Host: www.baidu.com  # 服务器的主机地址和端口号,默认是80
Connection: keep-alive # 和服务端保持长连接
Upgrade-Insecure-Requests: 1 # 让浏览器升级不安全请求,使用https请求
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36  # 用户代理,也就是客户端的名称
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 # 可接受的数据类型
Accept-Encoding: gzip, deflate # 可接受的压缩格式
Accept-Language: zh-CN,zh;q=0.9 #可接受的语言
Cookie: pgv_pvi=1246921728; # 登录用户的身份标识

---- 空行 ----
Host: www.baidu.com\r\n  
Connection: keep-alive\r\n
Upgrade-Insecure-Requests: 1\r\n
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36\r\n
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\r\n
Accept-Encoding: gzip, deflate\r\n
Accept-Language: zh-CN,zh;q=0.9\r\n
Cookie: pgv_pvi=1246921728; \r\n
\r\n  (请求头信息后面还有一个单独的’\r\n’不能省略)
POST /xmweb?host=mail.baidu.cn&_t=1542884567319 HTTP/1.1 # POST请求方式 请求资源路径 HTTP协议版本
---- 请求头 ----
Host: mail.itcast.cn # 服务器的主机地址和端口号,默认是80
Connection: keep-alive # 和服务端保持长连接
Content-Type: application/x-www-form-urlencoded  # 告诉服务端请求的数据类型
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36 # 客户端的名称
---- 空行 ----
---- 请求体 ----
username=hello&pass=hello # 请求参数

HTTP响应报文分析

HTTP/1.1 200 OK # HTTP协议版本 状态码 状态描述
--- 响应头 ---
Server: Tengine # 服务器名称
Content-Type: text/html; charset=UTF-8 # 内容类型
Transfer-Encoding: chunked # 发送给客户端内容不确定内容长度,发送结束的标记是0\r\n, Content-Length表示服务端确定发送给客户端的内容大小,但是二者只能用其一。
Connection: keep-alive # 和客户端保持长连接
Date: Fri, 23 Nov 2018 02:01:05 GMT # 服务端的响应时间
--- 空行 ---
--- 响应体 ---
<!DOCTYPE html><html lang=“en”> …</html> # 响应给客户端的数据
状态码说明
200请求成功
307重定向
400错误的请求,请求地址或者参数有误
404请求资源在服务器不存在
500服务器内部源代码出现错误

静态web服务器

什么是静态web服务器

搭建python自带的静态web服务器

搭建自己的静态web服务器

import socket

def main():
    tcp_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    tcp_server.bind(('192.168.0.104',8080))

    tcp_server.listen(128)

    new_socket,addr = tcp_server.accept()

    recv_request = new_socket.recv(1024).decode('utf-8')

    with open('index.html','r') as f:
        response_body = f.read()
    response_line = 'HTTP/1.1 200 OK\r\n'
    response_header = "Server: PWS/1.1\r\n"
    response_data = response_line + response_header + '\r\n' + str(response_body)

    new_socket.send(response_data.encode('utf-8'))

运行程序,并通过浏览器对自定义的服务器进行访问,可以得到如下结果:在这里插入图片描述

import socket

def main():
    tcp_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    tcp_server.bind(('192.168.0.104',8080))

    tcp_server.listen(128)

    while True:
        new_socket, addr = tcp_server.accept()

        recv_request = new_socket.recv(4096).decode('utf-8')
        print('recv_request:',recv_request)

        recv_request1 = recv_request.split(" ")
        print('split:',recv_request1)

        request_path = recv_request1[1]
        print('path:',request_path)

        try:
            with open('static'+request_path,'r',encoding='utf-8') as f:
                response_body = f.read()
        except Exception as e:
            print(e)
            response_line = 'HTTP/1.1 404 Not Found\r\n'
            response_header = "Server: PWS/1.1\r\n"
            with open('static/404.html','r',encoding='utf-8') as b:
                response_body = b.read()
            response_data = response_line + response_header + '\r\n' + response_body
            new_socket.send(response_data.encode('utf-8'))
        else:
            response_line = 'HTTP/1.1 200 OK\r\n'
            response_header = "Server: PWS/1.1\r\n"
            response_data = response_line + response_header + '\r\n' + str(response_body)

            new_socket.send(response_data.encode('utf-8'))
        finally:

            new_socket.close()

if __name__ == '__main__':
    main()


标签:协议,HTTP,socket,黏包,报文,服务器,response,请求
来源: https://blog.csdn.net/weixin_43973956/article/details/113680359