Determining if a client PC has an Internet connection

Chris S. chrisks at NOSPAM.udel.edu
Sun Sep 19 05:03:13 EDT 2004


Cliff Wells wrote:

> Hi,
> 
> I'm writing an application that needs to know if an Internet connection
> is available.  Basically, I want to have something similar to what a lot
> of email clients have, where the app can work either in "online" or
> "offline" mode (it keeps a cache of downloaded info, so it can work
> without a connection if needed).
> 
> The basic problem is this: it downloads info (RSS feeds) from a variety
> of sources.  Any one (or more) of these could conceivably fail to
> download, so simply waiting for a timeout isn't sufficient (not easy to
> differentiate between having a bad server and the client not having a
> connection).  Further, if it waits (say 30s) for the timeout to occur,
> this is going to be a bit annoying to the user.
> 
> I've considered trying to connect to a stable host (i.e. Google) and if
> that fails then assume the client can't connect to the internet, but
> this seems like a sub-optimal solution.
> 
> Is there any way to reliably determine the state of the client's
> internet connectivity?
> 
> Regards,
> Cliff
> 

As others have mentioned, there's no clean-cut paradigm in the language 
that can distinguish between the Internet and your local LAN. Your best 
bet is to simple go about your business can try accessing hosts like 
normal. When several of your requests timeout, then you can safely 
assume the local host to be offline. That's what you see in those 
applications when they say you're "offline"; an educated guess. After 
all, if you can't reach your target, then you are, for all intents and 
purposes, offline.



More information about the Python-list mailing list