使用 urllib2 使用个人数字证书打开 URL

发布于 2024-10-31 13:06:26 字数 1731 浏览 0 评论 0原文

我正在尝试使用 urllib2 使用个人数字证书打开网页。

实际上通过命令行模式,使用“curl -k”,可以打开这个资源。

所以我的问题是:

1)是否可以使用 urllib2 绕过个人数字证书打开此网页?

2)如果选项(1)不可行,如何使用urllib2和“个人数字证书”访问此资源。

聚苯乙烯 我尝试使用来访问此资源的代码如下:


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') ) 响应 = opener.open("https://........") 打印响应.read()

我得到的错误是:


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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你对谁都笑 2024-11-07 13:06:26

最后我解决了这个问题。

我确实绕过了个人数字证书访问https服务
简单地这样:

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

有什么想法为什么标头 'Accept': 'text/html' 允许在没有证书的情况下连接到 SSL 站点?

也许这取决于服务器设置。

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.

无声无音无过去 2024-11-07 13:06:26

我不熟悉 pycurl,但这里有一些可能有帮助的链接:

问题发送使用 PyCurl 进行 HTTPGET
(特别注意使用 --libcurl example.c 来获取 libcurl 实现的建议)

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

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

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

与之呼应 2024-11-07 13:06:26

除了一个例外,你对我来说效果很好。 init 前后必须有双 __,因为它是初始化类的特殊函数。每个类中的函数始终相同:

class HTTPSClientAuthHandler(urllib2.HTTPSHandler):
def _init_(self, key, cert):
urllib2.HTTPSHandler._init_(自身)
self.key = 密钥
self.cert = 证书

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文