【问题标题】:Python requests equivalent of '--proxy-header' in curl with SSL certificationPython 在 curl 中请求等效于“--proxy-header”的 SSL 认证
【发布时间】: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


    【解决方案1】:

    这不是一个解决方案:

    当我通常遇到证书/验证错误时,我只是使用下面的代码强制它不验证证书:

    conda config --set ssl_verify false
    

    请注意,通常不建议这样做,我通常会暂时这样做,直到我完成运行特定脚本或下载库等。如果你想试试这个,如果它适合你,记得使用下面的代码重新打开它:

    conda config --set ssl_verify true
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-13
      • 1970-01-01
      相关资源
      最近更新 更多