Python Script to Connect to Sftp and Upload Files

In this tutorial you will understand the process of uploading files to an SFTP server using the "Pysftp Library" in a Python-based client-side script. This is the 3rd article in our series of tutorials on how to communicate with the SFTP server in Python using the pysftp library. In the previous article, nosotros taught you nigh how to establish a connection and download files from our targeted SFTP server, "test.rebex.net".

To start with, establish a connection with the SFTP server equally follows:

          import pysftp host = 'test.rebex.net' port = 22 username = 'demo' countersign= 'countersign' try:   conn = pysftp.Connection(host=host,port=port,username=username, password=password)   print("connection established successfully") except:   impress('failed to establish connection to targeted server')        

Requirements To Uploading Files To SFTP Server

Now, endeavor to upload the file to the SFTP server. Afterwards all, this tutorial is about uploading files to SFTP server. To practice that, you must accept a valid path of the file that you desire to upload, a valid path of directory on the server, and permission to modify content on the SFTP server. (Recall, whenever you are uploading a file to a server, y'all are making changes in its directory, then you must have permission to make those changes.)

pysftp.Connection.put() method

Let's try to upload a simple "tmp.txt" file to our targeted server, 'test.rebex.internet'. Our 'tmp.txt' file resides in the same directory of our Python script, then we will be passing the filename only. In your instance, however, if your targeted file is in some other directory, so you lot must pass the complete path of that file.

          # import required library import pysftp # credentials of targeted sftp server host = 'exam.rebex.net' port = 22 username = 'demo' password= 'countersign' try:   conn = pysftp.Connection(host=host,port=port,username=username, password=password)   impress("connection established successfully") except:   impress('failed to found connection to targeted server') # file path of local file and targeted location local_file='tmp.txt' target_location='/pub/example' # call conn.put() method to upload file to server conn.put(local_file, target_location)        

In our code snippet shown higher up, we are using put() method of connection object to upload files. For this, nosotros take to pass two cord parameters. The starting time parameter determines the path of the target file that we want to upload, which, in in our case, is 'tmp.txt'. The second parameter tells you near the path of the directory on the server where we want to upload target file, 'tmp.txt'.

At present, permit's run the code and encounter what happens. As you will see, when you run the above code, information technology throws upward some errors every bit shown beneath:

Information technology is a permission fault. It means that we don't have permission to upload the file on the targeted server. It means the user account we are using lacks permission to upload files on the SFTP server, "test.rebex.net". To solve this result, either nosotros get permission by contacting the support squad of the server, or we can try another server. For this tutorial though, we will try another SFTP server running or our local machine.

So to check and upload the file to the SFTP server, lets use the local SFTP server running on a local machine. To larn how to create a local server on your motorcar please click here.

Let's endeavor to upload the file now to the local SFTP server.

                      import pysftp # credentials of targeted sftp server host = '127.0.0.1' port = 22 username = 'myuser' password= 'mypassword' try:   conn = pysftp.Connection(host=host,port=port,username=username, password=password)   impress("connexion established successfully") except:   impress('failed to establish connexion to targeted server') # file path of local file and targeted location local_file='tmp.txt' target_location='/' # telephone call conn.put() method to upload file to server conn.put(local_file, target_location)                  

If you run the to a higher place lawmaking, it results in the uploading of the file, "tmp.txt" to the root directory of the local SFTP server.

How To Upload Multiple Files To SFTP Server

There may come a time when you may have to upload multiple files to an SFTP server. For the purposes of this example, let's effort to upload all text files with ".txt" extension available in the local directory.

We will use "with conn.cd('targeted directory')" to attain generator, and we will pass each .txt file to information technology, and then call conn.put() method to upload each unmarried file in the following manner:

          import os files=os.listdir() with conn.cd('/testing/'):     for file in files:         if file[-iv:]=='.txt':             try:                 conn.put(file)                 print('uploaded file successfully: ',file)             except:                 print('failed to upload file: ',file) uploaded file successfully: tmp.txt        

Summary

In this tutorial, you've learned about uploading files to an SFTP server (single/multiple) using the pysftp library in Python. You may have realized that in order to do and so, you lot require a proper path of the local file, a targeted directory on the server and valid "write" permissions on the server for the given customer. In the adjacent tutorial, we will teach you how to delete files on an SFTP server using the pysfpt library in Python.


References

  • TechRepublic
  • PythonForBeginners
  • pysftp
  • pysftp

mcnultyexciation.blogspot.com

Source: https://www.datacourses.com/how-to-upload-files-to-sftp-server-in-python-2218/

0 Response to "Python Script to Connect to Sftp and Upload Files"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel