【问题标题】:How do I check whether Kivy UrlRequest Get is running on Android?如何检查 Kivy UrlRequest Get 是否在 Android 上运行?
【发布时间】:2019-07-29 23:02:15
【问题描述】:

我正在尝试使用 Kivy 的 UrlRequest 抓取网站。当我从 Android 运行应用程序时,代码进入 UrlRequest 函数,并且永远不会返回任何失败、发生错误或成功的信息。 我如何“监听”日志输出以找出为什么什么也没发生? 或者,我的 UrlRequest 代码有什么问题?

我已经尝试了以下

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

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

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

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

5] 将网址从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)>

网址请求代码

    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

我使用的网址

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

更新

我再次从我的网址“https://orob.co.za”中删除了“s”,从我的手机中完全卸载了该应用程序并再次安装(未更新)。这次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.

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

更新并成功,尽管不是 HTTP...S!

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

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

更新更新更新...

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

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

问题已编辑

有人可以帮助我如何从抓取中返回result .. 从带有 https 的 url 中返回吗?

【问题讨论】:

    标签: python python-3.x kivy kivy-language


    【解决方案1】:

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

    解决方案

    使用 Python 3.6.8 和 Kivy 1.11.0

    第 1 步> 我必须 pip3 install requests 才能获得 certifi python 库。
    见@Petar Luktina cmets "HTTPS Request in Kivy"

    步骤 2> 将 ca_file=certifi.where() 参数添加到您的 UrlRequest()。作为额外的我添加了verify=True 参数。
    见@Erik cmets "Kivy UrlRequest with https"

    第 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 上运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-09
      • 2011-05-11
      • 2018-03-14
      • 2015-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-23
      相关资源
      最近更新 更多