简体   繁体   中英

python requests error and cx_freeze

I am trying to cx_freeze a python app that depends on requests, but I am getting the following error:

  File "C:\Python33\lib\site-packages\requests-2.5.3-py3.3.egg\requests\api.py",
 line 65, in get
    return request('get', url, **kwargs)
  File "C:\Python33\lib\site-packages\requests-2.5.3-py3.3.egg\requests\api.py",
 line 49, in request
    response = session.request(method=method, url=url, **kwargs)
  File "C:\Python33\lib\site-packages\requests-2.5.3-py3.3.egg\requests\sessions
.py", line 461, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Python33\lib\site-packages\requests-2.5.3-py3.3.egg\requests\sessions
.py", line 573, in send
    r = adapter.send(request, **kwargs)
  File "C:\Python33\lib\site-packages\requests-2.5.3-py3.3.egg\requests\adapters
.py", line 431, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: [Errno 2] No such file or directory

this simple script everything works fine:

import requests
url = 'https://test.com'
try:
    res = requests.get(url, headers={'User-Agent': self.user_agent},timeout=10)
    redirecturl = res.url
    code = res.status_code
except requests.exceptions.ConnectTimeout as err:
    print ("error -%s" %err)

cx_freeze setup.py

from cx_Freeze import setup, Executable
import sys

build_exe_options = {"package":["requests"],"excludes" : "[Tkinter]"}
base = None
if sys.platform == "win32":
    none = "Win32GUI"

setup(
    name ="PageFound",
    version ="1.0",
    description ="PageFound Tool",
    options = {"buid_exe": build_exe_options},
    executables=[Executable("PageFound.py")])

if someone could give me a tip it would be much appreciated.

The file needed to authenticate the ssl certificate is not being found, because when you freeze it, that particular file is not frozen with the build. Then requests goes looking for it in its default location and does not find it there. This is because when you run the script without being frozen, it works fine, but when the directory is changed, it does not. Someone else put it better than I:

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.

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:

import requests.certs

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

...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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