Checking if the computer is online

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mickel Grönroos

    Checking if the computer is online

    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.

    --
    Mickel Grönroos, application specialist, linguistics, Research support,CSC
    PL 405 (Tekniikantie 15 a D), 02101 Espoo, Finland, phone +358-9-4572237
    CSC is the Finnish IT center for science, www.csc.fi

  • Thomas Heller

    #2
    Re: Checking if the computer is online

    Mickel Grönroos <mickel@csc.f i> writes:
    [color=blue]
    > 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.[/color]

    For windows?

    <http://article.gmane.o rg/gmane.comp.pyth on.windows/969>

    Thomas

    Comment

    • Mickel Grönroos

      #3
      Re: Checking if the computer is online

      On Thu, 18 Sep 2003, Thomas Heller wrote:
      [color=blue]
      > Mickel Grönroos <mickel@csc.f i> writes:
      >[color=green]
      > > 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.[/color]
      >
      > For windows?
      >
      > <http://article.gmane.o rg/gmane.comp.pyth on.windows/969>[/color]

      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

      Comment

      • Thomas Heller

        #4
        Re: Checking if the computer is online

        Mickel Grönroos <mickel@csc.f i> writes:
        [color=blue]
        > On Thu, 18 Sep 2003, Thomas Heller wrote:
        >[color=green]
        >> Mickel Grönroos <mickel@csc.f i> writes:
        >>[color=darkred]
        >> > 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.[/color]
        >>
        >> For windows?
        >>
        >> <http://article.gmane.o rg/gmane.comp.pyth on.windows/969>[/color]
        >
        > 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?[/color]

        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

        Comment

        • Mickel Grönroos

          #5
          Re: Checking if the computer is online

          On Thu, 18 Sep 2003, Thomas Heller wrote:
          [color=blue]
          > Mickel Grönroos <mickel@csc.f i> writes:
          >[color=green]
          > > On Thu, 18 Sep 2003, Thomas Heller wrote:
          > >[color=darkred]
          > >> Mickel Grönroos <mickel@csc.f i> writes:
          > >>
          > >> > 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.
          > >>
          > >> For windows?
          > >>
          > >> <http://article.gmane.o rg/gmane.comp.pyth on.windows/969>[/color]
          > >
          > > 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?[/color]
          >
          > Of course you can do it this way. Or even simpler with the socket
          > module.[/color]

          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

          Comment

          • Thomas Heller

            #6
            Re: Checking if the computer is online

            Mickel Grönroos <mickel@csc.f i> writes:
            [color=blue]
            > 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.)[/color]

            Sorry, no. But it's easy to figure out yourself.

            Thomas

            Comment

            • Dave Brueck

              #7
              Re: Checking if the computer is online

              On Thursday 18 September 2003 01:10 am, Thomas Heller wrote:[color=blue]
              > Mickel Grönroos <mickel@csc.f i> writes:[color=green]
              > > 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.[/color]
              >
              > For windows?
              >
              > <http://article.gmane.o rg/gmane.comp.pyth on.windows/969>[/color]

              The problem with the InternetGetConn ectedState 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(i sOnline) 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_E STAB. 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 RasEnumConnecti ons 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

              Comment

              • Daniel Klein

                #8
                Re: Checking if the computer is online

                On Thu, 18 Sep 2003 09:16:03 +0300 (EEST), Mickel Grönroos
                <mickel@csc.f i> wrote:
                [color=blue]
                >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[/color]

                'ping external_ipaddr ess' 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

                Comment

                • Thomas Bellman

                  #9
                  Re: Checking if the computer is online

                  Mickel Grönroos <mickel@csc.f i> wrote:
                  [color=blue]
                  > Hi all,[/color]
                  [color=blue]
                  > 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.[/color]

                  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, Lysator Computer Club, Linköping University, Sweden
                  "I don't think [that word] means what you ! bellman @ lysator.liu.se
                  think it means." -- The Princess Bride ! Make Love -- Nicht Wahr!

                  Comment

                  • Mickel Grönroos

                    #10
                    Re: Checking if the computer is online

                    On Thu, 18 Sep 2003, Thomas Bellman wrote:
                    [color=blue]
                    > Mickel Grönroos <mickel@csc.f i> wrote:
                    >[color=green]
                    > > Hi all,[/color]
                    >[color=green]
                    > > 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.[/color]
                    >
                    > First of all you need to define what "being online" means...[/color]
                    [...][color=blue]
                    > 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>[/color]

                    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.urlretri eve(). 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.urlretri eve() is that the execution
                    of the script halts as there is no timeout available for Python 2.2.2. (I
                    tried timeoutsocket.p y 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.

                    --
                    Mickel Grönroos, application specialist, linguistics, Research support,CSC
                    PL 405 (Tekniikantie 15 a D), 02101 Espoo, Finland, phone +358-9-4572237
                    CSC is the Finnish IT center for science, www.csc.fi

                    Comment

                    • Max M

                      #11
                      Re: Checking if the computer is online

                      > On Thu, 18 Sep 2003, Thomas Bellman wrote:
                      [color=blue][color=green]
                      >>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>[/color][/color]


                      *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


                      Comment

                      • Pierre Quentel

                        #12
                        Re: Checking if the computer is online

                        Have you tried :
                        socket.gethostb yname(socket.ge thostname())
                        or
                        socket.gethostb yname_ex(socket .getfqdn())[2]

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

                        Cheers,
                        Pierre

                        "Mickel Grönroos" <mickel@csc.f i> a écrit dans le message de
                        news:mailman.10 64300795.6408.p ython-list@python.org ...
                        On Thu, 18 Sep 2003, Thomas Bellman wrote:
                        [color=blue]
                        > Mickel Grönroos <mickel@csc.f i> wrote:
                        >[color=green]
                        > > Hi all,[/color]
                        >[color=green]
                        > > I have a silly question. Is there are simple way to check if the[/color][/color]
                        computer[color=blue][color=green]
                        > > is connected to the Internet? It seems this should be a pretty
                        > > straight-forward thing to do, but as I am totally unfamiliar with[/color][/color]
                        sockets[color=blue][color=green]
                        > > and such, I ask for your help before getting my hands dirty.[/color]
                        >
                        > First of all you need to define what "being online" means...[/color]
                        [...][color=blue]
                        > 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>[/color]

                        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.urlretri eve(). 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.urlretri eve() is that the execution
                        of the script halts as there is no timeout available for Python 2.2.2. (I
                        tried timeoutsocket.p y 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.

                        --
                        Mickel Grönroos, application specialist, linguistics, Research support, CSC
                        PL 405 (Tekniikantie 15 a D), 02101 Espoo, Finland, phone +358-9-4572237
                        CSC is the Finnish IT center for science, www.csc.fi


                        Comment

                        • Pierre Quentel

                          #13
                          Re: Checking if the computer is online

                          Have you tried :
                          socket.gethostb yname(socket.ge thostname())
                          or
                          socket.gethostb yname_ex(socket .getfqdn())[2]

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

                          Cheers,
                          Pierre

                          "Mickel Grönroos" <mickel@csc.f i> a écrit dans le message de
                          news:mailman.10 64300795.6408.p ython-list@python.org ...
                          On Thu, 18 Sep 2003, Thomas Bellman wrote:
                          [color=blue]
                          > Mickel Grönroos <mickel@csc.f i> wrote:
                          >[color=green]
                          > > Hi all,[/color]
                          >[color=green]
                          > > I have a silly question. Is there are simple way to check if the[/color][/color]
                          computer[color=blue][color=green]
                          > > is connected to the Internet? It seems this should be a pretty
                          > > straight-forward thing to do, but as I am totally unfamiliar with[/color][/color]
                          sockets[color=blue][color=green]
                          > > and such, I ask for your help before getting my hands dirty.[/color]
                          >
                          > First of all you need to define what "being online" means...[/color]
                          [...][color=blue]
                          > 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>[/color]

                          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.urlretri eve(). 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.urlretri eve() is that the execution
                          of the script halts as there is no timeout available for Python 2.2.2. (I
                          tried timeoutsocket.p y 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.

                          --
                          Mickel Grönroos, application specialist, linguistics, Research support, CSC
                          PL 405 (Tekniikantie 15 a D), 02101 Espoo, Finland, phone +358-9-4572237
                          CSC is the Finnish IT center for science, www.csc.fi


                          Comment

                          • Mickel Grönroos

                            #14
                            Re: Checking if the computer is online

                            On Wed, 24 Sep 2003, Pierre Quentel wrote:
                            [color=blue]
                            > Have you tried :
                            > socket.gethostb yname(socket.ge thostname())
                            > or
                            > socket.gethostb yname_ex(socket .getfqdn())[2]
                            >
                            > Normally the result should be different if you're connected or not[/color]

                            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

                            Comment

                            • Mickel Grönroos

                              #15
                              Re: Checking if the computer is online

                              On Wed, 24 Sep 2003, Pierre Quentel wrote:
                              [color=blue]
                              > Have you tried :
                              > socket.gethostb yname(socket.ge thostname())
                              > or
                              > socket.gethostb yname_ex(socket .getfqdn())[2]
                              >
                              > Normally the result should be different if you're connected or not[/color]

                              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

                              Comment

                              Working...