user326260 user326260 - 13 days ago 5
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

You can write something like this:

def wait_for_internet_connection():
    while True:
        try:
            response = urllib2.urlopen('http://google.com',timeout=1)
            return
        except urllib2.URLError:
            pass

def main():
    #your code here

wait_for_internet_connection()
main()

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

Comments