我刚刚完成了一个Python程序的测试,它涉及登录到一个站点,并要求设置一个CSRF cookie。我尝试过使用py2exe将其打包为exe,并得到了一个套接字错误。当我尝试使用PyInstaller时,我也遇到了同样的问题。在搜索Errno时,我发现了其他几个有同样问题的人,所以我知道问题与SLL证书的位置有关。
这是我的site_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这是否意味着问题在requests.adapters中存在?
如果是这样的话,我是否可以在我安装的Python包中编辑它以查找其他地方的cacert.pem,用py2exe或PyInstaller重新构建我的exe,然后在我安装的Python版本中更改它呢?
编辑
现在,在用PyInstaller编译并在所有requests.get()和requests.post()调用中设置verify=False之后,程序就可以运行了。但是SSL的存在是有原因的,我非常希望能够在允许任何人使用该工具之前修复这个错误。
发布于 2015-12-11 15:38:12
如果您使用的是“请求”模块,这是很容易解决的。
1)将以下代码放在使用“请求”模块的主要python文件中
os.environ['REQUESTS_CA_BUNDLE'] = "certifi/cacert.pem"2)在存在exe的可分发文件夹中,创建一个名为"cacert.pem“的文件夹,并将”“文件放入其中。
3)您可以通过以下方式找到"cacert.pem“文件
pip install certifi
import certifi
certifi.where()太棒了。现在,您的发行版包含了验证ssl调用所需的证书。
发布于 2014-09-24 14:12:34
如果使用pyinstaller..。在hook-requests.py中为包含以下内容的请求库创建PyInstaller\hooks\文件
from hookutils import collect_data_files
# Get the cacert.pem
datas = collect_data_files('requests') 发布于 2015-01-31 02:09:39
除了frmdstryr给出的答案之外,我还必须告诉requests 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')此解决方案来自于以下链接:http://kittyandbear.net/python/pyinstaller-request-sslerror-manual-cacert-solution.txt
https://stackoverflow.com/questions/17158529
复制相似问题