简体   繁体   English

python请求和cx_freeze

[英]python requests and cx_freeze

I am trying to freeze a python app that depends on requests, but I am getting the following error: 我试图冻结一个取决于请求的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

Looks like it is having trouble finding the ssl certificate with the executable. 看起来很难找到带有可执行文件的ssl证书。 I found this which seems to be the same problem, but I am not able to figure out how they got it to work. 我发现似乎是同样的问题,但我无法弄清楚他们是如何让它工作的。 The main problem seems to be that the certificate bundled by requests is not copied over to the compressed library. 主要问题似乎是请求捆绑的证书不会复制到压缩库。 So it seems that I will have to force cx_freeze to bundle the certificates and then point to it from my script. 因此,我似乎必须强制cx_freeze捆绑证书,然后从我的脚本指向它。

Starting with this simple script everything works fine: 从这个简单的脚本开始一切正常:

import requests
r = requests.get("https://yourapihere.com")
print(r.json())

Then if I add the certificate file I stat getting errors: 然后,如果我添加证书文件我stat获取错误:

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

- -

Traceback (most recent call last):
  File "c:\Python33\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 480, in urlopen
    body=body, headers=headers)
  File "c:\Python33\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 285, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "c:\Python33\lib\http\client.py", line 1065, in request
    self._send_request(method, url, body, headers)
  File "c:\Python33\lib\http\client.py", line 1103, in _send_request
    self.endheaders(body)
  File "c:\Python33\lib\http\client.py", line 1061, in endheaders
    self._send_output(message_body)
  File "c:\Python33\lib\http\client.py", line 906, in _send_output
    self.send(msg)
  File "c:\Python33\lib\http\client.py", line 844, in send
    self.connect()
  File "c:\Python33\lib\site-packages\requests\packages\urllib3\connection.py", line 164, in connect
    ssl_version=resolved_ssl_version)
  File "c:\Python33\lib\site-packages\requests\packages\urllib3\util.py", line 637, in ssl_wrap_socket
    context.load_cert_chain(certfile, keyfile)
ssl.SSLError: [SSL] PEM lib (_ssl.c:2155)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Python33\lib\site-packages\requests\adapters.py", line 330, in send
    timeout=timeout
  File "c:\Python33\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 504, in urlopen
    raise SSLError(e)
requests.packages.urllib3.exceptions.SSLError: [SSL] PEM lib (_ssl.c:2155)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "example.py", line 10, in <module>
    r = requests.get("https://yourapihere.com", cert=requests.certs.where())
  File "c:\Python33\lib\site-packages\requests\api.py", line 55, in get
    return request('get', url, **kwargs)
  File "c:\Python33\lib\site-packages\requests\api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "c:\Python33\lib\site-packages\requests\sessions.py", line 383, in request
    resp = self.send(prep, **send_kwargs)
  File "c:\Python33\lib\site-packages\requests\sessions.py", line 486, in send
    r = adapter.send(request, **kwargs)
  File "c:\Python33\lib\site-packages\requests\adapters.py", line 385, in send
    raise SSLError(e)
requests.exceptions.SSLError: [SSL] PEM lib (_ssl.c:2155)

I guess I am using it correctly , but cant really figure out why it is not working. 我想我正确使用它,但不能确切地知道它为什么不起作用。 I guess that after fixing this I can continue and add the certificate to the cx_freeze bundle, something like: 我想在修复之后我可以继续并将证书添加到cx_freeze包中,例如:

example.py: example.py:

import os
import requests

cert = os.path.join(os.path.dirname(requests.__file__),'cacert.pem')
r = requests.get("https://yourapihere.com", cert=cert)
print(r.json())

setup.py: 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
      )

if someone could give me a tip it would be much appreciated. 如果有人能给我一个提示,我将不胜感激。

I found this comment from another thread that worked for me: https://stackoverflow.com/a/25239701/3935084 我从另一个对我有用的线程中发现了这条评论: https//stackoverflow.com/a/25239701/3935084

Summary below: 摘要如下:

You can also use environment variable "REQUESTS_CA_BUNDLE" (as said http://docs.python-requests.org/en/latest/user/advanced/#ssl-cert-verification ) 您还可以使用环境变量“REQUESTS_CA_BUNDLE”(如http://docs.python-requests.org/en/latest/user/advanced/#ssl-cert-verification所述)

It's much simpler than correcting all your requests: 它比纠正您的所有请求简单得多:

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

Steps to get this to work: 让它工作的步骤:

  • Explicitly tell requests where the certificate are 明确告知证书所在的请求
  • Tell cx_freeze to also get the certificate file when "building" 告诉cx_freeze在“构建”时也获取证书文件
  • have the code to use the proper certificate file when frozen 有代码在冻结时使用适当的证书文件

test.py: 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: setup.py:

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]
)

to build it, just: 建立它,只是:

$ python setup.py build

If successful you should see: 如果成功,您应该看到:

$ test
<Response [200]>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM