我有一个读取 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 将不再找到受信任的 CA(验证服务器证书所需),因为它们不包含在 exe 中。在这种情况下,使用 requests.get
和 verify=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')
之前从中创建一个临时文件>.
关于python - 将 .py 编译为 .exe 后验证 ssl 证书失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39928062/