gpt4 book ai didi

python - 如何让 python httplib 接受不受信任的证书?

转载 作者:IT老高 更新时间:2023-10-28 21:06:24 28 4
gpt4 key购买 nike

如何让 python httplib 接受不受信任的证书?我在我的网络服务器上创建了一个蛇油/自签名证书,我的 python 客户端无法连接,因为我使用的是不受信任的证书。

我宁愿在我的客户端代码中解决这个问题,而不是让它在我的系统上受信任。

import httplib


def main():
conn = httplib.HTTPSConnection("127.0.0.1:443")
conn.request("HEAD","/")
res = conn.getresponse()
print res.status, res.reason
data = res.read()
print len(data)


if __name__ == "__main__":
main()

最佳答案

我的一些脚本在更新我的计算机后停止工作。原来,这就是问题所在:https://docs.python.org/2/library/httplib.html#httplib.HTTPSConnection

Changed in version 2.7.9: context was added.

This class now performs all the necessary certificate and hostname checks by default. To revert to the previous, unverified, behavior ssl._create_unverified_context() can be passed to the context parameter.

因此,如果您的 Python 版本 >= 2.7.9(在我的情况下为 2.7.10),您可能会遇到这种情况。为了解决这个问题,我更新了我的电话:

httplib.HTTPSConnection(hostname, timeout=5, context=ssl._create_unverified_context())

这可能是保持相同行为的最简单更改。

关于python - 如何让 python httplib 接受不受信任的证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5319430/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com