0

I'm working on a simple code that is downloading a file over HTTP using the package urllib and urllib.request. Everything is working good excepted that I would like to be able to handle the network problems that could happens.

  • Checking if the computer is online (Connected to the internet). And proceed only if true.

  • Restarting the download of the file if during it, the connection is lost or too bad.

I would like, if possible, to use as less packages as possible. Here is my actual code :

import urllib
import urllib.request
url = "http://my.site.com/myFile"
urlSplited = url.split('/')[-1];
print ("Downloading : "+urlSplited)
urllib.request.urlretrieve(url, urlSplited)

To check if a connection is etablished, I believe I can do something like

while connection() is true:
   Download()

But that would do the downloading many times..

I'm working on Linux.

B.T
  • 521
  • 1
  • 7
  • 26

2 Answers2

1

I suggest you to use a combination of try, while and sleep function. Like this:

import urllib
import urllib.request
import time
url = "http://my.site.com/myFile"
urlSplited = url.split('/')[-1];
try_again = True
print ("Downloading : "+urlSplited)

while try_again:
    try:
        urllib.request.urlretrieve(url, urlSplited, timeout = 100)
        try_again = False
    except Exception as e:
        print(e)
        time.sleep(600)
0

Replace while with if, then everything under it will run only once.

if connection() == True:
   Download()

Also, connection() function could be something like this:

try:    
    urllib.urlopen(url, timeout=5)
    return True
return False
Roxerg
  • 116
  • 1
  • 13
  • Thank you for your answer. Will the `try` also work while the `urlopen` function is working ? I mean, if during the download the connection is lost ? – B.T Sep 17 '17 at 14:56
  • 1
    You specified that connection() should check whether the connection was established, so this function runs only once at the start, returns a value, and stops. If you're worried about the file downloading correctly, you should check out [this answer](https://stackoverflow.com/questions/987876/how-to-know-if-urllib-urlretrieve-succeeds) (and probably use urllib2) – Roxerg Sep 17 '17 at 15:05