如何检查 Android 上的 Kivy UrlRequest Get 是否为 运行?

How do I check whether Kivy UrlRequest Get is running on Android?

我正在尝试使用 Kivy 的 UrlRequest 抓取网站。当我 运行 来自 Android 的应用程序时,代码进入 UrlRequest 函数并且从不 return 发送失败、发生错误或成功的任何信息。 我如何 "listen" 进行日志输出以找出为什么没有任何反应? 或者,我的 Url 请求代码有什么问题?

我试过以下方法

1]定义了on_success、on_progress、on_error和on_failure函数,看是否有输出。

2] 创建了一个具有不同 chunk_sizedebugdecodetimeout 值的设置面板,以查看当我更改它们时是否有所不同。

3]导入Kivy的LoggerHistory函数,查看是否有错误日志。

4] 查看终端中的 buildozer android logcat 输出,看看当我按下请求按钮时会发生什么。

5] 将 url 从 https://www.google.com 更改为 http://www.google.com

6] 将编码从 'Latin-1' 更改为 'utf-16' 并再次返回。

7] 使用 Urllib.request 库添加了一个函数来查看我是否可以得到任何东西,它给了我以下错误:

07-30 21:51:09.933  9393  9417 I python  :  urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1051)>

Url请求代码

    def req_url(self, *args):
        url_chnk = self.the.config.getint('urlreqsettings', 'optionschunk')
        tme = self.the.config.getint('urlreqsettings', 'inttime')
        dcode = self.the.config.getboolean('urlreqsettings', 'booldecode')
        dbug = self.the.config.getboolean('urlreqsettings', 'booldebug')
        self.ids.result_success.color = (0,0,1,1)
        self.ids.result_success.text = "Busy..."
        # self.ids.conn_status.text = str(url_chnk)
        the_request = UrlRequest(self.the._url,on_progress=self.on_progress, on_success=self.on_success, on_error=self.on_error, on_failure=self.on_failure, chunk_size=url_chnk, timeout=tme, debug=dbug, decode=dcode)
        self.ids.req_info.text = str(the_request.is_finished) + " - " + str(the_request.chunk_size)
    def on_success(self, the_request, result, *args):
        self.ids.req_info.text = str(the_request.is_finished) + " - " + str(the_request.chunk_size)
        # print(the_request.resp_headers)
        if self.prog_circle:
            self.loader.dismiss()
            self.ids.result_success.color = (0,0,1,1)
            self.ids.result_success.text = "Successful UrlRequest!"
            self.ids.get_result.text = ""
            self.ids.get_result.text = str(result, encoding='Latin-1')
            self.prog_circle = False

我用的Url

config.setdefaults('myurl', {
            'url': 'https://www.google.com'})

当 on_release 按钮调用 Kivy 的 UrlRequest 函数时输出 Logcat

07-30 21:44:42.795  2122  2122 D NetworkController.WifiSignalController: Change in state from: connected=true,enabled=true,level=4,inetCondition=1,iconGroup=IconGroup(Wi-Fi Icons),activityIn=true,activityOut=true,rssi=-51,lastModified=07-30 21:43:46,ssid="TP-Link_4837_5G",isTransient=false,statusLabel=null
07-30 21:44:42.795  2122  2122 D NetworkController.WifiSignalController:    to: connected=true,enabled=true,level=4,inetCondition=1,iconGroup=IconGroup(Wi-Fi Icons),activityIn=false,activityOut=false,rssi=-51,lastModified=07-30 21:43:46,ssid="TP-Link_4837_5G",isTransient=false,statusLabel=null
07-30 21:44:42.861  1685  1748 E LightsService: Light requested not available on this device. 2
07-30 21:44:43.792  1685  1987 D WifiTrafficPoller: TRAFFIC_STATS_POLL true Token 625 num clients 1
07-30 21:44:43.794  1685  1987 D WifiTrafficPoller:  packet count Tx=76508 Rx=110820
07-30 21:44:43.797  2122  2402 D MessengerImpl: <send> message = 1 ,msg.sendingUid = 1000 ,Handler = Handler (com.android.systemui.statusbar.policy.WifiSignalController$WifiHandler) {3efccc8}
07-30 21:44:43.800  2122  2122 D NetworkController.WifiSignalController: Change in state from: connected=true,enabled=true,level=4,inetCondition=1,iconGroup=IconGroup(Wi-Fi Icons),activityIn=false,activityOut=false,rssi=-51,lastModified=07-30 21:44:42,ssid="TP-Link_4837_5G",isTransient=false,statusLabel=null
07-30 21:44:43.800  2122  2122 D NetworkController.WifiSignalController:    to: connected=true,enabled=true,level=4,inetCondition=1,iconGroup=IconGroup(Wi-Fi Icons),activityIn=false,activityOut=true,rssi=-51,lastModified=07-30 21:44:42,ssid="TP-Link_4837_5G",isTransient=false,statusLabel=null
07-30 21:44:44.797  1685  1987 D WifiTrafficPoller: TRAFFIC_STATS_POLL true Token 625 num clients 1
07-30 21:44:44.799  1685  1987 D WifiTrafficPoller:  packet count Tx=76509 Rx=110820
07-30 21:44:45.117   728   868 D SDM     : DisplayBase::BuildLayerStackStats: LayerStack layer_count: 7, app_layer_count: 6, gpu_target_index: 6, display type: 0
07-30 21:44:45.419   728   868 I chatty  : uid=1000(system) HwBinder:728_2 identical 1 line
07-30 21:44:45.637   728   868 D SDM     : DisplayBase::BuildLayerStackStats: LayerStack layer_count: 7, app_layer_count: 6, gpu_target_index: 6, display type: 0
07-30 21:44:45.800  1685  1987 D WifiTrafficPoller: TRAFFIC_STATS_POLL true Token 625 num clients 1
07-30 21:44:45.802  1685  1987 D WifiTrafficPoller:  packet count Tx=76526 Rx=110835
07-30 21:44:45.803  2122  2402 D MessengerImpl: <send> message = 1 ,msg.sendingUid = 1000 ,Handler = Handler (com.android.systemui.statusbar.policy.WifiSignalController$WifiHandler) {3efccc8}
07-30 21:44:45.805  2122  2122 D NetworkController.WifiSignalController: Change in state from: connected=true,enabled=true,level=4,inetCondition=1,iconGroup=IconGroup(Wi-Fi Icons),activityIn=false,activityOut=true,rssi=-51,lastModified=07-30 21:44:43,ssid="TP-Link_4837_5G",isTransient=false,statusLabel=null
07-30 21:44:45.805  2122  2122 D NetworkController.WifiSignalController:    to: connected=true,enabled=true,level=4,inetCondition=1,iconGroup=IconGroup(Wi-Fi Icons),activityIn=true,activityOut=true,rssi=-51,lastModified=07-30 21:44:43,ssid="TP-Link_4837_5G",isTransient=false,statusLabel=null

释放Urllib函数按钮时输出Logcat

