You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python中重放原始HTTP请求?

重放原始HTTP请求的Python解决方案

针对你需要自动解析原始HTTP请求、同时保留TLS和代理支持的需求,以下是几个实用的解决方案,无需手动拆分请求行和请求头:

1. 使用 httpx 直接发送原始请求

httpx 是支持HTTP/1.1和HTTP/2的现代HTTP客户端,原生支持直接传入原始请求字符串,自动处理解析、TLS加密和代理配置:

import httpx

# 你的原始HTTP请求文本
raw_request = """GET / HTTP/1.1
Host: google.de
Cookie: CONSENT=PENDING+071; AEC=ARSKqsKvfEvPS9Vp1bDM6YMPdHpVOoH357W-7q3cqtdDwYeHf3MqPEO1xA; SOCS=CAISHAgBEhJnd3NfMjAyMzAyMjMtMF9SQzEaAmRlIAEaBgiAn-WfBg; NID=511=N6YvXcWd_hnVVnV8w6JK4jscqE2pEt8MuTrw3yZJp-84ZxV6RJLee_yj2DEo2UJuOse0sqLjdnAD7qgPw9al7aEJqsQOCAQPIs21rLy5HQ5IAoObj7icI7ayKJttejI9Va2jDFkk0ZLvUC7P_VPJuxRJyhvLspqU1YVUcYCThrYizbo; 1P_JAR=2023-2-25-20
Sec-Ch-Ua: "Not A(Brand";v="24", "Chromium";v="110"
Sec-Ch-Ua-Mobile: ?0
Sec-Ch-Ua-Platform: "Linux"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.5481.78 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
X-Client-Data: CO3/ygE=
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Connection: close
"""

# 配置代理(不需要可删除proxies参数)
proxies = {"http": "http://your-proxy:port", "https": "http://your-proxy:port"}

with httpx.Client(proxies=proxies) as client:
    # 发送原始请求
    response = client.send(httpx.Request("GET", "https://google.de"), content=raw_request.encode('utf-8'))
    print(f"状态码: {response.status_code}")
    print(f"响应内容预览: {response.text[:500]}")

2. 用 requests 配合标准库解析原始请求

如果你更习惯使用requests,可以借助Python标准库http.client自动解析原始请求文本,提取方法、路径、请求头和请求体,再生成requests预请求对象:

import requests
from http.client import HTTPMessage

def parse_raw_request(raw_request):
    # 拆分请求行与请求头
    lines = raw_request.split('\r\n')
    method, path, _ = lines[0].split()
    
    # 解析请求头
    msg = HTTPMessage()
    for line in lines[1:]:
        if not line:
            break
        key, value = line.split(': ', 1)
        msg.add_header(key, value)
    
    # 提取请求体(如果存在)
    body = '\r\n'.join(lines[lines.index('')+1:]) if '' in lines else ''
    return method, path, dict(msg.items()), body

# 你的原始请求文本
raw_request = """GET / HTTP/1.1
Host: google.de
Cookie: CONSENT=PENDING+071; AEC=ARSKqsKvfEvPS9Vp1bDM6YMPdHpVOoH357W-7q3cqtdDwYeHf3MqPEO1xA; SOCS=CAISHAgBEhJnd3NfMjAyMzAyMjMtMF9SQzEaAmRlIAEaBgiAn-WfBg; NID=511=N6YvXcWd_hnVVnV8w6JK4jscqE2pEt8MuTrw3yZJp-84ZxV6RJLee_yj2DEo2UJuOse0sqLjdnAD7qgPw9al7aEJqsQOCAQPIs21rLy5HQ5IAoObj7icI7ayKJttejI9Va2jDFkk0ZLvUC7P_VPJuxRJyhvLspqU1YVUcYCThrYizbo; 1P_JAR=2023-2-25-20
Sec-Ch-Ua: "Not A(Brand";v="24", "Chromium";v="110"
Sec-Ch-Ua-Mobile: ?0
Sec-Ch-Ua-Platform: "Linux"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.5481.78 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
X-Client-Data: CO3/ygE=
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Connection: close
"""

method, path, headers, body = parse_raw_request(raw_request)
url = f"https://{headers['Host']}{path}"

# 配置代理(不需要可删除proxies参数)
proxies = {"http": "http://your-proxy:port", "https": "http://your-proxy:port"}

with requests.Session() as s:
    req = requests.Request(method, url, headers=headers, data=body.encode('utf-8'))
    prepped = req.prepare()
    # 发送请求,支持TLS验证、代理等参数
    resp = s.send(prepped, proxies=proxies, verify=True)
    print(f"状态码: {resp.status_code}")
    print(f"响应内容预览: {resp.text[:500]}")

3. 使用专门的 rawhttp 库

rawhttp 是专为处理原始HTTP请求设计的第三方库,支持直接发送原始请求字符串,同时内置TLS和代理支持:

先安装库:

pip install rawhttp

使用示例:

from rawhttp import RawHTTP

raw_request = """GET / HTTP/1.1
Host: google.de
Cookie: CONSENT=PENDING+071; AEC=ARSKqsKvfEvPS9Vp1bDM6YMPdHpVOoH357W-7q3cqtdDwYeHf3MqPEO1xA; SOCS=CAISHAgBEhJnd3NfMjAyMzAyMjMtMF9SQzEaAmRlIAEaBgiAn-WfBg; NID=511=N6YvXcWd_hnVVnV8w6JK4jscqE2pEt8MuTrw3yZJp-84ZxV6RJLee_yj2DEo2UJuOse0sqLjdnAD7qgPw9al7aEJqsQOCAQPIs21rLy5HQ5IAoObj7icI7ayKJttejI9Va2jDFkk0ZLvUC7P_VPJuxRJyhvLspqU1YVUcYCThrYizbo; 1P_JAR=2023-2-25-20
Sec-Ch-Ua: "Not A(Brand";v="24", "Chromium";v="110"
Sec-Ch-Ua-Mobile: ?0
Sec-Ch-Ua-Platform: "Linux"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.5481.78 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
X-Client-Data: CO3/ygE=
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Connection: close
"""

# 发送HTTPS请求,配置代理(不需要可删除proxy参数)
response = RawHTTP().send(raw_request, url='https://google.de', proxy='http://your-proxy:port')
print(f"状态行: {response.status_line}")
print(f"响应头: {dict(response.headers)}")
print(f"响应内容预览: {response.body[:500]}")

以上方案均无需手动解析请求头,同时保留了TLS加密和代理支持,可直接满足你重放原始HTTP请求的需求。

内容的提问来源于stack exchange,提问作者CryptoPerson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 16:37:28