对于一些冷冻的应用程序,你必须设置(一般或外部数据)的CACERT里面的路径冷冻应用。
Setup.py科
首先,您需要把它列入你的编译选项,并手动指定安装目录。这就是setup.py里面去的唯一部分:
# notice how I say the folder the certificate is installed
{"include_files":[(requests.certs.where(),'cacert.pem')]}
在你的情况,这将产生以下安装文件:
import requests
import sys
# more imports
setup(name = 'WallDit_QT',
version = '1.0',
author = 'Disco Dolan',
description ='Set your wallpaper interactively!',
executables = [exe],
options = {
'build.exe': {
"include_files": [
(requests.certs.where(),'cacert.pem'),
'praw.ini',
'README.md'
]
}
},
requires = ['PySide', 'cx_Freeze', 'praw', 'shutil', 'requests']
)
应用部分
然后,您需要在冻结应用程序的运行时获取证书路径。 对于PyInstaller,在运行时将路径定义为名为_MEIPASS(可从sys._MEIPASS获取)的数据目录,允许您访问应用程序所需的所有数据。
cacertpath = os.path.join(sys._MEIPASS, "cacert.pem")
对于cx_Freeze,路径可以从安装的路径来确定,并且具有期望的数据加入其:在cacert.pem的情况下,如下所述路径将被确定。在这里,我们得到的路径,如下所示:
cacertpath = os.path.join(datadir, 'cacert.pem')
您可以轻松地获得数据目录冷冻应用具有以下:
datadir = os.path.dirname(sys.executable)
(请注意,这不会与非工作冷冻应用,因此要保证它同时适用于冷冻和非冷冻的应用程序,你Cx_Freeze recommends如下代码时):
def find_data_file(filename):
if getattr(sys, 'frozen', False):
# The application is frozen
datadir = os.path.dirname(sys.executable)
else:
# The application is not frozen
# Change this bit to match where you store your data files:
datadir = os.path.dirname(__file__)
return os.path.join(datadir, filename)
然后,包括这条道路所有请求国防部ULE GET和POST请求如下:
request.get(url, headers=headers, verify=cacertpath)
实施例1个
的示例代码片段将是如下:
# load modules
import os
import sys
import requests
# define our path finder
def find_data_file(filename):
if getattr(sys, 'frozen', False):
# The application is frozen
datadir = os.path.dirname(sys.executable)
else:
# The application is not frozen
# Change this bit to match where you store your data files:
datadir = os.path.dirname(__file__)
return os.path.join(datadir, filename)
# get our cacert path and post our GET request
cacertpath = find_data_file('cacert.pem')
r = requests.get('https://api.github.com/events', verify=cacertpath)
# print the text from the request
print(r.text)
实施例2
您也可以通过执行以下操作来告诉请求在哪里查找证书:
os.environ["REQUESTS_CA_BUNDLE"] = cacertpath
在这种情况下,我们将执行以下操作。这样做的好处是cacertpath不会在每个模块中明确定义(或从另一个模块导入),并且可以在环境中定义。
import os
import sys
import requests
def find_data_file(filename):
if getattr(sys, 'frozen', False):
# The application is frozen
datadir = os.path.dirname(sys.executable)
else:
# The application is not frozen
# Change this bit to match where you store your data files:
datadir = os.path.dirname(__file__)
return os.path.join(datadir, filename)
cacertpath = find_data_file('cacert.pem')
os.environ["REQUESTS_CA_BUNDLE"] = cacertpath
r = requests.get('https://api.github.com/events')
r.text
我对你的代码块有点困惑,除了最后一个应该在我的setup.py中去的所有这些吗?你也是什么意思的数据文件? –
我会编辑一下。 –
这是否澄清了一点? –