python - 将 .py 编译为 .exe 后验证 ssl 证书失败

标签 python ssl ssl-certificate python-requests

我有一个读取 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.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') 之前从中创建一个临时文件>.

关于python - 将 .py 编译为 .exe 后验证 ssl 证书失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39928062/

相关文章:

python - 如何使用 matplotlib 的动画编写器加速 MP4 的生成?

magento ssl + Varnish

php - 如何加密非阻塞 PHP 套接字流?

IIS 7.5 SSL 错误 “ssl_error_rx_record_too_long” 与自签名证书

node.js - 无法验证 Node.js 中的第一个证书

ssl - 为什么 SSL 证书是用私钥签名的?

python - 神经网络似乎在每次执行时都停留在单个输出上

python - 在 python unittest 中关闭一些打印

python - 在列表推导中创建多个生成器

.htaccess - https ://www redirect on single domain certificate