【问题标题】:Using client certificates with urllib2将客户端证书与 urllib2 一起使用
【发布时间】:2010-12-24 21:46:13
【问题描述】:

我需要在我的服务器和远程 Web 服务之间创建一个安全通道。我将使用带有客户端证书的 HTTPS。我还需要验证远程服务提供的证书。

  1. 如何将我自己的客户端证书与 urllib2 一起使用?

  2. 我需要在我的代码中做些什么来确保远程证书是正确的?

【问题讨论】:

    标签: python ssl certificate urllib2 mutual-authentication


    【解决方案1】:

    因为 alex 的答案是一个链接,并且该页面上的代码格式不正确,所以我将把它放在这里以供后代使用:

    import urllib2, httplib
    
    class HTTPSClientAuthHandler(urllib2.HTTPSHandler):
        def __init__(self, key, cert):
            urllib2.HTTPSHandler.__init__(self)
            self.key = key
            self.cert = cert
    
        def https_open(self, req):
            # Rather than pass in a reference to a connection class, we pass in
            # a reference to a function which, for all intents and purposes,
            # will behave as a constructor
            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('/path/to/file.pem', '/path/to/file.pem.') )
    response = opener.open("https://example.org")
    print response.read()
    

    【讨论】:

    • 非常感谢您!几天来,我一直在使用私有证书和证书颁发机构使用 python 中的请求库连接到 Apache 服务器来追踪一个错误。我可以验证我的证书是否在浏览器中工作,但是每次都失败并出现“握手错误”我最终使用上面的类来证明客户端证书正在工作并正确验证。原来,确切地说,我使用 1.2.3 的请求库版本中有一个错误。希望此评论对遇到相同问题的其他人有所帮助。
    • 我是 Python 新手,所以我想知道 - 这里何时调用 https_open() 函数?如果可能请描述这段代码的执行流程是什么?
    • 所以我已按照这些说明进行操作,但在使用 python 2.7.9 或更高版本时出现 urlopen 错误:“ssl certificate verify failed”。我相信我需要用我自己的证书链添加一个 SSLContext,但我不是 100% 肯定,你介意在你的例子中提供任何建议或提示吗?谢谢!
    【解决方案2】:

    这是一个看起来相关的bug in the official Python bugtracker,并且有一个建议的补丁。

    【讨论】:

      【解决方案3】:

      根据 Antoine Pitrou 对 Hank Gay 的回答中链接的问题的回应,这可以通过使用包含的 ssl 库在一定程度上简化(截至 2011 年):

      import ssl
      import urllib.request
      
      context = ssl.create_default_context()
      context.load_cert_chain('/path/to/file.pem', '/path/to/file.key')
      opener = urllib.request.build_opener(urllib.request.HTTPSHandler(context=context))
      response = opener.open('https://example.org')
      print(response.read())
      

      (Python 3 代码,但ssl 库也可用于 Python 2)。

      load_cert_chain 函数还接受可选的密码参数,允许加密私钥。

      【讨论】:

      【解决方案4】:

      【讨论】:

      猜你喜欢
      • 2013-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-23
      • 1970-01-01
      • 1970-01-01
      • 2020-01-23
      • 1970-01-01
      相关资源
      最近更新 更多