07-30 21:51:09.902  9393  9417 I python  :  ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1051)
07-30 21:51:09.903  9393  9417 I python  :  
07-30 21:51:09.903  9393  9417 I python  :  During handling of the above exception, another exception occurred:
07-30 21:51:09.903  9393  9417 I python  :  
07-30 21:51:09.904  9393  9417 I python  :  Traceback (most recent call last):
07-30 21:51:09.904  9393  9417 I python  :    File "/home/dataleaf/Build_Environ/.buildozer/android/app/main.py", line 499, in <module>
07-30 21:51:09.905  9393  9417 I python  :    File "/home/dataleaf/Build_Environ/.buildozer/android/platform/build/build/python-installs/urlsign/kivy/app.py", line 855, in run
07-30 21:51:09.906  9393  9417 I python  :    File "/home/dataleaf/Build_Environ/.buildozer/android/platform/build/build/python-installs/urlsign/kivy/base.py", line 504, in runTouchApp
07-30 21:51:09.906  9393  9417 I python  :    File "/home/dataleaf/Build_Environ/.buildozer/android/platform/build/build/python-installs/urlsign/kivy/core/window/window_sdl2.py", line 746, in mainloop
07-30 21:51:09.907  9393  9417 I python  :    File "/home/dataleaf/Build_Environ/.buildozer/android/platform/build/build/python-installs/urlsign/kivy/core/window/window_sdl2.py", line 478, in _mainloop
07-30 21:51:09.908  9393  9417 I python  :    File "/home/dataleaf/Build_Environ/.buildozer/android/platform/build/build/python-installs/urlsign/kivy/base.py", line 342, in idle
07-30 21:51:09.909  9393  9417 I python  :    File "/home/dataleaf/Build_Environ/.buildozer/android/platform/build/build/python-installs/urlsign/kivy/base.py", line 327, in dispatch_input
07-30 21:51:09.909  9393  9417 I python  :    File "/home/dataleaf/Build_Environ/.buildozer/android/platform/build/build/python-installs/urlsign/kivy/base.py", line 293, in post_dispatch_input
07-30 21:51:09.910  9393  9417 I python  :    File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
07-30 21:51:09.911  9393  9417 I python  :    File "/home/dataleaf/Build_Environ/.buildozer/android/platform/build/build/python-installs/urlsign/kivy/uix/behaviors/button.py", line 179, in on_touch_up
07-30 21:51:09.920  9393  9417 I python  :    File "kivy/_event.pyx", line 703, in kivy._event.EventDispatcher.dispatch
07-30 21:51:09.922  9393  9417 I python  :    File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
07-30 21:51:09.923  9393  9417 I python  :    File "kivy/_event.pyx", line 1098, in kivy._event.EventObservers._dispatch
07-30 21:51:09.925  9393  9417 I python  :    File "/home/dataleaf/Build_Environ/.buildozer/android/platform/build/build/python-installs/urlsign/kivy/lang/builder.py", line 64, in custom_callback
07-30 21:51:09.926  9393  9417 I python  :    File "<string>", line 179, in <module>
07-30 21:51:09.926  9393  9417 I python  :    File "/home/dataleaf/Build_Environ/.buildozer/android/app/main.py", line 300, in on_urllib_prog
07-30 21:51:09.927  9393  9417 I python  :    File "/home/dataleaf/Build_Environ/.buildozer/android/app/main.py", line 303, in url_library
07-30 21:51:09.928  9393  9417 I python  :    File "/home/dataleaf/Build_Environ/.buildozer/android/platform/build/build/other_builds/python3-libffi-openssl-sqlite3/armeabi-v7a__ndk_target_21/python3/Lib/urllib/request.py", line 222, in urlopen
07-30 21:51:09.929  9393  9417 I python  :    File "/home/dataleaf/Build_Environ/.buildozer/android/platform/build/build/other_builds/python3-libffi-openssl-sqlite3/armeabi-v7a__ndk_target_21/python3/Lib/urllib/request.py", line 525, in open
07-30 21:51:09.929  9393  9417 I python  :    File "/home/dataleaf/Build_Environ/.buildozer/android/platform/build/build/other_builds/python3-libffi-openssl-sqlite3/armeabi-v7a__ndk_target_21/python3/Lib/urllib/request.py", line 543, in _open
07-30 21:51:09.930  9393  9417 I python  :    File "/home/dataleaf/Build_Environ/.buildozer/android/platform/build/build/other_builds/python3-libffi-openssl-sqlite3/armeabi-v7a__ndk_target_21/python3/Lib/urllib/request.py", line 503, in _call_chain
07-30 21:51:09.931  9393  9417 I python  :    File "/home/dataleaf/Build_Environ/.buildozer/android/platform/build/build/other_builds/python3-libffi-openssl-sqlite3/armeabi-v7a__ndk_target_21/python3/Lib/urllib/request.py", line 1360, in https_open
07-30 21:51:09.932  9393  9417 I python  :    File "/home/dataleaf/Build_Environ/.buildozer/android/platform/build/build/other_builds/python3-libffi-openssl-sqlite3/armeabi-v7a__ndk_target_21/python3/Lib/urllib/request.py", line 1319, in do_open
07-30 21:51:09.933  9393  9417 I python  :  urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1051)>
07-30 21:51:09.933  9393  9417 I python  : Python for android ended.
07-30 21:51:10.229  1685  1987 D WifiTrafficPoller: TRAFFIC_STATS_POLL true Token 625 num clients 1
07-30 21:51:10.229  1685  1987 D WifiTrafficPoller:  packet count Tx=77858 Rx=112074
07-30 21:51:10.238  1685  1974 W InputDispatcher: channel '2fc1551 org.url_requesting.urlsign/org.kivy.android.PythonActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
07-30 21:51:10.238  1685  1974 E InputDispatcher: channel '2fc1551 org.url_requesting.urlsign/org.kivy.android.PythonActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
07-30 21:51:10.239  1685  4609 I ActivityManager: Process org.url_requesting.urlsign (pid 9393) has died: fore TOP 
07-30 21:51:10.240  1685  2993 I WindowManager: WIN DEATH: Window{2fc1551 u0 org.url_requesting.urlsign/org.kivy.android.PythonActivity}
07-30 21:51:10.240  1685  2993 W InputDispatcher: Attempted to unregister already unregistered input channel '2fc1551 org.url_requesting.urlsign/org.kivy.android.PythonActivity (server)'
07-30 21:51:10.240   648   648 I Zygote  : Process 9393 exited cleanly (255)
07-30 21:51:10.241  1685  1750 W libprocessgroup: kill(-9393, 9) failed: No such process
07-30 21:51:10.241  1685  1750 I libprocessgroup: Successfully killed process cgroup uid 10272 pid 9393 in 0ms
07-30 21:51:10.243  1685  4609 W ActivityManager: Force removing ActivityRecord{e32495c u0 org.url_requesting.urlsign/org.kivy.android.PythonActivity t6783}: app died, no saved state

