Add SSL CA file using urllib2

I need to specify the root CA key of the SSL certificate, but you will be able to insert the HTTP cookie with Python 2.7.10 urllib2

library

ssl_handler = urllib2.HTTPSHandler()
opener = urllib2.build_opener(ssl_handler)
opener.addheaders.append(("Cookie","foo=blah"))
res = opener.open(https://example.com/some/info)

      

I know urllib2 supports cafile

param, where should I use it in my code?

+4


source to share


2 answers


Documentationurlopen

:

urllib2.urlopen (url [, data [, timeout [, cafile [, capath [, cadefault [, context]]]]])

so please try:



urllib2.urlopen("https://example.com/some/info", cafile="test_cert.pem")

      

or

cxt = ssl.create_default_context(cafile="/path/test_cert.pem")
urllib2.urlopen("https://example.com/some/info", context=cxt)

      

+3


source


The ability to specify a CA file was added in python 2.7.9 according to the documentation and is only available in the urlopen call as stated in the previous answer.

So, you need to change opener.open () to urllib2.urlopen. To make it still use the opener call urllib2.install_opener (knife) before calling urlopen



This is the only way I found that everything (cookie confirmation and login authorization and CA certificate)

+2


source







All Articles