Pi wrote: > Hi Tutor, > > with this code i am getting actual date from internet. I need correct > date, because i am not sure this set on computer is right. > > import requests, time > > try: > OnLineDate = requests.get("http://just-the-time.appspot.com").text[:10] > OffLineDate = time.strftime("%Y-%m-%d") > > if OnLineDate == OffLineDate: > > do_something > > else: > > do_something_else > > > except requests.ConnectionError: > print("Can not connect.") > > But this code is run once. And i am stuck. Tries with while loop doesnt > took any advance. I was thinking about something like after method used > in tkinter, but still i cant figure out how do this. Can You guide me, > how check connection to internet to check for every lets say 30 seconds > until success?
You are on the right track with the while-loop. You can use the else clause of the try...except construct to break out of the loop once you have successfully read the date: # untested import requests, time while True: # run forever try: OnLineDate = requests.get("http://just-the-time.appspot.com").text[:10] except requests.ConnectionError: print("Can not connect.") else: # no exception OffLineDate = time.strftime("%Y-%m-%d") if OnLineDate == OffLineDate: do_something else: do_something_else break # out of the while-True loop time.sleep(30) # try again after 30 seconds If you want to give up after a few tries replace the while loop with for attempt in range(max_attempts): ... # same as above _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor