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 third article in our series of tutorials on how to communicate with the SFTP server in Python using the pysftp library. In the previous article, we taught you about 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 as follows:
import pysftp
host = 'test.rebex.net'
port = 22
username = 'demo'
password= 'password'
try:
conn = pysftp.Connection(host=host,port=port,username=username, password=password)
print("connection established successfully")
except:
print('failed to establish connection to targeted server')
Requirements To Uploading Files To SFTP Server
Now, try to upload the file to the SFTP server. After all, this tutorial is about uploading files to SFTP server. To do that, you must have a valid path of the file that you want to upload, a valid path of directory on the server, and permission to modify content on the SFTP server. (Remember, whenever you are uploading a file to a server, you are making changes in its directory, so 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.net’. Our ‘tmp.txt’ file resides in the same directory of our Python script, so we will be passing the filename only. In your case, however, if your targeted file is in another directory, then you must pass the complete path of that file.
# import required library
import pysftp
# credentials of targeted sftp server
host = 'test.rebex.net'
port = 22
username = 'demo'
password= 'password'
try:
conn = pysftp.Connection(host=host,port=port,username=username, password=password)
print("connection established successfully")
except:
print('failed to establish 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 above, we are using put() method of connection object to upload files. For this, we have to pass two string parameters. The first 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 about the path of the directory on the server where we want to upload target file, ‘tmp.txt’.
Now, let’s run the code and see what happens. As you will see, when you run the above code, it throws up some errors as shown below:
It is a permission error. 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 issue, either we get permission by contacting the support team 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 learn how to create a local server on your machine please click here.
Let’s try 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)
print("connection established successfully")
except:
print('failed to establish connection to targeted server')
# file path of local file and targeted location
local_file='tmp.txt'
target_location='/'
# call conn.put() method to upload file to server
conn.put(local_file, target_location)
If you run the above code, 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 try to upload all text files with “.txt” extension available in the local directory.
We will use “with conn.cd(‘targeted directory’)” to achieve generator, and we will pass each .txt file to it, and then call conn.put() method to upload each single file in the following manner:
import os
files=os.listdir()
with conn.cd('/testing/'):
for file in files:
if file[-4:]=='.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 so, you require a proper path of the local file, a targeted directory on the server and valid “write” permissions on the server for the given client. In the next tutorial, we will teach you how to delete files on an SFTP server using the pysfpt library in Python.
References
Recommended Books:
- Mastering Python for Networking and Security
- Effective Python: 90 Specific Ways to Write Better Python
The books above are affiliate links which benefit this site.