更新

我再次从我的 url“https://orob.co.za”中删除了 "s",从我的 phone 中完全卸载了应用程序并重新安装(未更新)。这次 Urllib 函数给出了以下错误:

07-30 22:15:10.961 10706 10730 I python  :  socket.timeout: timed out
07-30 22:15:10.961 10706 10730 I python  : Python for android ended.

而KivyUrl请求函数第一次进入on_progress函数。但是请求没有成功,只是超时。

虽然不是 HTTP,但在更新和成功时更新...S!

成功!我破解了它。当我尝试上面的 [5] 时,我只更新了 phone 上的应用程序,并没有卸载它。我也失去了情节并将域更改为与我在初始测试中使用的域不同的域,这意味着我得到了正确的结果但错误的事实。我已将其改回“http://www.google.com”并且应用程序 return 编辑了 UrlRequest 和 Urllib 的抓取("Latin-1")。非常精彩!

问题似乎是 SSL 证书。我有 openssl 作为我的 buidozer.spec 文件的一部分,那么为什么它在 Android 和 HTTPS 中不起作用!

更新更新更新...

所以我之前的发现是正确的。 "HTTPS://..." url 的 Kivy Url 请求函数的证书验证也失败了。在修改我的代码后,我发现 on_error 函数的 error 输出是一个 string 而不是一个类似字节的对象,这意味着我尝试 decoding 一个导致错误的字符串并阻止了我的应用程序中的错误输出:

[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1051)

已编辑问题

有人可以帮助我如何 return 来自刮擦的 result.. 来自 url 和 https 吗?

我正在回答我自己的问题,因为我终于解决了它,但我不确定是否有其他方式通知其他人。
SSL证书的问题在这个论坛上有很多答案,但是解决我问题的信息大多是评论。

解决方案

使用 Python 3.6.8 和 Kivy 1.11.0

第 1 步> 我必须 pip3 install requests 才能获得 certifi python 库。
请参阅@Petar Luktina 评论“”

步骤 2> 将 ca_file=certifi.where() 参数添加到您的 UrlRequest()。作为额外,我添加了 verify=True 参数。
请参阅@Erik 评论“

步骤 3> 将 certifi and openssl 添加到 buildozer.spec 文件作为应用程序要求,否则您将得到 ModuleNotFoundError: No module named '_ctypes'。该错误是由于显式导入的库未在 .spec 文件中声明或不构成已声明包的一部分

步骤 4> 添加函数来处理 on_success 参数(不是必需的,但对我有用)。

我的编码答案:

import certifi as cfi
from kivy.network.urlrequest import UrlRequest

    def req_url(self, *args):
        the_request = UrlRequest(self.the._url, on_success=self.on_success, 
                               on_error=self.on_error, on_failure=self.on_failure,
                                     ca_file=cfi.where(), verify=True)

急!它现在适用于 Android.