Python 修复使用py2exe(或PyInstaller)编译的exe中的SSL证书错误
我刚刚完成了一个Python程序的测试,该程序需要登录到一个站点,并且需要设置一个CSRF cookie。我尝试使用Python 修复使用py2exe(或PyInstaller)编译的exe中的SSL证书错误,python,ssl,py2exe,python-requests,pyinstaller,Python,Ssl,Py2exe,Python Requests,Pyinstaller,我刚刚完成了一个Python程序的测试,该程序需要登录到一个站点,并且需要设置一个CSRF cookie。我尝试使用py2exe将其打包为exe,但出现了套接字错误。当我尝试使用PyInstaller时,我也遇到了同样的问题。通过谷歌搜索Errno,我发现其他几个人也有同样的问题,因此我知道问题与SLL证书的位置有关 这是我的site\u agent类,包括日志调用 class site_agent: self.get_params() URL = roo
py2exe
将其打包为exe,但出现了套接字错误。当我尝试使用PyInstaller
时,我也遇到了同样的问题。通过谷歌搜索Errno,我发现其他几个人也有同样的问题,因此我知道问题与SLL证书的位置有关
这是我的site\u agent
类,包括日志调用
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 (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
回溯(最近一次呼叫最后一次):
文件“”,第223行,在
文件“”,第198行,主
文件“”,第49行,在_init中__
文件“C:\pyinstaller-2.0\pyinstaller-2.0\autoresponder\b
“uild\pyi.win32\autoresponder\out00 PYZ.PYZ\requests.sessions”,get中第350行
文件“C:\pyinstaller-2.0\pyinstaller-2.0\autoresponder\b
“uild\pyi.win32\autoresponder\out00 PYZ.PYZ\requests.sessions”,requ中第338行
美国东部时间
文件“C:\pyinstaller-2.0\pyinstaller-2.0\autoresponder\b
uild\pyi.win32\autoresponder\out00 PYZ.PYZ\requests.sessions”,发送中第441行
文件“C:\pyinstaller-2.0\pyinstaller-2.0\autoresponder\b
uild\pyi.win32\autoresponder\out00 PYZ.PYZ\requests.adapters”,发送中第331行
requests.exceptions.SSLError:[Errno 185090050]\u ssl.c:336:错误:0B084002:x509
证书例程:X509\u加载\u证书\u crl\u文件:系统库
这是否意味着问题出在请求.适配器中
如果是这样,我是否可以在安装的Python软件包中编辑它以在其他地方查找cacert.pem,用py2exe
或PyInstaller
重新生成我的exe,然后在安装的Python版本中更改回它
编辑
在使用PyInstaller
编译并在所有requests.get()
和requests.post()调用中设置verify=False
后,我现在让程序运行。但是SSL存在是有原因的,我真的希望在允许任何人使用该工具之前能够修复此错误。如果使用pyinstaller。。。在PyInstaller\hooks\
中为包含
from hookutils import collect_data_files
# Get the cacert.pem
datas = collect_data_files('requests')
除了frmdstryr
给出的答案外,我还必须告诉请求cacert.pem
在哪里。我在导入后立即添加了以下行:
# Get the base directory
if getattr( sys , 'frozen' , None ): # keyword 'frozen' is for setting basedir while in onefile mode in pyinstaller
basedir = sys._MEIPASS
else:
basedir = os.path.dirname( __file__ )
basedir = os.path.normpath( basedir )
# Locate the SSL certificate for requests
os.environ['REQUESTS_CA_BUNDLE'] = os.path.join(basedir , 'requests', 'cacert.pem')
此解决方案源自此链接:如果您使用“请求”模块,则可以轻松解决此问题
1) 将下面的代码放在使用“requests”模块的主python文件中
os.environ['REQUESTS_CA_BUNDLE'] = "certifi/cacert.pem"
2) 在存在exe的可分发文件夹中,创建一个名为“certifi”的文件夹,并将“cacert.pem”文件放在其中
3) 您可以通过以下方式找到“cacert.pem”文件:
太棒了。。现在,您的distributable包括验证ssl调用所需的证书。pyinstaller现在默认包括此钩子
pip install certifi
import certifi
certifi.where()