Python要求通过SSL认证的curl等效于'--proxy-header' [英] Python requests equivalent of '--proxy-header' in curl with SSL certification

查看:209
本文介绍了Python要求通过SSL认证的curl等效于'--proxy-header'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考:如何在请求中指定 --proxy-headers curl参数的等效项?

我是Python的新手。

I am a newbie vis-a-vis Python.

我有一个要求,即到目的地的请求(网页)必须通过代理服务器。

I have a requirement, where a request to a destination(webpage) must go through a proxy server.


  • 我需要将标头传递给代理服务器 (与--proxy-header相同)

  • 需要添加 SSL证书(。cer文件)以读取传递给代理服务器的标头(中间人场景) on CONNECT

  • I need to pass headers to the "Proxy server" (same as --proxy-header of curl)
  • Need to add an SSL certificate (a '.cer' file) to read the passed headers to the proxy server(a 'Man In the Middle' scenario) on CONNECT.

与我的要求等效的卷曲如下:

The curl equivalent of my requirement is as follows:

curl -k --verbose --cacert /proxy/cert/folder/proxy-certificate.cer --proxy-header "header1: value1" --proxy 'http://localhost:8080/' 'https://destination.com'

我确实遇到了一个类似的示例如何在请求中指定 --proxy-headers curl参数的等效项? 。但是我不确定如何将其与SSL证书合并。

I did come across a similar example How does one specify the equivalent of `--proxy-headers` curl argument into requests?. But I am unsure how to incorporate this with an SSL certificate.

我的代码:


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)')))


推荐答案

这不是解决方案:

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

When i usually encounter certificate/verification errors, i just force it to not verify the certificate using the code below:

conda config --set ssl_verify false

请注意,通常不建议这样做,我通常会暂时这样做,直到完成运行特殊脚本或下载库为止或者。如果您想尝试一下,并且对您有用,请记住使用以下代码将其重新打开:

Note that this is not usually recommended and i usually do it temporarily until i finish either running spicific script or downloading a library or so. If you want to try this and if it works for you, remember to turn it back on once done using the code below:

conda config --set ssl_verify true

这篇关于Python要求通过SSL认证的curl等效于'--proxy-header'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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