python curl headers解析,Python要求通过SSL认证的curl等效于'--proxy-header'

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.

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'

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.

My Code:

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)

I get the following error:

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

解决方案

THIS IS NOT A SOLUTION:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值