import osimport sysimport socketimport urllibimport urllib2def Schedule(nBlock, nBlkSize, nFileSize): ''''' nBlock:block num nBlkSize:block size nFileSize:remote size ''' if nFileSize != -1: per = 100.0 * nBlock * nBlkSize / nFileSize if per > 100 : per = 100 os.write(1, '\r processing percent: %.2f%%' % per) else: os.write(1, '\r processing download: %.2f MBytes' % ((nBlock * nBlkSize) / (1024 * 1024))) sys.stdout.flush() def getRemoteFileSize(url, proxy=None): """ """ opener = urllib2.build_opener() if proxy: if url.lower().startswith('https://'): opener.add_handler(urllib2.ProxyHandler({'https' : proxy})) else: opener.add_handler(urllib2.ProxyHandler({'http' : proxy})) request = urllib2.Request(url) request.get_method = lambda: 'HEAD' try: response = opener.open(request) response.read() except Exception, e: print '%s %s' % (url, e) else: return dict(response.headers).get('content-length', 0)def downloadRemoteFile(strUrl, strLocalFile): """ """ socket.setdefaulttimeout(10) print "Beg Download %s -> %s" % (strUrl, strLocalFile) urllib.urlretrieve(strUrl, strLocalFile, Schedule) print "\nEnd Download %s -> %s\n" % (strUrl, strLocalFile) if __name__ == '__main__': url1='http://www.site-digger.com/uploads/soft/130313/China_Regions.csv' url2='http://a.hiphotos.baidu.com/image/w%3D2048/sign=ca64d241e9c4b7453494b016fbc41f17/1c950a7b02087bf4cae00381f0d3572c11dfcf4e.jpg' url3='http://dldir1.qq.com/music/clntupate/QQMusic_Setup_1020.exe' url3='http://news.qq.com' downloadRemoteFile(url1, "1.csv") downloadRemoteFile(url2, "1.jpg") downloadRemoteFile(url3, "1.html")
这个帖子貌似写的比较详细,读者可以参考:
http://blog.ihipop.info/2010/10/1736.html