<p>如果你知道你想要得到的网站是一个“好人”,你可以尝试创建这样的“开场白”:</p>
<pre><code>import httplib2
if __name__ == "__main__":
h = httplib2.Http(".cache", disable_ssl_certificate_validation=True)
resp, content = h.request("https://site/whose/certificate/is/bad/", "GET")
</code></pre>
<p>(有趣的部分是<code>disable_ssl_certificate_validation=True</code>)</p>
<p>从文档中:
<a href="http://bitworking.org/projects/httplib2/doc/html/libhttplib2.html#httplib2.Http" rel="nofollow noreferrer">http://bitworking.org/projects/httplib2/doc/html/libhttplib2.html#httplib2.Http</a></p>
<p><strong>编辑01:</strong></p>
<p>既然您的问题实际上是为什么会发生这种情况,那么您可以检查<a href="http://www.sslshopper.com/ssl-certificate-errors.html" rel="nofollow noreferrer">this</a>或<a href="http://en.wikipedia.org/wiki/X.509#Certificates" rel="nofollow noreferrer">this</a>。</p>
<p><strong>编辑02:</strong></p>
<p>鉴于访问这个答案的人比我预期的多,我想解释一下<em>何时</em>禁用证书验证可能有用。</p>
<p>首先,对这些证书的工作原理有一点浅显的了解。在上面提供的链接中有很多信息,但不管怎样,还是在这里。</p>
<p>SSL证书需要由一个众所周知的(至少对您的浏览器来说是众所周知的)<a href="https://en.wikipedia.org/wiki/Certificate_authority" rel="nofollow noreferrer">Certificate Authority</a>来验证。您通常从其中一个机构购买整个证书(<a href="https://www.symantec.com/ssl-certificates/" rel="nofollow noreferrer">Symantec</a>,<a href="https://www.godaddy.com/web-security/ssl-certificate" rel="nofollow noreferrer">GoDaddy</a>…)</p>
<p>广义地说,其思想是:那些证书颁发机构(CA)给您一个证书,其中还包含CA信息。您的浏览器有一个众所周知的ca列表,因此当您的浏览器收到证书时,它将执行如下操作:<em>“hmmmmmmm。。。。</em><sub>[浏览器在这里做了一个令人惊奇的脸]</sub><em>。。。我收到了一份证书,上面说是赛门铁克验证的。我认识那个“赛门铁克”吗?</em><sub>[然后浏览器转到其著名的CA列表并检查Symantec]</sub><em>哦,是的!我愿意。好的,证书很好!</em></p>
<p>如果在浏览器中单击URL旁边的小锁,您可以自己看到该信息:</p>
<p><a href="https://i.stack.imgur.com/qXBED.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qXBED.png" alt="Chrome certificate information"/></a></p>
<p>但是,在有些情况下,您只想测试HTTPS,然后使用几个<a href="https://jamielinux.com/docs/openssl-certificate-authority/create-the-root-pair.html" rel="nofollow noreferrer">command line</a>工具创建自己的证书颁发机构,并使用“自定义”CA来签署您刚刚生成的“自定义”证书,对吧?在这种情况下,您的浏览器(顺便说一句,问题是<code>httplib2.Http</code>)不会将您的“自定义”CA放在受信任的CA列表中,因此它会说证书无效。信息仍将加密传输,但浏览器告诉您的是,它并不完全信任加密传输到您所假设的位置。</p>
<p>例如,假设您为您的<code>localhost</code><a href="https://en.wikipedia.org/wiki/Fully_qualified_domain_name" rel="nofollow noreferrer">FQDN</a>创建了一组自定义密钥和CA以及<a href="https://blog.didierstevens.com/2008/12/30/howto-make-your-own-cert-with-openssl/" rel="nofollow noreferrer">this tutorial</a>之后的所有mambo jumbo,并且您的CA证书文件位于当前目录中。使用自定义证书和其他东西,您很可能在<code>https://localhost:4443</code>上运行服务器。现在,您的CA证书文件位于当前目录中的文件<code>./ca.crt</code>(与运行Python脚本的目录相同)。你可以这样使用<code>httplib2</code>:</p>
<pre><code>h = httplib2.Http(ca_certs='./ca.crt')
response, body = h.request('https://localhost:4443')
print(response)
print(body)
</code></pre>
<p>。。。你不会再看到警告了。为什么?因为你让<code>httplib2</code>去找CA的证书<code>./ca.crt</code>)</p>
<p>但是,由于Chrome(引用浏览器)不知道这个CA的证书,它将认为它无效:</p>
<p><a href="https://i.stack.imgur.com/lmftY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lmftY.png" alt="enter image description here"/></a></p>
<p>同时,证书过期。你有可能在一家公司工作,该公司使用内部站点进行SSL加密。它可以正常工作一年,然后你的浏览器开始抱怨。你去找负责保安的人,问他“哟!!我收到警告了!怎么了?”答案很可能是“哦,天哪!!我忘了换证书了!没关系,从现在开始接受它,直到我解决它。”</em>(真实的故事,尽管我得到的答案中有脏话:<strong>:-D</strong>)</p>