修复使用 py2exe(或 PyInstaller)编译的 exe 中的 SSL 证书错误 [英] Fixing SSL certificate error in exe compiled with py2exe (or PyInstaller)

查看:70
本文介绍了修复使用 py2exe(或 PyInstaller)编译的 exe 中的 SSL 证书错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚完成了一个 Python 程序的测试,该程序涉及登录站点并需要设置 CSRF cookie.我尝试使用 py2exe 将其打包为 exe,但出现套接字错误.当我尝试使用 PyInstaller 时,我遇到了同样的问题.谷歌搜索 Errno 我发现其他一些人也有同样的问题,所以我知道问题与 SLL 证书的位置有关.

I've just finished testing a Python programme which involves logging into a site and requires a CSRF cookie to be set. I've tried packaging it as an exe using py2exe and got a socket error. I have the same problem when I try with PyInstaller. Googling the Errno I found a few other people with the same problem and so I know the problem is to do with the location of SLL certificates.

这是我的 site_agent 类,包括日志记录调用.

This is my site_agent class including the logging calls.

    class site_agent:
        self.get_params()
        URL = root_url + '/accounts/login/'        
        # Retrieve the CSRF token first
        self.agent = requests.session()
        self.agent.get(URL)  # retrieves the cookie # This line throws the error
        self.csrftoken = self.agent.cookies['csrftoken']    
        # Set up login data including the CSRF cookie
        login_data = {'username': self.username,
                      'password': self.password,
                      'csrfmiddlewaretoken' : self.csrftoken}
        # Log in
        logging.info('Logging in')
        response = self.agent.post(URL, data=login_data, headers=hdr)

错误出现在 self.agent.get(URL) 行,Traceback 显示:

The error comes at the self.agent.get(URL) line and the Traceback shows:

Traceback (most recent call last):
  File "<string>", line 223, in <module>
  File "<string>", line 198, in main
  File "<string>", line 49, in __init__
  File "C:\pyinstaller-2.0\pyinstaller-2.0\autoresponder\b
uild\pyi.win32\autoresponder\out00-PYZ.pyz\requests.sessions", line 350, in get
  File "C:\pyinstaller-2.0\pyinstaller-2.0\autoresponder\b
uild\pyi.win32\autoresponder\out00-PYZ.pyz\requests.sessions", line 338, in requ
est
  File "C:\pyinstaller-2.0\pyinstaller-2.0\autoresponder\b
uild\pyi.win32\autoresponder\out00-PYZ.pyz\requests.sessions", line 441, in send

  File "C:\pyinstaller-2.0\pyinstaller-2.0\autoresponder\b
uild\pyi.win32\autoresponder\out00-PYZ.pyz\requests.adapters", line 331, in send

requests.exceptions.SSLError: [Errno 185090050] _ssl.c:336: error:0B084002:x509
certificate routines:X509_load_cert_crl_file:system lib

这是否意味着问题出在 requests.adapters 中?

Does this mean that the problem is in requests.adapters?

如果是这样,我可以在我安装的 Python 包中编辑它以在其他地方查找 cacert.pem,使用 py2exePyInstaller 重建我的 exe,然后更改它回到我安装的 Python 版本?

If so, can I just edit it in my installed Python packages to look for cacert.pem somewhere else, rebuild my exe with py2exe or PyInstaller, then change it back in my installed version of Python?

编辑

我现在在使用 PyInstaller 编译并在所有 requests.get() 中设置 verify=False 后运行程序requests.post() 调用.但是 SSL 存在是有原因的,我真的很希望能够在让任何人使用该工具之前修复此错误.

I now have the programme running after compiling with PyInstaller and setting verify=False in all requests.get() and requests.post() calls. But SSL is there for a a reason and I'd really like to be able to fix this error before letting anyone use the tool.

推荐答案

如果您使用请求"模块,这个问题很容易解决.

This can be solved easily if you are using "requests" module.

1) 将以下代码放在使用请求"模块的主 python 文件中

1) Place the below code in your main python file where "requests" module is used

os.environ['REQUESTS_CA_BUNDLE'] = "certifi/cacert.pem"

2) 在 exe 所在的可分发文件夹中,创建一个名为certifi"的文件夹并将cacert.pem"文件放入其中.

2) Within your distributable folder where exe is present, create a folder called "certifi" and place the "cacert.pem" file within it.

3) 您可以通过

pip install certifi

import certifi
certifi.where()

太棒了.. 现在您的分发包括验证 ssl 调用所需的证书.

Awesome.. now your distributable includes the necessary certificates to validate ssl calls.

这篇关于修复使用 py2exe(或 PyInstaller)编译的 exe 中的 SSL 证书错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