请求库:cx_freeze后丢失的文件

时间:2021-10-07 18:04:07

I'm building an application in python 3.3 which uses the requests library. When I try to get a URL with SSL connection I want to verify it with verify = true. This works perfectly when running my python scripts.

我在python 3.3中构建一个应用程序,它使用请求库。当我尝试获取一个带有SSL连接的URL时,我想用verify = true验证它。这在运行python脚本时非常有效。

When I freeze the same scripts it crashes. It misses something and I really cant figure out how to integrate it in my frozen application.

当我冻结相同的脚本时,它会崩溃。它漏掉了一些东西,我实在想不出如何在我的冷冻应用程序中集成它。

I get the following error (which also triggers other errors, but I don't post them here):

我得到以下错误(也会引发其他错误,但我不在这里发布):

Traceback (most recent call last):
File "C:\Python33-32\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 422, in urlopen
body=body, headers=headers)
File "C:\Python33-32\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 274, in _make_request
conn.request(method, url, **httplib_request_kw)
File "C:\Python33-32\lib\http\client.py", line 1049, in request
self._send_request(method, url, body, headers)
File "C:\Python33-32\lib\http\client.py", line 1087, in _send_request
self.endheaders(body)
File "C:\Python33-32\lib\http\client.py", line 1045, in endheaders
self._send_output(message_body)
File "C:\Python33-32\lib\http\client.py", line 890, in _send_output
self.send(msg)
File "C:\Python33-32\lib\http\client.py", line 828, in send
self.connect()
File "C:\Python33-32\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 105, in connect
ssl_version=self.ssl_version)
File "C:\Python33-32\lib\site-packages\requests\packages\urllib3\util.py", line 281, in ssl_wrap_socket
context.load_verify_locations(ca_certs)
FileNotFoundError: [Errno 2] No such file or directory 

It seems that ca_certs is missing. There is a file called cacert.pem in the requests library, but I don't know if this is the missing file and how to import it since it seems to be not integrated into my final frozen package.

看来ca_certs丢失了。有一个文件叫做cacert。请求库中的pem,但是我不知道这是否是丢失的文件,以及如何导入它,因为它似乎没有集成到我最终冻结的包中。

3 个解决方案

#1


9  

Looking at the requests source, it seems you can pass the path to the cacert.pem file as verify=path, instead of verify=True. So you don't need to modify requests for it to work.

查看请求源,似乎可以将路径传递给cacert。pem文件作为verify=path,而不是verify=True。因此,您不需要修改请求来工作。

You can pass the path of a file to include in the include-files parameter of the cx_Freeze options (docs). You can find the path from requests, so something like this should work in the setup.py you use to freeze it:

您可以将文件的路径传递到cx_Freeze选项(文档)的includefile参数中。您可以从请求中找到路径,所以类似这样的东西应该可以在设置中使用。你用来冷冻它的py:

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

#...

#2


4  

As Thomas K said, you need to include a CA certificates file if you enable verification.

正如Thomas K所说,如果启用验证,则需要包含CA证书文件。

However, I found that at least for me, requests will look for [INSTALL PATH]\library.zip\cacert.pem which will fail.

然而,我发现至少对我来说,请求将查找[安装路径]\library.zip\cacert。pem将失败。

I solved it by copying the cacert.pem as described

我复制了cacert。pem所描述的

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

#...

and specified its path directly when performing a request:

并在执行请求时直接指定路径:

requests.get(..., verify = '[INSTALL PATH]\cacert.pem')

#3


3  

You can also use enviroment variable "REQUESTS_CA_BUNDLE" (as said http://docs.python-requests.org/en/latest/user/advanced/#ssl-cert-verification)

您还可以使用环境变量“REQUESTS_CA_BUNDLE”(参见http://docs.pythonrequests.org/en/latest/user/advanced/# ssl-cert-验证)

It's much simpler, than correct all your requests:

这比纠正你所有的要求要简单得多:

os.environ["REQUESTS_CA_BUNDLE"] = os.path.join(os.getcwd(), "cacert.pem")

#1


9  

Looking at the requests source, it seems you can pass the path to the cacert.pem file as verify=path, instead of verify=True. So you don't need to modify requests for it to work.

查看请求源,似乎可以将路径传递给cacert。pem文件作为verify=path,而不是verify=True。因此,您不需要修改请求来工作。

You can pass the path of a file to include in the include-files parameter of the cx_Freeze options (docs). You can find the path from requests, so something like this should work in the setup.py you use to freeze it:

您可以将文件的路径传递到cx_Freeze选项(文档)的includefile参数中。您可以从请求中找到路径,所以类似这样的东西应该可以在设置中使用。你用来冷冻它的py:

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

#...

#2


4  

As Thomas K said, you need to include a CA certificates file if you enable verification.

正如Thomas K所说,如果启用验证,则需要包含CA证书文件。

However, I found that at least for me, requests will look for [INSTALL PATH]\library.zip\cacert.pem which will fail.

然而,我发现至少对我来说,请求将查找[安装路径]\library.zip\cacert。pem将失败。

I solved it by copying the cacert.pem as described

我复制了cacert。pem所描述的

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

#...

and specified its path directly when performing a request:

并在执行请求时直接指定路径:

requests.get(..., verify = '[INSTALL PATH]\cacert.pem')

#3


3  

You can also use enviroment variable "REQUESTS_CA_BUNDLE" (as said http://docs.python-requests.org/en/latest/user/advanced/#ssl-cert-verification)

您还可以使用环境变量“REQUESTS_CA_BUNDLE”(参见http://docs.pythonrequests.org/en/latest/user/advanced/# ssl-cert-验证)

It's much simpler, than correct all your requests:

这比纠正你所有的要求要简单得多:

os.environ["REQUESTS_CA_BUNDLE"] = os.path.join(os.getcwd(), "cacert.pem")