Python equivalent of a given wget command

前端 未结 10 2192
面向向阳花
面向向阳花 2020-12-02 10:04

I\'m trying to create a Python function that does the same thing as this wget command:

wget -c --read-timeout=5 --tries=0 \"$URL\"

-c

10条回答
  •  渐次进展
    2020-12-02 10:59

    import urllib2
    import time
    
    max_attempts = 80
    attempts = 0
    sleeptime = 10 #in seconds, no reason to continuously try if network is down
    
    #while true: #Possibly Dangerous
    while attempts < max_attempts:
        time.sleep(sleeptime)
        try:
            response = urllib2.urlopen("http://example.com", timeout = 5)
            content = response.read()
            f = open( "local/index.html", 'w' )
            f.write( content )
            f.close()
            break
        except urllib2.URLError as e:
            attempts += 1
            print type(e)
    

提交回复
热议问题