import urllib2,os,md5,shutil
def downloadfile(url,filename):
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 \
(KHTML, like Gecko) Chrome/51.0. 2704.103 Safari/537.36"}
request = urllib2.Request(url,headers = headers)
try:
response = urllib2.urlopen(request)
except:
print('ERROR:Cannot download '+url)
return False
else:
createDir(filename)
with open(filename,'wb') as f:
f.write(response.read())
return True
def calMD5(filename):
with open(filename,'rb') as file:
text=file.read()
md=md5.new()
md.update(text)
return md.hexdigest()
def submd5(file,rootdir,predir):
list=os.listdir(rootdir)
for index in range(0,len(list)):
path=os.path.join(rootdir,list[index])
curdir=list[index]
if predir:
curdir=os.path.join(predir,list[index])
if os.path.isfile(path):
file.write("%s %s\n" %(curdir,calMD5(path)))
else:
submd5(file,path,curdir)
def createMD5(rootdir,filename):
with open(filename,'w') as file:
submd5(file,rootdir,None)