Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python请求和cx_冻结_Python_Python Requests_Cx Freeze - Fatal编程技术网

python请求和cx_冻结

python请求和cx_冻结,python,python-requests,cx-freeze,Python,Python Requests,Cx Freeze,我正在尝试冻结一个依赖于请求的python应用程序,但出现以下错误: Traceback (most recent call last): File "c:\Python33\lib\site-packages\requests\packages\urllib3\util.py", line 630, in ssl_wrap_socket context.load_verify_locations(ca_certs) FileNotFoundError: [Errno 2] No s

我正在尝试冻结一个依赖于请求的python应用程序,但出现以下错误:

Traceback (most recent call last):
  File "c:\Python33\lib\site-packages\requests\packages\urllib3\util.py", line 630, in ssl_wrap_socket
    context.load_verify_locations(ca_certs)
FileNotFoundError: [Errno 2] No such file or directory
看起来它在查找带有可执行文件的ssl证书时遇到问题。我发现这似乎是同样的问题,但我无法弄清楚他们是如何让它工作的。主要问题似乎是请求绑定的证书没有复制到压缩库中。因此,我似乎必须强制cx_freeze绑定证书,然后从脚本中指向它

从这个简单的脚本开始,一切正常:

import requests
r = requests.get("https://yourapihere.com")
print(r.json())
然后,如果我添加证书文件,我会发现错误:

import requests
r = requests.get("https://yourapihere.com", cert=requests.certs.where())
print(r.json())
-

setup.py:

from cx_Freeze import setup, Executable

import requests.certs
build_exe_options = {"zip_includes":[(requests.certs.where(),'requests/cacert.pem')]}

executables = [
    Executable('example.py')
]

setup(
      executables=executables
      )
from cx_Freeze import setup, Executable
import requests
import sys

executable = Executable( script = "test.py" )

# Add certificate to the build
options = {
    "build_exe": {
        'include_files' : [(requests.certs.where(), 'cacert.pem')]
    }
}

setup(
    version = "0",
    requires = ["requests"],
    options = options,
    executables = [executable]
)

如果有人能给我一点建议,我将不胜感激。

我从另一条对我有用的帖子中找到了这条评论:

总结如下: 您还可以使用环境变量“REQUESTS\u CA\u BUNDLE”(如上所述)

这比更正所有请求要简单得多:


使其工作的步骤:

  • 显式告诉请求证书的位置
  • 告诉cx_freeze在“构建”时也获取证书文件
  • 有代码在冻结时使用正确的证书文件
test.py:

import os
import sys

import requests

# sets the path to the certificate file
if getattr(sys, 'frozen', False):
    # if frozen, get embeded file
    cacert = os.path.join(os.path.dirname(sys.executable), 'cacert.pem')
else:
    # else just get the default file
    cacert = requests.certs.where()

# remember to use the verify to set the certificate to be used
# I guess it could also work with REQUESTS_CA_BUNDLE, but I have not tried
r = requests.get('https://www.google.com', verify=cacert)

print(r)
setup.py:

from cx_Freeze import setup, Executable

import requests.certs
build_exe_options = {"zip_includes":[(requests.certs.where(),'requests/cacert.pem')]}

executables = [
    Executable('example.py')
]

setup(
      executables=executables
      )
from cx_Freeze import setup, Executable
import requests
import sys

executable = Executable( script = "test.py" )

# Add certificate to the build
options = {
    "build_exe": {
        'include_files' : [(requests.certs.where(), 'cacert.pem')]
    }
}

setup(
    version = "0",
    requires = ["requests"],
    options = options,
    executables = [executable]
)
要构建它,只需:

$ python setup.py build
如果成功,您应该看到:

$ test
<Response [200]>
$test

在冻结的代码
请求中。文件\uuuu
不会指向真实文件,因为模块位于zip文件中。请参阅常见问题解答。我仔细检查了该文件和请求。_文件指向zip文件。所以我想这很好。不,因为它不能只打开
../library.zip/requests/cacert.pem
。该路径需要指向一个可以打开和读取的文件。第一个错误不是使用冻结,而是正常运行。我觉得应该传递路径,因为
verify
参数-
cert
似乎有所不同。基于
os.getcwd()执行此操作
仅当您从包含该文件的目录运行程序时才起作用。如果您创建一个快捷方式并从其他地方运行它,它将找不到该文件。常见问题解答介绍了如何查找与exe相关的数据文件。
$ test
<Response [200]>