Upload file via sftp with python

前端 未结 2 1339
误落风尘
误落风尘 2020-12-14 10:26

I wrote a simple code to upload a file to a sftp server in python. I am using python 2.7

import pysftp

srv = pysftp.Connection(host=\"www.destination.com\",         


        
相关标签:
2条回答
  • 2020-12-14 10:55

    I found the answer to my own question.

    import pysftp
    
    srv = pysftp.Connection(host="www.destination.com", username="root",
    password="password",log="./temp/pysftp.log")
    
    with srv.cd('public'): #chdir to public
        srv.put('C:\Users\XXX\Dropbox\test.txt') #upload file to nodejs/
    
    # Closes the connection
    srv.close()
    

    Put the srv.put inside with srv.cd

    0 讨论(0)
  • 2020-12-14 11:14
    import pysftp
    
    with pysftp.Connection(host="www.destination.com", username="root",
    password="password",log="./temp/pysftp.log") as sftp:
    
      srv.cwd('/root/public'): #Write the whole path
      srv.put('C:\Users\XXX\Dropbox\test.txt') #upload file to nodejs/
    

    No srv.close() as connection closed automatically at the end of the with-block

    I did a minor change with cd to cwd

    Syntax -

    # sftp.put('/my/local/filename')  # upload file to public/ on remote
    # sftp.get('remote_file')         # get a remote file
    
    0 讨论(0)
提交回复
热议问题