Python 请求:无效的标头名称 [英] Python Requests: Invalid Header Name

查看:23
本文介绍了Python 请求:无效的标头名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发送一个带有标题的请求::hello".但是,前导冒号会导致脚本无法正常运行,并发出此回溯:

I am trying to send a request with the header: ":hello". However, the leading colon causes the script to not function properly, and emit this traceback:

Traceback (most recent call last):

(为了我的隐私删除了前几行)

(first few lines removed for my privacy)

File "C:Python27libsite-packages
equestsapi.py", line 109, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "C:Python27libsite-packages
equestsapi.py", line 50, in request
    response = session.request(method=method, url=url, **kwargs)
  File "C:Python27libsite-packages
equestssessions.py", line 468, in request
    resp = self.send(prep, **send_kwargs)
  File "C:Python27libsite-packages
equestssessions.py", line 576, in send
    r = adapter.send(request, **kwargs)
  File "C:Python27libsite-packages
equestsadapters.py", line 370, in send
    timeout=timeout
  File "C:Python27libsite-packages
equestspackagesurllib3connectionpool.py", line 559, in urlopen
    body=body, headers=headers)
  File "C:Python27libsite-packages
equestspackagesurllib3connectionpool.py", line 353, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "C:Python27libhttplib.py", line 1057, in request
    self._send_request(method, url, body, headers)
  File "C:Python27libhttplib.py", line 1096, in _send_request
    self.putheader(hdr, value)
  File "C:Python27libhttplib.py", line 1030, in putheader
    raise ValueError('Invalid header name %r' % (header,))
ValueError: Invalid header name ':hello'

是否有解决方法?我的脚本是:

Is there a workaround for this? My script is:

import requests
headers = {'user-agent': 'alsotesting', ':hello': 'test'}
requests.post("my server", headers=headers)

推荐答案

正如您的错误所说,:header 不是有效的 HTTP 标头名称(您不能以:"开头标头 -请参阅文档).你应该改变

As your error says, :header is not a valid HTTP header name (you cannot start headers with ":" - see documentation). You should change

headers = {'user-agent': 'alsotesting', ':hello': 'test'}

headers = {'user-agent': 'alsotesting', 'hello': 'test'}

HTTP/2 使用以冒号开头的伪标头字段(参见 文档).此外,如此处所述,您可能会在 Chrome 的开发人员中看到一些以冒号开头的标题工具,当 Chrome 使用 SPDY 与 Web 服务器通信时可能会发生这种情况 - 以及 HTTP/2(基于 SPDY/2),它们对应于伪标头.如文档中所述,伪标头字段不是 HTTP 标头字段.

HTTP/2 uses pseudo-headers fields, which start with a colon (see documentation). Also, as explained here, you may see some headers starting with a colon in Chrome's Developer Tools, which can happen when Chrome is talking to a web server using SPDY - and also HTTP/2 (which is based on SPDY/2), which correspond to pseudo-headers. As stated in documentation, pseudo-header fields are not HTTP header fields.

总而言之,标准 HTTP 协议不允许以冒号开头的标头字段,这就是您收到 Invalid header name 错误的原因

In conclusion, header fields starting with a colon are not allowed with standard HTTP protocol, so that is why you are getting the Invalid header name error

这篇关于Python 请求:无效的标头名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