Python请求模块 - 仅发送指定的标头

ani*_*mwr 3 python http-headers python-requests

我需要向仅包含"主机"和"内容长度"标头的网络服务器发送帖子请求.我在字典中指定了这两个标题,我将其传递给请求模块,但它添加了"接受","接受编码","用户代理"标题.

Python代码:

headers = {'Content-Length': content_length, 'Host': 'Server-1:8080'}
r = requests.post(url, data=data, headers=headers)
print(r.request.headers)
Run Code Online (Sandbox Code Playgroud)

发送的实际请求标头:

{'Accept': '*/*', 'Host': 'Server-1:8080', 'Content-Length': '3072', 'User-Agent': 'python-requests/2.6.0 CPython/3.4.1 Windows/7', 'Connection': 'keep-alive, 'Accept-Encoding': 'gzip, deflate'}
Run Code Online (Sandbox Code Playgroud)

如何限制请求模块发送的标头?

Jam*_*ull 5

通过阅读文档,它们看起来像是会话级参数,您可以通过将其值设置为强制来省略这些参数None.

headers = {'Content-Length': content_length, 'Host': 'Server-1:8080',
           'User-Agent': None, 'Connection': None, 'Accept-Encoding': None}
Run Code Online (Sandbox Code Playgroud)

  • 将其他标题设置为无效! (2认同)