【问题标题】:Verification of ssl certificates failed after compiling .py to .exe将 .py 编译为 .exe 后验证 ssl 证书失败
【发布时间】:2016-10-08 02:22:09
【问题描述】:

我有一个读取 URL 的脚本。如果我使用python app.py 将它运行到 cmd,它工作正常。但是在使用 py2exe 编译它并运行 .exe 之后,我收到了这个错误

entering main now.....
Traceback (most recent call last):
  File "strtojson.py", line 257, in <module>
  File "strtojson.py", line 209, in main
  File "requests\api.pyc", line 70, in get
  File "requests\api.pyc", line 56, in request
  File "requests\sessions.pyc", line 475, in request
  File "requests\sessions.pyc", line 596, in send
  File "requests\adapters.pyc", line 497, in send
requests.exceptions.SSLError: [Errno 2] No such file or directory

这是我访问该网站的方式。

url = 'https://www.sec.gov/cgi-bin/browse-edgar?company=&CIK=&type=8-k&owner=include&count=100&action=getcurrent'
response = requests.get(url)
html = response.content
soup = BeautifulSoup(html, "html.parser")

这是我的setup.py

from distutils.core import setup
import py2exe
setup(console=['strtojson.py'], options = {'py2exe' : {'packages': ['bs4', 'requests']}})

我在 Win 7 64 位上使用 Python 2.7.12

【问题讨论】:

    标签: python ssl ssl-certificate python-requests


    【解决方案1】:

    我的猜测是 Python 不会再找到受信任的 CA(需要验证服务器证书),因为它们不包含在 exe 中。在这种情况下,使用 requests.getverify=False 应该可以工作。

    但这应该只用于验证这确实是问题的原因。更好的解决方法是将必要的受信任 CA 放入文件中,将此文件与 exe 一起发送,然后使用 verify='trusted-ca.pem'。您可以通过 https://curl.haxx.se/docs/caextract.html 获取广泛使用的可信 CA 集合。

    虽然让requests 使用一些字符串作为受信任的 CA 会很好,但我不认为这是受支持的。您当然可以将受信任的 CA 作为字符串包含在 exe 中,然后在调用 requests.get(..., verify='temporary-ca-file.pem') 之前从中创建一个临时文件。

    【讨论】:

    • 感谢您的建议。实际上我切换到 urrlib2 并没有给我这个错误。 @Steffen
    • @wasp:据我所知,urllib2 默认情况下根本不进行证书验证。不是验证意味着没有错误,但这当然意味着这是不安全的。
    猜你喜欢
    • 1970-01-01
    • 2017-10-09
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 2021-10-08
    • 2016-11-26
    相关资源
    最近更新 更多