我正在尝试获取Python 3.7 Kivy代码以使用UrlRequest检索https网络数据。代码可以很好地与http配合使用,但是将URL更改为任何https时却没有任何数据。当我同时使用http或https编译并运行时,两者都运行无误。我需要添加导入以使https工作吗?这是测试代码。谢谢。
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.network.urlrequest import UrlRequest
from functools import partial
class MainApp(App):
def build(self):
grid = GridLayout(cols=1)
button1 = Button(text="Press to say Hello",
on_release=self.run_Hello)
button2 = Button(text="Kivy UrlRequest",
on_release=self.run_UrlRequests)
blank_button = Button(text="Click me!")
grid.add_widget(button1)
grid.add_widget(button2)
grid.add_widget(blank_button)
return grid
def run_Hello(self, *args):
print("Hello")
def run_UrlRequests(self, *args):
for i in range(10):
self.r = UrlRequest("https://www.google.com", verify=False,
on_success=partial(self.update_label, i),
on_error=partial(self.error_label, i))
def update_label(self, i, *args):
print(i)
print("success")
print(self.r.result)
def error_label(self, i, *args):
print("failed")
print(i)
print(self.r.result)
MainApp()。run()
答案 0 :(得分:0)
以下示例说明了如何针对https
使用Kivy UrlRequest
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.network.urlrequest import UrlRequest
from kivy.uix.rst import RstDocument
from kivy.uix.label import Label
class MyWidget(BoxLayout):
def __init__(self, **kwargs):
super(MyWidget, self).__init__(**kwargs)
self.orientation = 'vertical'
search_url = "https://www.metservice.com/towns-cities/christchurch/christchurch-city"
self.add_widget(Label(text=search_url, size_hint=(1, 0.1)))
self.request = UrlRequest(search_url, self.res)
def res(self, *args):
self.add_widget(RstDocument(text=self.request.result))
class MyApp(App):
def build(self):
return MyWidget()
if __name__ == '__main__':
MyApp().run()
答案 1 :(得分:0)
def run_UrlRequests(self, *args):
for i in range(10):
self.r = UrlRequest("https://www.google.com", verify=False,
on_success=partial(self.update_label, i), on_error=partial(self.error_label, i))
我在UrlRequest之后也添加了verify = False,也添加到了原始代码中。该代码运行并生成html数据的打印语句。尽管这可以解决https问题,但我不知道此明显的SSL问题是否已得到正确解决。