Have not gotten any responses on this, nor very much by way of searching, which 
is strange and a little disappointing for such a seemingly basic thing. (May 
just be too obvious of a thing, so no one wanted to post the solution:-). )

But, I did find a decent recipe on ASPN that serves the purpose, so I will 
share in case others needed to do the same check as I did.

Before I needed to make the network call in my program, I have the following:

        if checkURL('http://www.google.com/'):
            networkup = True
        else:
            networkup = False
        if networkup:
            print "Internet connection seems to be up."
        else:
            print "Internet connection seems to be down. Please check it",
            print "and retry."
            sys.exit()

Then I continue with my network calls. The function I use is:

def checkURL(url):
    """For checking internet connection. Taken from recipe:
    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/101276""";
    try:
        p = urlparse(url)
        h = HTTP(p[1])
        h.putrequest('HEAD', p[2])
        h.endheaders()
        if h.getreply()[0] == 200: return 1
        else: return 0
    except:
        return 0

The nice thing about this check is that is just looks at the head of the site, 
and so is rather fast.

One other consideration: While it is a rare day indeed that Google.com is ever 
down, to be even safer, would could check against several reliable sites, such 
as Amazon, Yahoo, w3c.org, etc. The status of each check could be put in a 
list, and if any list item was networkup, then the internet connection may be 
considered up.

Hope someone finds this useful.

-Sam

_______________________
----- Original Message ----
From: wormwood_3 <[EMAIL PROTECTED]>
To: Python Tutorlist <tutor@python.org>
Sent: Thursday, September 6, 2007 4:46:08 PM
Subject: Re: [Tutor] Socket Timeout Handling

Since no one bit on this yet, let me simplify to the core issue I am having:

What is the best practice for checking for network connectivity errors when 
making network calls? Is it better to wrap the functions that make said calls 
in threads and time them? Or to use timeout variables for modules like socket? 
Something else?

I found some good general info here: 
http://www.onlamp.com/pub/a/python/2003/11/06/python_nio.html

But I have had a hard time finding info on network error handling specifically.

Thoughts?

______________________________________
----- Original Message ----
From: wormwood_3 <[EMAIL PROTECTED]>
To: Python Tutorlist <tutor@python.org>
Sent: Thursday, September 6, 2007 9:40:21 AM
Subject: [Tutor] Socket Timeout Handling

I am trying to figure out the optimal way to make socket connections (INET) and 
check for timeouts. The socket module has settimeout(timeout) and 
setdefaulttimeout(timeout). However, so far as I can tell, these apply to 
socket objects. The type of socket connection I want to make is 
getfqdn(address). So I can set the default timeout for socket, but not a socket 
object (makes sense so far). I cannot use the getfqdn(address) method on a 
socket object, I have to use it on socket. This means (as I understand it thus 
far), that while I can set a timeout value for socket objects, this will not 
apply to when I use the getfqdn() method, which is where I need a timeout 
check! Some example code for the steps so far:

>>> import socket
>>> conn = socket.socket()
>>> conn.setdefaulttimeout(2.0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: '_socketobject' object has no attribute 'setdefaulttimeout'
>>> socket.setdefaulttimeout(2.0)
>>> conn.getfqdn("64.33.212.2")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: '_socketobject' object has no attribute 'getfqdn'
>>> socket.getfqdn("64.33.212.2")
'64-33-212-2.customers.pingtone.net'
>>> # Disconnected network connection here
... 
>>> socket.getfqdn("64.33.212.2")
'64.33.212.2'
>>> # Reconnected network connection here
>>> socket.getfqdn("64.33.212.2")
'64-33-212-2.customers.pingtone.net'

After I disconnected my network connection and called getfqdn(), it returned 
the IP address I called it with after about 25 seconds. So the default timeout 
was ignored? Is there some other way to call this function so that I can check 
for timeouts? Should I instead just put my network calls in a thread and see 
how long they take, stopping them after a certain period?

Thanks for any help.

-Sam


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor



_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor



_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to