Kivy UrlRequest called as a class - Methods not executed? -
trying experiment , build class resolving around use of urlrequest check if given url valid. turns out being bit more difficult anticipated!
the issue on_success , on_failure/error methods defined part of class never called. script throws following output (based on print commands):
http://www.google.com request sent url doesn't work
now suspicion i´m getting return code ("none") test_connection method, , not connectionsuccess or connectionfailure. how can make call wait 1 of latter give return? suggestion welcome. thanks.
from kivy.app import app kivy.uix.floatlayout import floatlayout kivy.network.urlrequest import urlrequest class webexplorer(): def test_connection(self, path): self.path = path print (self.path) req = urlrequest(self.path,on_failure=self.connectionfailure,on_error=self.connectionfailure,on_success=self.connectionsuccess) print ("request sent") def connectionsuccess(self,*args): print ("connectionsuccess") return 0 def connectionfailure(self,*args): print ("connectionfailure") return 1 class mainscreen(floatlayout): def __init__(self, **kwargs): super(mainscreen, self).__init__(**kwargs) self.address = 'http://www.google.com' if webexplorer().test_connection(self.address) == 0: print ("url works") else: print ("url doesn't work") class app(app): def build(self): return mainscreen() if __name__ == "__main__": app().run()
update 2016-09-27 changed code , have spent hours on trying figure out problem. first code:
from kivy.app import app kivy.uix.floatlayout import floatlayout kivy.network.urlrequest import urlrequest class webexplorer(): def test_connection(self, path): self.path = path req = urlrequest(self.path,on_failure=self.connectionfailure,on_error=self.connectionfailure,on_success=self.connectionsuccess) req.wait() return (self._return_value) def connectionsuccess(self, req, results): print ("success") self._return_value = [0,results] def connectionfailure(self, req, results): print ("failure") self._return_value = [1,results] class mainscreen(floatlayout): def __init__(self, **kwargs): super(mainscreen, self).__init__(**kwargs) self.urltest = ['http://www.ikea.com/','https://www.google.com','https://www.sdfwrgaeh.com'] url in self.urltest: self.returncode = webexplorer().test_connection(url) if self.returncode[0] == 0: print ("correct url") else: print ("wrong url") class app(app): def build(self): return mainscreen() if __name__ == "__main__": app().run()
why 3 urls? because 1 "correct" (ikea), 1 redirect (google) , 1 bogus. turns out, code works first 1 only. req.wait doesn't work when result failure/error (btw have 0 idea what's difference between these two).
so question how make req.wait process failure, alternatively how exit class correct error code. considered clock.schedule_interval periodically check status, ut since event methods not executed when url incorrect, have set variales -_-
urlrequest asynchron, means actual request processed in background while application proceeds. functions on_failure
etc. callbacks, i.e. called when request done - means return value "lost". return value indeed test_connection
function.
you mentioned on mailing list want make call root url, processing on , issue new requests suburls depending on content , user interaction.
the following should provide skeleton trying do.
class webexplorer def __init__(self, gui): self._visited_urls = [] # prevent endless redirect loops self._visiting = set() # check if still doing self.gui = gui # used display buttons in process_result def get_url(self, url): self._visited_urls.append(url) self._visiting.add(url) urlrequest(url, on_success=self.process_result, on_failure=self.failure, on_error=self.failure, # difference between failure , error? on_redirect=self.redirect) def redirect(self, req): self._visiting.discard(req.url) self.get_url(req.req_headers["location"]) # check if correct handling of 3xx def process_result(self, req): self._visiting.discard(req.url) #todo process body extract urls want visit box = boxlayout() url in urls_you_want_to_visit: button = button() button.text = url # i'm not sure if following works due scoping oddities # if - no matter button pressed - same url called # line @ fault button.bind(on_press=lambda: self.get_url(url)) box.add_widget(button) gui.add_widget(box)
of course there still lot improve. it's not tested can contain bugs.
Comments
Post a Comment