离开
skip_host
默认值,即。,
False
Host
putheader
导致发送
主持人
头两次(在本例中使用不同的值)。这可以通过设置
debuglevel
>>> import http.client
>>> connection = http.client.HTTPSConnection("duckduckgo.com", "443")
>>> connection.set_debuglevel(1)
>>> connection.putrequest("GET", "/")
>>> connection.putheader("User-Agent", "Python/3.8")
>>> connection.putheader("Host", "duckduckgo.com")
>>> connection.endheaders()
send: b'GET / HTTP/1.1\r\nHost: duckduckgo.com:443\r\nAccept-Encoding: identity\r\nUser-Agent: Python/3.8\r\nHost: duckduckgo.com\r\n\r\n'
>>>
>>> response = connection.getresponse()
reply: 'HTTP/1.1 400 Bad Request\r\n'
header: Server header: Date header: Content-Type header: Content-Length header: Connection header: X-XSS-Protection header: X-Content-Type-Options header: Referrer-Policy header: Expect-CT
在
http.client
's code
有人提到
主持人
putrequest
:
if not skip_host:
# this header is issued *only* for HTTP/1.1
# connections. more specifically, this means it is
# only issued when the client uses the new
# HTTPConnection() class. backwards-compat clients
# will be using HTTP/1.0 and those clients may be
# issuing this header themselves. we should NOT issue
# it twice; some web servers (such as Apache) barf
# when they see two Host: headers
您的代码可以通过添加
skip_host=True
或者不明确指定
标题。两者都会导致
主持人
>>> import http.client
>>> connection = http.client.HTTPSConnection("duckduckgo.com", "443")
>>> connection.putrequest("GET", "/", skip_host=True)
>>> connection.putheader("User-Agent", "Python/3.8")
>>> connection.putheader("Host", "duckduckgo.com")
>>> connection.endheaders()
>>> response = connection.getresponse()
>>> print(response.status, response.reason)
200 OK
>>> # OR
>>> connection = http.client.HTTPSConnection("duckduckgo.com", "443")
>>> connection.putrequest("GET", "/")
>>> connection.putheader("User-Agent", "Python/3.8")
>>> connection.endheaders()
>>> response = connection.getresponse()
>>> print(response.status, response.reason)
200 OK
至于要用哪一个
docs
似乎在暗示除非你有理由
主持人
标题(使用
putheader公司
)您可以依靠模块自动发送
主持人
跳过\u主机
默认值,即。,
,并且不指定
标题使用