开发者

Open URL using personal digital ceritificate with urllib2

开发者 https://www.devze.com 2023-02-22 22:40 出处:网络
I\'m trying to use urllib2 to open a web page using personal digital certificate. Actually by command line mode, using \"curl -k\", is possible to open this resource.

I'm trying to use urllib2 to open a web page using personal digital certificate.

Actually by command line mode, using "curl -k", is possible to open this resource.

So my question is:

1) Is possible to open this web page using urllib2 bypassing the use of personal digital certificate?

2) If the option (1) is not possible, how can access this resource using urllib2 and "personal digital certificate".

P.S. the code I'm trying to use in order to access this resource is the following:


class HTTPSClientAuthHandler(urllib2.HTTPSHandler):
    def init(self, key, cert):
        urllib2.HTTPSHandler.init(self)
        self.key = key
        self.cert = cert

def https_open(self, req):
    return self.do_open(self.getConnection, req)

def getConnection(self, host, timeout=300):
    return httplib.HTTPSConnection(host, key_file=self.key, cert_file=self.cert)

opener = urllib2.build_opener(HTTPSClientAuthHandler('/Users/antonio/.globus/userkey.pem','/Users/antonio/.globus/usercert.pem') ) response = opener.open("https://........") print response.read()

The error I got is:


Traceback (most recent call last):
  File "HTTPSClientAuthHandler.py", line 18, in 
    response = opener.open("https://cmsweb.cern.ch/tier0/express_config")
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 389, in open
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 502, in http_response
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 427, in error
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 361, in _call_chain
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 510, in http_error_default
urllib2.HTTPError: HTTP Error开发者_开发知识库 500: Internal Server Error


finally I solved the problem.

I did access https service bypassing the use of personal digital certificate simply in this way:

txdata = None
txheaders = {
    'Accept': 'text/html'
}
req = urllib2.Request(url, txdata, txheaders)

Any ideas why the headers 'Accept': 'text/html' allows connections to SSL sites without certs?

Maybe it depends by the server settings.


I'm not familiar with pycurl, but here are some links that might help:

problem sending an HTTPGET with PyCurl (note in particular the suggestion to use --libcurl example.c to get the libcurl implementation)

http://code.google.com/p/friendlycurl/

http://www.phpfreaks.com/forums/index.php?topic=270222.0


What you have worked fine for me with one exception. The init must have the double __ before and after since it is a special function that initializes the class. This will always be the same function in every class:

class HTTPSClientAuthHandler(urllib2.HTTPSHandler): def _init_(self, key, cert): urllib2.HTTPSHandler._init_(self) self.key = key self.cert = cert

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号