Testing for Internet Connection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alexnb

    Testing for Internet Connection


    Hello internet.

    I am wondering, is there a simple way to test for Internet connection? If
    not, what is the hard way :p
    --
    View this message in context: http://www.nabble.com/Testing-for-In...p18460572.html
    Sent from the Python - python-list mailing list archive at Nabble.com.

  • Alex Marandon

    #2
    Re: Testing for Internet Connection

    Alexnb wrote:
    I am wondering, is there a simple way to test for Internet connection? If
    not, what is the hard way :p
    Trying to fetch the homepage from a few major websites (Yahoo, Google,
    etc.)? If all of them are failing, it's very likely that the connection
    is down. You can use urllib2 [1] to accomplish that.

    [1] <http://docs.python.org/lib/module-urllib2.html>

    Comment

    • Ben Finney

      #3
      Re: Testing for Internet Connection

      Alexnb <alexnbryan@gma il.comwrites:
      I am wondering, is there a simple way to test for Internet
      connection? If not, what is the hard way :p
      Refine the question: What do you mean by "internet"? It isn't a single
      entity.

      Do you mean "some particular internet host responding on a particular
      network port"?

      If you can define exactly what you mean by "internet connection", the
      test for it becomes correspondingly easier.

      --
      \ “Why should I care about posterity? What's posterity ever done |
      `\ for me?” —Groucho Marx |
      _o__) |
      Ben Finney

      Comment

      • Thomas Troeger

        #4
        Re: Testing for Internet Connection

        Alex Marandon wrote:
        Alexnb wrote:
        >I am wondering, is there a simple way to test for Internet connection? If
        >not, what is the hard way :p
        >
        Trying to fetch the homepage from a few major websites (Yahoo, Google,
        etc.)? If all of them are failing, it's very likely that the connection
        is down. You can use urllib2 [1] to accomplish that.
        >
        [1] <http://docs.python.org/lib/module-urllib2.html>
        This seems to work and is rather fast and wastes no bandwidth:

        =============== =============== =============== =============== =============== ===
        #!/usr/bin/python

        import socket, struct

        def check_host(host , port, timeout=1):
        """
        Check for connectivity to a certain host.
        """
        # assume we have no route.
        ret=False

        # connect to host.
        try:
        # create socket.
        sock=socket.soc ket()
        # create timeval structure.
        timeval=struct. pack("2I", timeout, 0)
        # set socket timeout options.
        sock.setsockopt (socket.SOL_SOC KET, socket.SO_RCVTI MEO, timeval)
        sock.setsockopt (socket.SOL_SOC KET, socket.SO_SNDTI MEO, timeval)
        # connect to host.
        sock.connect((h ost, port))
        # abort communications.
        sock.shutdown(S HUT_RDWR)
        # we have connectivity after all.
        ret=True
        except:
        pass

        # try to close socket in any case.
        try:
        sock.close()
        except:
        pass

        return ret

        # -------------------------------- main ---------------------------------

        if check_host("www .heise.de", 80):
        print "Horray!"
        else:
        print "We've lost headquarters!"
        =============== =============== =============== =============== =============== ===

        I hope the code is ok, but there is always something you can do better.
        Comments? :)

        Cheers,
        Thomas.

        Comment

        • Marc Christiansen

          #5
          Re: Testing for Internet Connection

          Alex Marandon <invalid@nowher e.invalid.orgwr ote:
          Alexnb wrote:
          >I am wondering, is there a simple way to test for Internet connection? If
          >not, what is the hard way :p
          >
          Trying to fetch the homepage from a few major websites (Yahoo, Google,
          etc.)? If all of them are failing, it's very likely that the connection
          is down
          or there is a mandatory proxy...

          Does it count as internet connection, when only port 80 and port 443 are
          accessible and those require going through a proxy (very strict firewall
          policy)? Or everything requires using SOCKS?
          Just some possible problems I came up with. I don't know how often some-
          thing like this will happen.

          Ciao
          Marc

          Comment

          • Alexnb

            #6
            Re: Testing for Internet Connection




            Ben Finney-2 wrote:

            Alexnb <alexnbryan@gma il.comwrites:
            >I am wondering, is there a simple way to test for Internet
            >connection? If not, what is the hard way :p
            Refine the question: What do you mean by "internet"? It isn't a single
            entity.

            Do you mean "some particular internet host responding on a particular
            network port"?

            If you can define exactly what you mean by "internet connection", the
            test for it becomes correspondingly easier.

            --
            \ “Why should I care about posterity? What's posterity everdone |
            `\ for me?” —Groucho Marx |
            _o__) |
            Ben Finney
            --
            Well, really I just need to figure out if I am able to connect to one site.
            That site is dictionary.com.
            --
            View this message in context: http://www.nabble.com/Testing-for-In...p18468350.html
            Sent from the Python - python-list mailing list archive at Nabble.com.

            Comment

            • Grant Edwards

              #7
              Re: Testing for Internet Connection

              >If you can define exactly what you mean by "internet connection", the
              >test for it becomes correspondingly easier.
              Well, really I just need to figure out if I am able to connect
              to one site. That site is dictionary.com.
              Then use urllib2 to try to fetch a page from dictionary.com. If
              it works, you're "connected" . If it fails, you're not. The
              most straight-forward way to find out if you can do X is to try
              to do X.

              --
              Grant Edwards grante Yow! if it GLISTENS,
              at gobble it!!
              visi.com

              Comment

              • Alexnb

                #8
                Re: Testing for Internet Connection




                Troeger Thomas (Ext) wrote:
                >
                Alex Marandon wrote:
                >Alexnb wrote:
                >>I am wondering, is there a simple way to test for Internet connection?
                >>If
                >>not, what is the hard way :p
                >>
                >Trying to fetch the homepage from a few major websites (Yahoo, Google,
                >etc.)? If all of them are failing, it's very likely that the connection
                >is down. You can use urllib2 [1] to accomplish that.
                >>
                >[1] <http://docs.python.org/lib/module-urllib2.html>
                >
                This seems to work and is rather fast and wastes no bandwidth:
                >
                =============== =============== =============== =============== =============== ===
                #!/usr/bin/python
                >
                import socket, struct
                >
                def check_host(host , port, timeout=1):
                """
                Check for connectivity to a certain host.
                """
                # assume we have no route.
                ret=False
                >
                # connect to host.
                try:
                # create socket.
                sock=socket.soc ket()
                # create timeval structure.
                timeval=struct. pack("2I", timeout, 0)
                # set socket timeout options.
                sock.setsockopt (socket.SOL_SOC KET, socket.SO_RCVTI MEO, timeval)
                sock.setsockopt (socket.SOL_SOC KET, socket.SO_SNDTI MEO, timeval)
                # connect to host.
                sock.connect((h ost, port))
                # abort communications.
                sock.shutdown(S HUT_RDWR)
                # we have connectivity after all.
                ret=True
                except:
                pass
                >
                # try to close socket in any case.
                try:
                sock.close()
                except:
                pass
                >
                return ret
                >
                # -------------------------------- main ---------------------------------
                >
                if check_host("www .heise.de", 80):
                print "Horray!"
                else:
                print "We've lost headquarters!"
                =============== =============== =============== =============== =============== ===
                >
                I hope the code is ok, but there is always something you can do better.
                Comments? :)
                >
                Cheers,
                Thomas.
                --

                >
                >
                Thomas this code did not work on google.com and I also tried it with port
                443
                --
                View this message in context: http://www.nabble.com/Testing-for-In...p18468756.html
                Sent from the Python - python-list mailing list archive at Nabble.com.

                Comment

                • Alexnb

                  #9
                  Re: Testing for Internet Connection




                  Alex Marandon-3 wrote:
                  >
                  Alexnb wrote:
                  >I am wondering, is there a simple way to test for Internet connection? If
                  >not, what is the hard way :p
                  >
                  Trying to fetch the homepage from a few major websites (Yahoo, Google,
                  etc.)? If all of them are failing, it's very likely that the connection
                  is down. You can use urllib2 [1] to accomplish that.
                  >
                  [1] <http://docs.python.org/lib/module-urllib2.html>
                  --

                  >
                  >
                  What exactly do you think will work? I am not sure what you think I should
                  do? If I use urlopen("http://www.google.com" ) and I am not connected, I am
                  not going to get an exception, the program will fail.

                  --
                  View this message in context: http://www.nabble.com/Testing-for-In...p18471183.html
                  Sent from the Python - python-list mailing list archive at Nabble.com.

                  Comment

                  • Grant Edwards

                    #10
                    Re: Testing for Internet Connection

                    On 2008-07-15, Alexnb <alexnbryan@gma il.comwrote:
                    What exactly do you think will work? I am not sure what you
                    think I should do? If I use urlopen("http://www.google.com" )
                    and I am not connected, I am not going to get an exception,
                    the program will fail.
                    Bullshit. You get an exception. Here's my program:

                    import urllib2
                    try:
                    con = urllib2.urlopen ("http://www.google.com/")
                    data = con.read()
                    print data
                    except:
                    print "failed"

                    If I run it with no internet connection, I get this:

                    $ python testit.py
                    failed

                    If I bring up the internet connection, then I get a bunch of
                    HTML.

                    --
                    Grant Edwards grante Yow! I'm ZIPPY the PINHEAD
                    at and I'm totally committed
                    visi.com to the festive mode.

                    Comment

                    • norseman

                      #11
                      Re: Testing for Internet Connection


                      Grant Edwards wrote:
                      On 2008-07-15, Alexnb <alexnbryan@gma il.comwrote:
                      >
                      >What exactly do you think will work? I am not sure what you
                      >think I should do? If I use urlopen("http://www.google.com" )
                      >and I am not connected, I am not going to get an exception,
                      >the program will fail.
                      >
                      Bullshit. You get an exception. Here's my program:
                      >
                      import urllib2
                      try:
                      con = urllib2.urlopen ("http://www.google.com/")
                      data = con.read()
                      print data
                      except:
                      print "failed"
                      >
                      If I run it with no internet connection, I get this:
                      >
                      $ python testit.py
                      failed
                      >
                      If I bring up the internet connection, then I get a bunch of
                      HTML.
                      >
                      =============== ==============
                      Yep -me two

                      Process:
                      copy/paste into afile
                      slide lines left to create proper indent values
                      save
                      python afile

                      I get same as Grant


                      If one does a copy/paste into interactive Python, it does fail.
                      (Lots of indent error messages. After all, it is Python :)


                      Steve
                      norseman@hughes .net

                      Comment

                      • Grant Edwards

                        #12
                        Re: Testing for Internet Connection

                        On 2008-07-15, norseman <norseman@hughe s.netwrote:
                        Process:
                        copy/paste into afile
                        slide lines left to create proper indent values
                        save
                        python afile
                        >
                        I get same as Grant
                        >
                        >
                        If one does a copy/paste into interactive Python, it does fail.
                        (Lots of indent error messages. After all, it is Python :)
                        I'm always a bit conflicted about how to post code snippets.
                        IMO, the posting is a lot more readable if the code is indented
                        to offset it from the prose, but it does make more work for
                        anybody who wants to run the example.

                        --
                        Grant Edwards grante Yow! Did I SELL OUT yet??
                        at
                        visi.com

                        Comment

                        Working...