python 远程链接、搜索与下载

# -*- coding: utf-8 -*-
import paramiko
import os
import sys
import stat
import utils
TOLLCOST_STORE_HOST = ''
TOLLCOST_STORE_USER = ''
TOLLCOST_STORE_PASSWORD = ''
TOLLCOST_STORE_SSH_PORT = 22
TOLLCOST_BASE_DIRECTORY = '/home/autonavi/'
TOLLCOST_DIRECTORY_PATTERN = ''
#TOLLCOST_SAVE_PATH = 'd:/c.rar'
TOLLCOST_SAVE_PATH = '/tmp/CHARGEINFO.rar'
TOLLCOST_PATH = TOLLCOST_SAVE_PATH.replace(".rar",'')

def tollcost(strore_data_version='17Q2_A5_20170630'):
try:
t = paramiko.Transport((TOLLCOST_STORE_HOST, 22))
t.connect(username=TOLLCOST_STORE_USER, password=TOLLCOST_STORE_PASSWORD)
sftp = paramiko.SFTPClient.from_transport(t) # use the style of t connection remote server
#
print 'success connect ' # success
#
tollcost_path = find_tollbase_path(sftp,strore_data_version)
download_tollcost(sftp,tollcost_path)
unrar_tollcost()
#
sftp.close()
t.close()
except Exception, e:
sftp.close()
t.close()
print e
print e.message

def download_tollcost(sftp,tollcost_path):
sftp.get(tollcost_path,TOLLCOST_SAVE_PATH)
return True
def unrar_tollcost():
# local_tollcost_path = os.path.join(TOLLCOST_SAVE_PATH,tollcost_filename)
if not os.path.exists(TOLLCOST_SAVE_PATH):
print "Error: toll cost file %s is not exist. " % TOLLCOST_SAVE_PATH
return False
cmd_obj = utils.CMDsexecute()
base_dir = os.path.basename(TOLLCOST_SAVE_PATH)
cmd_obj.add_cmd('cd %s ' % base_dir)
cmd_obj.add_cmd('rar x ' + TOLLCOST_SAVE_PATH )
cmd_obj.add_cmd('rm ' + TOLLCOST_SAVE_PATH)
cmd_obj.add_cmd('ls -al')
#
cmd_obj.run_cmds()
return True


def find_tollbase_path(sftp,dataversion):
path = os.path.join(TOLLCOST_BASE_DIRECTORY,dataversion)
all_filepath = search_allfiles(sftp, path)
print 'all files in %s num is %s' % (path, len(all_filepath))
for a_f in all_filepath:
if a_f.find('CHARGEINFO.rar') != -1:
print 'get CHARGEINFO.rar in ', a_f
return a_f
return None
'''
in remote data store machine get the tollcost file
'''
def search_tollcost_path(sftp,path):

return None
'''
get the filename path of Specified filename
'''
def search_filename_path(sftp,path,filename):
all_filepath = search_allfiles(sftp, path)
print 'all files in %s num is %s' % (path, len(all_filepath))
for a_f in all_filepath:
if a_f.find(filename) != -1:
print 'get %s in ' % filename, a_f
return a_f
print 'Failed: have not find %s in directory %s ' % (filename,path)
return None

'''
through sftp find all files in path

'''
def search_allfiles(sftp,path):
filenamelist = []
mode = sftp.stat(path).st_mode
if stat.S_ISDIR(mode):
# is directory
cur_filelist = sftp.listdir(path)
for f in cur_filelist:
filenamelist += search_allfiles(sftp,path+"/"+f)
else:
filenamelist.append(path)
return filenamelist

'''
Test
'''
# def method1():
# try:
# ssh = paramiko.SSHClient()
# ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# ssh.connect(hostname=TOLLCOST_STORE_HOST, username=TOLLCOST_STORE_USER, password=TOLLCOST_STORE_USER)
# # stdin, stdout, stderr = ssh.exec_command('ls')
# #
# # print stdout.readlines()
# print 'success connect '
# except Exception, e:
# print e.message ##this method failed

def method2():
try:
t = paramiko.Transport((TOLLCOST_STORE_HOST, 22))
t.connect(username=TOLLCOST_STORE_USER, password=TOLLCOST_STORE_PASSWORD)
sftp = paramiko.SFTPClient.from_transport(t) # use the style of t connection remote server
#
print sftp.listdir(TOLLCOST_BASE_DIRECTORY)
print 'success connect ' # success
#
find_tollbase_path(sftp,'17Q2_A5_20170630')

#
sftp.close()
t.close()
except Exception, e:
print e
print e.message

if __name__ == '__main__':
# method2()
tollcost()
posted @ 2017-08-21 14:39  百变小超  阅读(316)  评论(0编辑  收藏  举报