【发布时间】:2019-12-18 06:22:39
【问题描述】:
参考:How does one specify the equivalent of `--proxy-headers` curl argument into requests?
我是 Python 的新手。
我有一个要求,对目的地(网页)的请求必须通过代理服务器。
- 我需要将标头传递给“代理服务器”(与 curl 的 --proxy-header 相同)
- 需要添加 SSL 证书(一个“.cer”文件)来读取传递的标头到代理服务器(一个“中间人”场景)CONNECT强>。
我要求的 curl 等效如下:
curl -k --verbose --cacert /proxy/cert/folder/proxy-certificate.cer --proxy-header "header1: value1" --proxy 'http://localhost:8080/' 'https://destination.com'
我确实遇到过类似的例子How does one specify the equivalent of `--proxy-headers` curl argument into requests?。但我不确定如何将其与 SSL 证书合并。
我的代码:
proxyheaders = { 'http://localhost:9090/': { 'header1': 'value1' } }
class ProxyHeaderAwareHTTPAdapter(requests.adapters.HTTPAdapter):
def proxy_headers(self, proxy):
if proxy in proxyheaders:
return proxyheaders[proxy]
else:
return None
s = requests.Session()
s.mount('http://', ProxyHeaderAwareHTTPAdapter())
s.mount('https://', ProxyHeaderAwareHTTPAdapter())
URL = "https://stackoverflow.com/"
cert_file_path = "/Path/to/certificate/proxy-certificate.cer"
try:
s.get(URL, verify=cert_file_path)
except Exception as e:
print(e)
我收到以下错误:
HTTPSConnectionPool(host='stackoverflow.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)')))
【问题讨论】:
标签: python-3.x curl python-requests ssl-certificate