Download the file from the server

paramiko Import 
Import os 
Import datetime 
from STAT Import S_ISDIR AS isdir 


DEF down_from_remote (sftp_obj, remote_dir_name, local_dir_name): 
    "" "Remote file download" "" 
    remote_file = sftp_obj.stat (remote_dir_name) 
    IF isdir (remote_file.st_mode): 
        # Folder can not be directly downloaded, needs to continue circulation 
        check_local_dir (local_dir_name) 
        Print ( 'to start the download folder:' + remote_dir_name) 
        for remote_file_name in sftp.listdir (remote_dir_name): 
            sub_remote = os.path.join (remote_dir_name, remote_file_name) 
            sub_remote = sub_remote. Replace ( '\\', '/')  
            sub_local = the os.path.join (local_dir_name, remote_file_name)
            sub_local = sub_local.replace ( '\\', '/') 
            down_from_remote (sftp_obj, sub_remote, sub_local) 
    the else: 
        # file downloaded directly 
        print ( 'start downloading the file:' + remote_dir_name) 
        sftp.get (remote_dir_name, local_dir_name) 


DEF check_local_dir (local_dir_name): 
    "" "local folder exists, does not exist, create" "" 
    iF not os.path.exists (local_dir_name): 
        os.makdirs (local_dir_name) 


iF __name__ == "__main__": 
    "" "program main entrance. "" " 
    # server connection information 
    host_name = 'your port' 
    user_name = 'your username' 
    password = 'your password'
    22 is = Port 
    # remote file path (absolute path required) 
    remote_dir = '/ Home / Zhengquan / tumaoOffice / wpsweb-Demo-NodeJS / the dir'
    # Local file storage path (absolute or relative path can) 
    local_dir =' D:ServerFile \\ \\ '+ STR (datetime.date.today ()) 


    # connect to a remote server
    paramiko.Transport = T ((host_name, Port)) 
    t.connect (USER_NAME = username, password = password) 
    SFTP = paramiko.SFTPClient.from_transport (T) 

    # remote file starts downloading 
    down_from_remote (SFTP, remote_dir, local_dir) 

    # close the connection 
    t.close ()

  

Guess you like

Origin www.cnblogs.com/taozhengquan/p/12464539.html