[Tutor] Fwd: Re: checking connection to internet until success

Alan Gauld alan.gauld at yahoo.co.uk
Tue Jan 2 20:39:58 EST 2018


Forwarding to list...



-------- Forwarded Message --------
Subject: 	Re: [Tutor] checking connection to internet until success
Date: 	Wed, 3 Jan 2018 01:35:18 +0100
From: 	Pi <guest0x013 at gmail.com>
To: 	Alan Gauld <alan.gauld at yahoo.co.uk>



10 months ago i wrote application using time from OS (Windows 7), but my 
alpha tester found how to trick my app. Thats why i need to use date 
from web. And success of new application is when code will know, that 
computer is connected to web and can get actual date.


On 02.01.2018  17:36, Alan Gauld via Tutor wrote:
> 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?
>




More information about the Tutor mailing list