Ansichten: QuotePaste - CodePaste - NoPaste
Codesnippet eingetragen am 26.1.2016 um 22:51
Von: Michael
Sprache: Python
Beschreibung: Download file, retry 3 times if neccessary. It is also possible to add headers in this version
CodeSnippet:
  1. import urllib2
  2.  
  3. attempts = 0
  4.  
  5. while attempts < 3:
  6. try:
  7. response = urllib2.urlopen("http://example.com", timeout = 5)
  8. content = response.read()
  9. f = open( "local/index.html", 'w' )
  10. f.write( content )
  11. f.close()
  12. break
  13. except urllib2.URLError as e:
  14. attempts += 1
  15. print type(e)