def DownFile(dfile, sfile, username, password, ip, port, su, supassword, sudo, sudopassword, loginmethod, keyfile, fid, user): socket.setdefaulttimeout(3) dfile = os.path.basename(dfile) dfile = os.path.join(download_dir, dfile) model = "transfile_downfile" info = {"status": True, "content": "", "status": "running", "progres": "0"} translog = [] lasttime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) logline = { "fid": fid, "action": "download", "time": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), "ip": ip, "sfile": sfile, "dfile": dfile, "result": False, "user": user, "msg": "", "size": "0KB", "lasttime": lasttime } try: t = paramiko.Transport((ip, int(port))) if loginmethod == "key".upper(): keyfile = key_resolv.key_resolv(keyfile, cache) key = paramiko.RSAKey.from_private_key_file(keyfile) t.connect(username=username, pkey=key) else: t.connect(username=username, password=password) callback_info = functools.partial(set_progres, fid, 1, 1, False) sftp = paramiko.SFTPClient.from_transport(t) try: sftp.listdir(sfile) cheungssh_sftp(fid, ip, username, sfile, dfile, set_progres, port, loginmethod, password, keyfile) return except Exception, e: if e.errno == 2: pass else: raise IOError(e) sftp.get(sfile, "%s.%s" % (dfile, ip), callback=callback_info) log(model, True) info['status'] = 'True' logline["result"] = True cache_size_id = "fid:size:%s" % (fid) cache_size = cache.get(cache_size_id) if cache_size is None: cache_size = 0 t_size = float(cache_size) / float(1024) logline['size'] = "%0.2fKB" % t_size cache_translog = cache.get("translog") if cache_translog: cache_translog.append(logline) else: translog.append(logline) cache_translog = translog cache.set("translog", cache_translog, 3600000000)
def DownFile(dfile,sfile,username,password,ip,port,su,supassword,sudo,sudopassword,loginmethod,keyfile,fid,user): socket.setdefaulttimeout(3) dfile=os.path.basename(dfile) dfile=os.path.join(download_dir,dfile) model="transfile_downfile" info={"msgtype":"OK","content":"","status":"running","progres":"0"} translog=[] lasttime=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) logline={"fid":fid,"action":"download","time":time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()),"ip":ip,"sfile":sfile,"dfile":dfile,"result":"ERR","user":user,"msg":"","size":"0KB","lasttime":lasttime} try: t=paramiko.Transport((ip,int(port))) if loginmethod=="key".upper(): keyfile=key_resolv.key_resolv(keyfile,cache) key=paramiko.RSAKey.from_private_key_file(keyfile) t.connect(username = username,pkey=key) else: print '密码认证',username,password t.connect(username = username,password = password) callback_info = functools.partial(set_progres,fid,1,1,False) sftp = paramiko.SFTPClient.from_transport(t) try: sftp.listdir(sfile) print '是一个目录,开始下载目录' cheungssh_sftp(fid,ip,username,sfile,dfile,set_progres,port,loginmethod,password,keyfile) print '目录下载完成' return except Exception,e: if e.errno==2: pass else: raise IOError(e) sftp.get(sfile,dfile,callback=callback_info) log(model,"OK") info['status']='OK' logline["result"]="OK" cache_size_id="fid:size:%s" %(fid) cache_size=cache.get(cache_size_id) if cache_size is None: cache_size=0 t_size=float(cache_size)/float(1024) logline['size']="%0.2fKB" %t_size cache_translog=cache.get("translog") if cache_translog: print 888888888 cache_translog.append(logline) else: print 9999999999 translog.append(logline) cache_translog=translog cache.set("translog",cache_translog,3600000000)