python在互联互通中下载应用一例

    在实际工作碰到一个需求,每天需要在大量的服务器上下载更新文件,并且文件都是几个G的大文件,由于服务器数量比较多,且分布在大江南北的联通电信,如果电信的服务器去下载联通源的文件,会很慢,反之亦然,最好能有一个脚本,自动判断到电信的服务器下载服务器快,还是到联通的下载服务器快。

    思路是这样的,在电信联通的下载服务器分别放置一个20M大小的测试文件,正式下载之前,分别测试下2个文件的下载所用时间,然后决定使用哪个下载源。

    代码如下:

 

 
 
  1. #/usr/bin/env python 
  2. # big file download mod 
  3.  
  4. import os 
  5. import sys 
  6. import string 
  7. import commands 
  8. import time 
  9.  
  10.  
  11.  
  12. def getfile(filename,url): 
  13.     a=filename 
  14.     b=url 
  15.     dlcmd="wget "+b+a+" -O /data/"+a+" -c " 
  16.     dlcmdmd5="wget "+b+a+".md5 -O /data/"+a+".md5 -c " 
  17.     os.popen(dlcmd) 
  18.     os.popen(dlcmdmd5) 
  19.     print "**********************************************" 
  20.     return 0 
  21.     #end 
  22. def urlTest(url): 
  23.     urlTestCmd="wget -c "+url+"ratetest -O /data/ratetest" 
  24.     rmTestFile="rm -f /data/ratetest*" 
  25.     a=time.time() 
  26.     os.popen(urlTestCmd) 
  27.     os.popen(rmTestFile) 
  28.     b=time.time() 
  29.     c=b-a 
  30.     return c 
  31.     #end 
  32. def geturl(url1,url2): 
  33.     c1=urlTest(url1) 
  34.     c2=urlTest(url2) 
  35.     if ( c2 < c1): 
  36.         return url2 
  37.     else
  38.         return url1 
  39.     #end 
  40. def checkFile(filename,url1,url2): 
  41.     a=filename 
  42.     checkmd5="md5sum -c /data/"+a+".md5" 
  43.     if os.path.isfile("/data/"+a): 
  44.         colpt.ptgreen("vm images "+a+" exists ok!"
  45.         return 0 
  46.     else
  47.         print a+" not exists ,Will download " 
  48.         filegeturl=geturl(url1,url2) 
  49.         b=getfile(a,filegeturl) 
  50.         if (b != 0): 
  51.             print "Download errors Please check !" 
  52.             return 11 
  53.         j=1 
  54.         statusCheckMd5, outputCheckMd5 = commands.getstatusoutput(checkmd5) 
  55.         print str(statusCheckMd5)+"  md5 status is!" 
  56.         print "outputCheckMd5 is "+outputCheckMd5 
  57.         if (string.find(outputCheckMd5,"OK")>0): 
  58.             statusCheckMd5=0 
  59.         else
  60.             statusCheckMd5=1 
  61.         while (statusCheckMd5 != 0): 
  62.             os.system("rm /data/"+a+" -f"
  63.             os.system("rm /data/"+a+"md5 -f"
  64.             getTemp(a) 
  65.             statusCheckMd5, outputCheckMd5 = commands.getstatusoutput(checkmd5) 
  66.             j=j+1 
  67.             print str(statusCheckMd5)+"  md5 status is!" 
  68.             if (j > 6): 
  69.                 print "images sync filed" 
  70.                 return 12 
  71.         print "file downlaod  ok!" 
  72.         return 0 
  73. #end 
  74.  

将以上代码作为模块使用,直接调用模块参数就可以:

 

 
 
  1. url1=ftp://2.2.2.2/  #电信区下载服务器 
  2. url1=ftp://1.1.1.1/  #联通区下载服务器 
  3. filename="bigfile"   #需要下载的大文件 
  4. bigfileDownload.checkFile(filename,url1,url2) 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值