我要声明标题:
headers = {
"Host": "somehost.somedomain.com",
"Authorization": "Basic %s" % base64.b64encode(authorization)
}
然后调用我的网址:
conn = httplib.HTTPSConnection(host_name, 443, timeout=timeout, context=ssl._create_unverified_context())
conn.timeout = timeout
conn.request("GET", url, "", headers)
授权有效,但是在我的Nginx日志中,我看到server_name为空白
{ "@timestamp": "2019-01-21T10:32:40+00:00", "client": "172.19.0.1", "server_name": "_", "server_port": "443",
如果我卷发-H可以
curl -H "Host: somehost.somedomain.com"
{ "@timestamp": "2019-01-21T10:32:21+00:00", "client": "172.19.0.1", "server_name": "somehost.somedomain.com", "server_port": "443",
答案 0 :(得分:0)
我想您可能会在Python代码之外的其他地方丢失from base64 import b64encode
from httplib import HTTPSConnection
import ssl
authorization = b"user:pass"
headers = {
"Host": "somehost.somedomain.com",
"Authorization": "Basic %s" % b64encode(authorization)
}
conn = HTTPSConnection(
'www.google.com', 443, timeout=30,
context=ssl._create_unverified_context())
conn.set_debuglevel(1)
conn.request("GET", "/", None, headers)
resp = conn.getresponse()
print resp.status, resp.reason
conn.close()
标头,它看起来很正确,并且对我来说是正确的事情
解决了几个问题,并将其转变为working example:
body
注意:我借此机会用request
将None
的{{1}}参数替换为set_debuglevel
-您正在传递一个空字符串,该字符串是一个空主体而不是没有身体。
send: 'GET / HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: somehost.somedomain.com\r\nAuthorization: Basic dXNlcjpwYXNz\r\n\r\n'
reply: 'HTTP/1.1 404 Not Found\r\n'
header: Content-Type: text/html; charset=UTF-8
header: Referrer-Policy: no-referrer
header: Content-Length: 1561
header: Date: Mon, 21 Jan 2019 11:50:28 GMT
header: Alt-Svc: quic=":443"; ma=2592000; v="44,43,39,35"
404 Not Found
导致库在运行时将协议级别的输出转储到控制台,因此产生如下输出:
404 Not Found
请注意,此响应是从Google返回的,显然是失败的,因此send:
返回了。
您可以从Host
行中看到它正在发送适当的{{1}}标头。