From: eglyph on
On 18 мар, 00:47, News123 <news1...(a)free.fr> wrote:
> 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
> "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