2

I have implemented a quick solution to check for internet connection in one python program, using what I found on SO :

def check_internet(self):
    try:
        response=urllib2.urlopen('http://www.google.com',timeout=2)
        print "you are connected"
        return True
    except urllib2.URLError as err:
        print err
        print "you are disconnected"

It works well ONCE, and show that I am not connected if I try it once. But if I re-establish the connection and try again, then it still says I am not connected.

Is the urllib2 connection not closed somehow ? Should I do something to reset it ?

ekianjo
  • 31
  • 1
  • 4
  • this won't work. Check your indenting of the code... – glglgl Jul 03 '13 at 05:55
  • Yeah my code was not indented, sorry about that. By it is correctly indented in my actual code. It works but works only once, and does not recognize when the connection is re-established. – ekianjo Jul 03 '13 at 06:04
  • 1
    what error do you see? – jfs Jul 03 '13 at 08:03
  • @ekianjo The error you see for `print err` could(!) be essential for answering your question. So it might really be helpful to tell us about that. – glglgl Jul 03 '13 at 09:41
  • Allright I will let you know. Give me a couple of hours (not able to do that right now). – ekianjo Jul 03 '13 at 09:44
  • My code works on desktop actually... but not on the handheld machine I am using running Python. Let me try to check what is going wrong... – ekianjo Jul 03 '13 at 11:48
  • Allright, the error code I get on the Handheld is when I am offline first and even after getting online after. That's until I restart the program. – ekianjo Jul 03 '13 at 12:11
  • Ok the wierd thing is that if I start the program online, execute the check_internet script first, (it works) then go offline, check again, it detects me being offline, and reactivating the connection is also recognized. But STARTING the application offline then going online does not work for some reason... Could it have anything with the connection type checked by urllib2 when importing the library ? I use a wifi connection, for info. On my desktop where this script works without issue, I use a LAN connection... could it have anything to do with eth0 / wlan0 assignment? – ekianjo Jul 03 '13 at 12:18

3 Answers3

3

This could be because of server-side caching.

Try this:

def check_internet(self):
    try:
        header = {"pragma" : "no-cache"} # Tells the server to send fresh copy
        req = urllib2.Request("http://www.google.com", headers=header)
        response=urllib2.urlopen(req,timeout=2)
        print "you are connected"
        return True
    except urllib2.URLError as err:
        print err

I haven't tested it. But according to the 'pragma' definition, it should work.

There is a good discussion here if you want to know about pragma: Difference between Pragma and Cache-control headers?

Community
  • 1
  • 1
rajpy
  • 2,436
  • 5
  • 29
  • 43
  • Thanks, I will try this! I will come back and confirm if it worked or not. – ekianjo Jul 03 '13 at 08:02
  • Which server side should have been cached if the 1st connection could not be established? – glglgl Jul 03 '13 at 08:04
  • glglgl, good point, but then what is happening that is causing the lack of detecting the second time you run this function with internet activated? There does seem to be a cache problem somewhere. – ekianjo Jul 03 '13 at 08:26
  • @ekianjo I thought about this as well - maybe a DNS cache problem - but normally the OS should be aware about that. That's why an answer to the most recent comment to your question might be very helpful... – glglgl Jul 03 '13 at 09:01
  • @glglgl: Yeah..correct!. urllib2 doesn't cache and also the server. What could be the problem? – rajpy Jul 03 '13 at 09:09
  • @ekianjo: I will test my system and let you know. – rajpy Jul 03 '13 at 09:11
  • @ekianjo: your scenario worked in my system. This is the error I got when disconnected "". Are you getting the same error? – rajpy Jul 03 '13 at 09:15
  • @rajpy Can you try it the other way around? Start disconnected first, test the script (via a button or something, without exiting the program), then connect and test the script again. On the second time in my case I still get the "you are disconnected" message (I have put that as print " you are disconnected" after err:), even though I am online. – ekianjo Jul 03 '13 at 09:27
  • @ekianjo: It worked. I used time.sleep in between. Connected to internet when program is on sleep. Can you try other way around? I mean connect to net, execute the program and disconnect from net and execute again. And also check the error you got. – rajpy Jul 03 '13 at 09:37
  • @rajpy no getting an error – ekianjo Jul 03 '13 at 12:27
  • I tried your function above and it did not solve my problem unfortunately :( I still cannot get it to work if I start offline then try to go online – ekianjo Jul 03 '13 at 12:28
  • 1
    @ekianjo: Its actually DNS problem: Have a look at http://stackoverflow.com/questions/8356517/permanent-temporary-failure-in-name-resolution-after-running-for-a-number-of-h – rajpy Jul 03 '13 at 13:53
  • @rajpy you seem to be right. The Python version on my handheld could be too old... any idea since which Python version this problem was fixed? Is there any other library I could fall back on ? – ekianjo Jul 04 '13 at 01:50
  • @ekianjo: I have no idea in which version it is fixed. Update to 2.7 which is my python version. BTW, urllib2 is fine, if you want to try out other modules, go for pycurl and requests. – rajpy Jul 04 '13 at 04:51
  • @ekianjo: Hey, are you able to resolve the issue? If so, can you update answer with your findings and resolution. Its even better if you answer your own question!:) It may help others. – rajpy Jul 05 '13 at 05:51
  • @rajpy, no, not yet, but I will try the solution from Babu below. – ekianjo Jul 06 '13 at 13:22
  • @rapjy, i tried the solution from Babu but it does not work either on the Python version used on the handheld... :( (See http://stackoverflow.com/questions/8356517/permanent-temporary-failure-in-name-resolution-after-running-for-a-number-of-h) – ekianjo Jul 06 '13 at 13:53
  • @ekianjo: Try requests and pycurl. BTW, You can raise bug here - http://docs.python.org/2.6/bugs.html . Since you are just checking internet connectivity, for time being, use shell script. – rajpy Jul 08 '13 at 05:32
0

This is how I used to check my connectivity for one of my applications.

import httplib
import socket
test_con_url = "www.google.com" # For connection testing
test_con_resouce = "/intl/en/policies/privacy/" # may change in future
test_con = httplib.HTTPConnection(test_con_url) # create a connection

try:
    test_con.request("GET", test_con_resouce) # do a GET request
    response = test_con.getresponse()
except httplib.ResponseNotReady as e:
    print "Improper connection state"
except socket.gaierror as e:
    print "Not connected"
else:
    print "Connected"

test_con.close()

I tested the code enabling/disabling my LAN connection repeatedly and it works.

Babu
  • 2,548
  • 3
  • 30
  • 47
  • How can you get a status if there is no internet connection? In this case, establishing the connection should have failed in the first place... – glglgl Jul 03 '13 at 08:05
  • @Babu, i have not tested your solution yet, let me come back to you soon. – ekianjo Jul 04 '13 at 01:51
  • @Babu, i tried your solution, it works on desktop but it does not work either on the Python version used on the Linux handheld... :( Maybe it is the same problem as the usage of urllib2 : a bug in the Python version. – ekianjo Jul 06 '13 at 13:54
  • I tested it on my Ubuntu machine 12.04 with Python version 2.7. – Babu Jul 07 '13 at 15:02
0

It will be faster to just make a HEAD request so no HTML will be fetched.
Also I am sure google would like it better this way :)

# uncomment for python2
# import httplib
import http.client as httplib

def have_internet():
    conn = httplib.HTTPConnection("www.google.com")
    try:
        conn.request("HEAD", "/")
        return True
    except:
        return False
Ivelin
  • 12,293
  • 5
  • 37
  • 35