user326260 user326260 - 1 year ago 82
Python Question

wrong in my script for download?

I write this script

import urllib
urllib.urlretrieve("URL","path\ name.jpg")

It's working
But if there is no internet it's make wrong
I want it if there is no internet. wait to connect by internet then work again

Answer Source

You can write something like this:

def wait_for_internet_connection():
    while True:
            response = urllib2.urlopen('',timeout=1)
        except urllib2.URLError:

def main():
    #your code here


The while loop will execute until there's an active internet connection, then executes your code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download