>我需要能够指定SSL证书CA根,但能够使用Python 2.7.10 urllib2
库插入HTTP cookie
ssl_handler = urllib2.HTTPSHandler()
opener = urllib2.build_opener(ssl_handler)
opener.addheaders.append(("Cookie","foo=blah"))
res = opener.open(https://example.com/some/info)
我知道urllib2支持cafile
参数,我应该在我的代码中在哪里使用它?
urlopen
文档:
urllib2.urlopen(url[, data[, timeout[, cafile[, capath[, cadefault[, context]]]]])
所以,请尝试:
urllib2.urlopen("https://example.com/some/info", cafile="test_cert.pem")
或
cxt = ssl.create_default_context(cafile="/path/test_cert.pem")
urllib2.urlopen("https://example.com/some/info", context=cxt)
根据文档,指定 CA 文件的功能是在 python 2.7.9 中添加的,并且仅在 urlopen 调用中可用,如前面的答案中所述。
所以你确实需要将opener.open()更改为urllib2.urlopen。为了使它仍然使用开瓶器,请调用urllib2.install_opener(开瓶器)在 urlopen 调用之前
这是我发现拥有所有(cookie和登录身份验证和CA证书指定)的唯一方法。