[Tutor] checking connection to internet until success
Alan Gauld
alan.gauld at yahoo.co.uk
Tue Jan 2 11:36:54 EST 2018
On 01/01/18 21:38, Pi wrote:
> 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.
The first obvious pointto make is that all modern OS come
with an option to read the date/time from an internet time
server so this is the correct solution to your problem
rather than writing a web scraper. At the very least you
should access a time server directly rather than scrape
a web site. (try the ntplib module)
But if you do want to try doing it yourself...
> 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.
WE need to see what doesn't work to comment. A while loop
with a suitable sleep() should work.
But again it's probably better to use the OS to set up
a timed job.
On Unix this is done either with 'cron' or 'at'.
On Windows I believe 'at' is the tool.
There are other schedulers available too with GUIs etc.
> how check connection to internet to check for every lets say 30 seconds
> until success?
What is success? When the times match? Or when they don't?
Or just whenever you succesfully connect?
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list