Checking if the computer is online

Discussion in 'Python' started by Dave Brueck, Sep 17, 2003.

  1. Dave Brueck

    Dave Brueck Guest

    The problem with the InternetGetConnectedState API is that, at least on older
    versions of Windows/Internet Explorer, it relies on the settings of Internet
    Explorer rather than the true state of the connection. In other words, it
    would return an incorrect answer if Netscape was the only browser ever used.
    Also, it doesn't work in a connection sharing setup (e.g. your computer is on
    a home LAN that is connected to another computer with a modem.

    Maybe this is way more than the OP needs, but I'll post it anyway because it's
    tough to reliably detect the connected state. After much painstaking
    experimentation :) I came up with a fairly decent method of knowing if the
    current computer is really connected to the Internet using a combination of
    all sorts of info available (works on Windows using ctypes). It turns out
    that there is no single API to call, and some APIs give unreliable info, and
    others will cause your modem to go online if you're not online, so they must
    be avoided.

    Here's a breakdown of the determination process that works for me:

    - First and foremost, if your application is currently downloading or
    uploading then you know you're online. At first it seems silly, but it's
    actually a pretty reliable nugget of info, so my connection detection lib has
    a SetOnlineHint(isOnline) API that the app calls periodically (internally my
    library uses an exponential moving average to "degrade" this value over time
    so that if it falls below some threshold then the library doesn't consider it
    to be accurate enough to rely on and moves to the next method of detection).

    - Call the GetTcpTable API and get a list of all IP addresses whose rows have
    the dwState of MIB_TCP_STATE_ESTAB. Then filter out all IP addresses
    127.0.0.1, 10.* 192.168.*, and 172.16* through 172.31.*. If the remaining
    list is non-empty, something on this computer is most likely connected to a
    public IP address, so it's fairly safe to assume you're online

    - If RasEnumConnections gives a non-empty list then you're online (but don't
    assume the converse is true)

    - If the IsNetworkAlive API is supported and returns false, you're not online
    (But you can't rely on a "yes" answer from this API)

    - If all the above don't yield any definite answers, then ping a known IP
    address (either by using raw sockets or by just calling os.popen('ping ...')
    and reading the output. Ping is nice because ICMP packets won't cause the
    modem to autodial if you're not online. My library has as a default the IP
    addresses of a bunch of well known servers (e.g. DNS root servers) to use in
    emergencies, but these are best avoided in all but the most extreme
    emergencies when it has no other IPs to use - it collects public IP addresses
    from the GetTcpTable call above, for example.

    The library runs through this determination process every second or so in its
    own thread, pinging at most once every 5 or 10 seconds, and the application
    just queries a IsOnline API.

    Convoluted, but it works, so it was worth the effort to put it in a separate
    library and never figure it out again. :)

    -Dave
     
    Dave Brueck, Sep 17, 2003
    #1
    1. Advertisements

  2. Hi all,

    I have a silly question. Is there are simple way to check if the computer
    is connected to the Internet? It seems this should be a pretty
    straight-forward thing to do, but as I am totally unfamiliar with sockets
    and such, I ask for your help before getting my hands dirty.

    Cheers,

    Mickel G.
     
    =?ISO-8859-1?Q?Mickel_Gr=F6nroos?=, Sep 18, 2003
    #2
    1. Advertisements

  3. For windows?

    <http://article.gmane.org/gmane.comp.python.windows/969>

    Thomas
     
    Thomas Heller, Sep 18, 2003
    #3
  4. I want to get this done on both Windows (2000/XP) and (Redhat) Linux (7.x
    or higher).

    Isn't there a way to wrap a call to urllib.urlopen in some kind of timer,
    so that the call fails if there is no answer in N seconds? (I.e. if
    urllib.urlopen cannot open a specific web page in N seconds, then I
    assume that the reason is that the computer is not online.) That would be
    good enough for me. Any suggestions?

    /Mickel
     
    =?ISO-8859-1?Q?Mickel_Gr=F6nroos?=, Sep 18, 2003
    #4
  5. Of course you can do it this way. Or even simpler with the socket
    module.

    But it depends on what you exactly want:
    It's been quite a time that I had a dial-up connection, but IIRC,
    trying network activity when the computer was not connected would result
    in Windows trying to establish the connection.

    To conclude: the above mentioned URL contains code which exactly answers
    your original question.

    Thomas
     
    Thomas Heller, Sep 18, 2003
    #5
  6. You wouldn't have some sample code on how I can use the socket module to
    test if I can connect to a given URL? (I would't want to rely on other
    modules than the ones in the standard library, so therefore I want to
    try this socket approach.)

    Cheers,

    /Mickel
     
    =?ISO-8859-1?Q?Mickel_Gr=F6nroos?=, Sep 18, 2003
    #6
  7. Sorry, no. But it's easy to figure out yourself.

    Thomas
     
    Thomas Heller, Sep 18, 2003
    #7
  8. Dave Brueck

    Daniel Klein Guest

    'ping external_ipaddress' would be worth looking into. I'm not sure
    if a computer can recognize its own ip when it's disconnected as I
    haven't really tried it.

    Daniel Klein
     
    Daniel Klein, Sep 18, 2003
    #8
  9. First of all you need to define what "being online" means...

    Is it that your modem has dialed up your ISP and been able to
    establish a PPP connection? What if you don't use a modem, and
    instead have ethernet straight into your ISP's network equipment?
    If the link to your ISP's closest network equipment (switch/
    router/PPP dialin) is up, but the link out from that is down --
    are you "online" then? Are you online if all the links within
    your ISP is up, so you can reach everyone connected to the same
    ISP, but their peering is broken so you can't reach anyone else?
    What if there is a firewall somewhere between you and "The
    Internet", blocking you from accessing many sites, are you
    "online" then? How large part of Internet do you need to be able
    to reach to claim that you are "online"? Or must perhaps the
    Internet be able to reach *you* before you are considered to be
    online? (That means that a NAT box immediately makes you offline;
    I tend to like that definition. :)

    Or perhaps you should tell us *why* you believe you need to check
    if the computer is connected to the Internet or not. Then we can
    go directly to telling you that it is a bad idea, and suggest what
    you should do instead. <0.5 wink>
     
    Thomas Bellman, Sep 18, 2003
    #9
  10. OK, here's what I want to do:

    When the Tkinter application I am writing starts up, it checks if certain
    files on the web exists and if so, it downloads them to a local directory
    replacing possible earlier copies of these files. So, what I mean by
    checking if the computer is online is that I want to check if I can access
    these certain remote files with urllib.urlretrieve(). If I can, then I
    download them. If I can't, I assume the computer is not connected to the
    Internet and I skip the update phase.

    The problem with just running urllib.urlretrieve() is that the execution
    of the script halts as there is no timeout available for Python 2.2.2. (I
    tried timeoutsocket.py to get this done, but that only worked on Windows
    2000, not Redhat Linux 7.0. I also tried upgrading to Python 2.3, but that
    caused some other problems, especially with the gettext module and with
    the fact that my ISO-8859-1 encoded files needed a "coding" line.)

    So that's that. Anybody still got a few lines of code that would get this
    done on Python 2.2.2 on Windows _and_ Linux?

    Cheers,

    /Mickel G.
     
    =?ISO-8859-1?Q?Mickel_Gr=F6nroos?=, Sep 23, 2003
    #10
  11. Dave Brueck

    Max M Guest

    *I* would like to use it to run a service in the background on my
    Windows machine checking the machines ip adress perhaps once an hour,
    and if it was different than it is supposed to be, it should send an
    email to my mail adress containing the time and the ip adress.

    That should make it possible to make a stolen computer "phone home" with
    enough data to track it down.

    But if it tries to call up on a modem, it has sort of given itself up.
    An the new "owner" might get suspicious.


    regards Max M
     
    Max M, Sep 23, 2003
    #11
  12. Have you tried :
    socket.gethostbyname(socket.gethostname())
    or
    socket.gethostbyname_ex(socket.getfqdn())[2]

    Normally the result should be different if you're connected or not

    Cheers,
    Pierre

    "Mickel Grönroos" <> a écrit dans le message de
    OK, here's what I want to do:

    When the Tkinter application I am writing starts up, it checks if certain
    files on the web exists and if so, it downloads them to a local directory
    replacing possible earlier copies of these files. So, what I mean by
    checking if the computer is online is that I want to check if I can access
    these certain remote files with urllib.urlretrieve(). If I can, then I
    download them. If I can't, I assume the computer is not connected to the
    Internet and I skip the update phase.

    The problem with just running urllib.urlretrieve() is that the execution
    of the script halts as there is no timeout available for Python 2.2.2. (I
    tried timeoutsocket.py to get this done, but that only worked on Windows
    2000, not Redhat Linux 7.0. I also tried upgrading to Python 2.3, but that
    caused some other problems, especially with the gettext module and with
    the fact that my ISO-8859-1 encoded files needed a "coding" line.)

    So that's that. Anybody still got a few lines of code that would get this
    done on Python 2.2.2 on Windows _and_ Linux?

    Cheers,

    /Mickel G.
     
    Pierre Quentel, Sep 24, 2003
    #12
  13. Thanks for the tip! I tried this and the second line freezes execution
    when I have unplugged my Ethernet cable. So again, I need a timeout on my
    socket. This is not available in Python 2.2.2. I reckon I need to try
    Python 2.3.

    /Mickel G
     
    =?ISO-8859-1?Q?Mickel_Gr=F6nroos?=, Sep 24, 2003
    #13
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.