在冻结的应用程序中使用请求时出错

Posted

技术标签:

【中文标题】在冻结的应用程序中使用请求时出错【英文标题】:Error using Requests in a frozen app 【发布时间】:2014-02-24 23:08:23 【问题描述】:

我正在尝试在冻结的应用程序中使用出色的请求库。该代码在解释时工作正常,但当我生成 dist 可执行文件时它停止工作。

我尝试了这个解决方案,但它不起作用 (Requests library: missing file after cx_freeze)

我的 setup.py 文件:

import esky.bdist_esky
from esky.bdist_esky import Executable as Executable_Esky
from cx_Freeze import setup, Executable
from myapp import VERSION
import requests.certs

packages = [
    'PIL',
    '_winreg',
    'esky',
    ]

includes = [
    'PySide',
    'sys',
    'os',
    'datetime',
    'threading',
    'Queue',
    'uuid',
    'requests',
]

excludes = [
    'TKinter',
    'tcl',
    'ttk',
]

include_files =["icon-16px.ico",
                "icon-32px.ico",
                "logo-t-160x56.png",
                ]

setup(
    scripts = [
        Executable_Esky(
            "myapp.py",
            gui_only = False,
            icon = "icon-16px.ico",
        ),
    ],
    data_files = include_files,
    options="build_exe":
                 "packages":packages,
                  "includes": includes,
                  "include_files": include_files + [(requests.certs.where(),'cacert.pem')],
                  "excludes": excludes,
                  "optimize": 2,
                  "icon":"icon-16px.ico",
                 ,
             "bdist_esky":
                 'freezer_module':"cxfreeze",
                 'includes': includes,
                 'excludes': excludes,
             ,
    ,
    executables = [Executable(script="myapp.py",base="Win32GUI")],
)

追溯:

Traceback (most recent call last):
  File "C:\Users\Fernando\Dropbox\the all-seeing boss\myapp_client\testes\cx
_freeze\qt_gui\interface_qt.py", line 45, in login
    r = requests.post(url, data=data)
  File "C:\Python27\lib\site-packages\requests\api.py", line 88, in post
    return request('post', url, data=data, **kwargs)
  File "C:\Python27\lib\site-packages\requests\api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 383, in reques
t
    resp = self.send(prep, **send_kwargs)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 486, in send
    r = adapter.send(request, **kwargs)
  File "C:\Python27\lib\site-packages\requests\adapters.py", line 385, in send
    raise SSLError(e)
requests.exceptions.SSLError: [Errno 185090050] _ssl.c:343: error:0B084002:x509
certificate routines:X509_load_cert_crl_file:system lib

【问题讨论】:

github.com/kennethreitz/requests/issues/… 看起来适用。 是的,该线程有(可能的)答案。 requests 正在查找 SSL 证书文件,但在冻结时,您需要包含该文件的副本并告诉 requests 它在哪里。 我尝试导入证书文件,但没有成功,我会继续努力,谢谢大家的提示。 【参考方案1】:

或者你可以使用

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

【讨论】:

【参考方案2】:

我在 github(https://github.com/kennethreitz/requests/issues/557#issuecomment-6420819) 上的这个问题之后得到了这个。感谢 Martjin Pieters 的提示。

首先,我将 cacert.pem 文件放在我的项目文件夹中,然后将其包含在冻结的应用程序中:

include_files =["icon-16px.ico",
                "icon-32px.ico",
                "logo-t-160x56.png",
                "cacert.pem",
                ]

setup(
    scripts = [
        Executable_Esky(
            "myapp.py",
            gui_only = False,
            icon = "icon-16px.ico",
        ),
    ],
    data_files = include_files,
    options="build_exe":
                 "packages":packages,
                  "includes": includes,
                  "include_files": include_files,
                  "excludes": excludes,
                  "optimize": 2,
                  "icon":"icon-16px.ico",
                 ,
             "bdist_esky":
                 'freezer_module':"cxfreeze",
                 'includes': includes,
                 'excludes': excludes,
             ,
    ,
    executables = [Executable(script="myapp.py",base="Win32GUI")],
)

最后,您必须在每个 POST 或 GET 中使用 requests 指明此证书,如下所示:

r = requests.post(url, data=data, verify = os.path.join(appdata,'cacert.pem'))

XD

【讨论】:

以上是关于在冻结的应用程序中使用请求时出错的主要内容,如果未能解决你的问题,请参考以下文章

使用 axios 向服务器发出请求会冻结 React Native 应用程序

请求访问地址簿时应用程序冻结

Firebase Firestore get() 快照在第二次查询时冻结

phonegap ios:当用户为权限请求框选择“不允许”时,推送通知冻结

使用 cx_freeze 4.3.1 冻结 h5py 2.4 时出错

Python中的冻结Qt GUI