Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

url2lib (windows 7) does not notice when network reconnects (getaddrinfo problem)

66 views
Skip to first unread message

News123

unread,
Mar 17, 2010, 6:47:41 PM3/17/10
to
Hi,


I'd like to write a function, that knows when the 'internet' is reachable.

My setup is a windows7 host with a wireless USB modem. The modem might
connect / disconnect any time.

I thought I write a small function just checking whether I can fetch a url.
######### script starts
import time,urllib2

myurl = "http://www.mysite.com" # or "www.google.com" or whatever you like

while True:
connected = False
try:
urllib2.urlopen(myurl)
connected = True
except urllib2.URLError as e:
print "urlerr %s" % e
print "connected",connected
time.sleep(1)
########## end of script

if the network is connected when I start the script the first time after
reboot, then I receive
> connected True
As expected

If the network is disconnected when I start the script the first time
after reboot, then I receive
> urlerr <urlopen error [Errno 11004] getaddrinfo failed>
> connected False
as expected

Things are getting weired, when my USB wireless modem connects after
I started my script:

It happens, that I just continue to receive:
> urlerr <urlopen error [Errno 11004] getaddrinfo failed>
> connected False

however when I use a windows cmd window and I start
"ping www.mysite.com", then my python script recovers and reports
> connected True

My perhaps false conclusion is, that getaddrinfo() is not retrying to
resolve an IP address, when it failed once.

Is this behaviour known? Can this really be possible?
I can reproduce this issue. not every time, but rather often.

If yes, is there any way to force a new host name lookup under windows?


What else could I be doing wrong?


Thanks a lot in advance for any ideas


N



egl...@gmail.com

unread,
Mar 18, 2010, 6:19:55 AM3/18/10
to
> "pingwww.mysite.com", then my python script recovers and reports

>
> > connected True
>
> My perhaps false conclusion is, that getaddrinfo() is not retrying to
> resolve an IP address, when it failed once.
>
> Is this behaviour known? Can this really be possible?
> I can reproduce this issue. not every time, but rather often.
>
> If yes, is there any way to force a new host name lookup under windows?
>
> What else could I be doing wrong?
>
> Thanks a lot in advance for any ideas
>
> N

Actually this should be expected behavior. Windows establishes
connection automatically when you start pinging something, your script
doesn't. Just check the connection state with winapi (ctypes?
pywin32?) and establish it if it isn't already.

--
regards, eGlyph

0 new messages