实例:
import urllib2
import base64,logging
def httpDownload(testModule,var):
testModule = testModule + ‘.zip‘
url = var[‘HttpRes‘] + ‘/‘ + testModule ########此url为下载路径
request = urllib2.Request(url)
downDir = ‘/home/Elves/agent/module/‘
file = downDir + testModule
base64string = base64.encodestring(‘%s:%s‘ % (var[‘HttpResUSer‘],var[‘HttpResPass‘])).replace(‘\n‘,‘‘) ######将用户名,密码编码,去掉最后的回车
request.add_header(‘Authorization‘,‘Basic %s‘ % base64string) #####将用户名密码以报文头的形式加到url
try:
result = urllib2.urlopen(request)
except Exception,e:
err = ‘requestUrl_err: %s‘%e
logging.error(err)
fp = open(file,‘wb‘)
fp.write(result.read()) ######将文件读取,写入
fp.close()
return ‘download success‘
原文:http://weilantiankong.blog.51cto.com/9469693/1789337