title: Python download videos from HTTP URL with bad internet
body: <p>I have problem with downloading videos from my server e.g. <a href="http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Cornaro%20USLUGE.mp4" rel="nofollow noreferrer">http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Cornaro%20USLUGE.mp4</a></p>
  
  <p>All works perfectly when internet is OK, but when I disconnect LAN cable from Raspberry Pi and stay like that less than 10-15 seconds. But when internet is off more than 10-15 seconds, my download does not continue or videos are not properly downloaded (I merge them later with MP4Box and they need to be). If someone has suggestion how to solve this problem and help me I would appreciate it very much.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import os
  import urllib
  import urllib2
  import time
  import commands
  import requests
  import shutil
  from urllib2 import URLError
  
  urls = ['http://screensfiles.dbtouch.com/screens2/Companies/89/HD/00 APPS OVERVIEW.mp4',
  'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Cornaro USLUGE.mp4',
  'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/ILIRIJA BIOGRAD 2016.mp4',
  'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Restoran marina.mp4',
  'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/HT Screens.mp4',
  'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Hotels Touch - Tasks.mp4',
  'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Croatia Full of life.mp4',
  'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/04 PROJECTS.mp4',
  'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/05 ATTEND.mp4',
  'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Cornaro Hotel.mp4',
  'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Plurato dron snimka 2.mp4',
  'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Plurato dron snimka 2.mp4',
  'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Plurato dron snimka 2.mp4',
  'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Cornaro USLUGE.mp4',
  'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Cornaro USLUGE.mp4',
  'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Hotels Touch - Screens.mp4',
  'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Hotels Touch - Screens.mp4',
  'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Hotels Touch - Tasks.mp4',
  'http://screensfiles.dbtouch.com/screens2/Companies/89/HD/Hotels Touch - Screens.mp4']
  
  directory = "/home/pi/pythonSignage/current_playlist/videos_to_merge/"
  
  
  i=1
  
  for url in urls:
  
      i += 1
      print("current iter: ")
      print(i)
  
      if (len(urls) &gt; 1):
          url_formatted = url.split('/')[-1].replace(" ", "").replace("%20", "") + " "
      else:
          url_formatted = url.split('/')[-1].replace(" ", "").replace("%20", "")
  
      url_formatted_name = url.split('/')[-1].replace(" ", "").replace("%20", "").rstrip()
  
      while True:
  
          print("inside while true")
          try:
              """ method 0 doesn't work """
              print("try")
              response = urllib2.urlopen(url, timeout=5)
              content = response.read()
              print("content")
              f = open(directory + url_formatted_name, 'wb')
              f.write(content)
              f.close()
  
              """ method 1 doesn't work """
              #video_fancy_downloader = urllib.FancyURLopener()
              #video_fancy_downloader.retrieve(url, directory + url_formatted_name)
  
              """ method 2 - doesn't work """
              #my_file = urllib.URLopener()
              #my_file = retrieve(url, directory + url_formatted_name)
  
              """ method 3 - doesn't work """
              #response = requests.get(url, stream=True)
              #response.raise_for_status()
              #with open(directory + url_formatted_name, 'wb') as handle:
              #    for block in response.iter_content(1024):
              #        handle.write(block)
  
          except:
              print("error download, sleep 5 sec")
              time.sleep(5)
  
  
  print("end")
  </code></pre>