httplib2.Http.request()的参数

时间:2013-10-17 08:20:53

标签: python parameters ssl-certificate httplib2

我想知道httplib2.Http类的请求方法采用什么参数。我正在尝试使用一个简单的get方法,但似乎我使用的url提供了需要验证的证书,因为我收到了SSL3_GET_SERVER_CERTIFICATE错误。这就是为什么我想知道是否使用httplib2库我们可以告诉我们忽略证书验证的请求(比如urllib.request的 unverifiable 参数)。 这是我的代码:

   import httplib2
   h = httplib2.Http(".cache")
   h.credentials_add(username, password)
   resp, content = h.request(url, "GET")

1 个答案:

答案 0 :(得分:4)

您可以使用disable_ssl_certificate_validation忽略证书验证。请查看下面的示例。但是,由于以下错误,它无法与 Python 3.x 一起使用。因此,您需要按照此问题的评论中的建议更新Python33\Lib\site-packages\httplib2\__init__.py

HTTPS request doesn't work when disable_ssl_certificate_validation = True

import httplib2

h = httplib2.Http(".cache", disable_ssl_certificate_validation=True)
resp, content = h.request("https://github.com/", "GET")

Python 3.x的补丁:

diff --git a/__init__.py b/__init__.py
index 65f90ac..4495994 100644
--- a/__init__.py
+++ b/__init__.py
@@ -823,10 +823,13 @@ class HTTPSConnectionWithTimeout(http.client.HTTPSConnection):
                 context.load_cert_chain(cert_file, key_file)
             if ca_certs:
                 context.load_verify_locations(ca_certs)
+        check_hostname = True
+        if disable_ssl_certificate_validation:
+            check_hostname = False
         http.client.HTTPSConnection.__init__(
                 self, host, port=port, key_file=key_file,
                 cert_file=cert_file, timeout=timeout, context=context,
-                check_hostname=True)
+                check_hostname=check_hostname)


 SCHEME_TO_CONNECTION = {
相关问题