pysftp is a Python module that provides a high-level interface for interacting with SFTP servers. It simplifies file transfer operations, directory management, and remote filesystem manipulation, making it easier to perform common SFTP tasks.
Key Features of pysftp:
- Simplified file transfers: Upload, download, rename, and delete files using simpler functions and methods.
- Directory management: Create, navigate, and remove directories on the remote SFTP server.
- Error handling: Convenient error handling mechanisms to deal with SFTP-related errors.
Installing pysftp:
To install pysftp, you can use the pip command:
pip install pysftpExample Usage:
Here’s an example of using pysftp to upload a file to an SFTP server:
import pysftp
# Establish an SFTP connection
with pysftp.Connection('sftp.example.com', username='username', password='password') as sftp:
# Upload a local file to the remote server
sftp.put('local_file.txt', 'remote_file.txt')
This code snippet first imports the pysftp module and then creates a connection to the SFTP server using the Connection class. It provides the server address, username, and password as parameters. Inside the with statement, it uploads the local file local_file.txt to the remote server as remote_file.txt. The connection is automatically closed upon exiting the with block.
Additional Capabilities:
- Recursive file transfers: Upload or download entire directory trees recursively.
- Progress monitoring: Track the progress of file transfers using callback functions.
- Custom file transfer modes: Perform binary or ASCII transfers depending on file type.
- Server information retrieval: Retrieve information about the SFTP server, such as system type and server version.
- Remote filesystem manipulation: Create, remove, and rename directories on the remote SFTP server.
pysftp is a valuable tool for automating file transfers, managing remote files, and interacting with SFTP servers in Python applications. Its simplified interface and error handling mechanisms make it suitable for both beginners and experienced Python developers.
Overview
This Python script uses the pysftp library to establish an SFTP (Secure File Transfer Protocol) connection to a remote server, retrieves a list of files and directories in the root directory, and prints their names and attributes.
Importing Libraries
import pysftp
import getpassHere, the script imports the necessary libraries:
pysftpfor SFTP functionality.getpassto securely input the password without displaying it on the screen.
Constants
HOSTNAME = 'localhost'
PORT = 22Defines constants for the default values of the SFTP server’s hostname and port.
Function: sftp_getfiles
def sftp_getfiles(username, password, hostname=HOSTNAME, port=PORT):
with pysftp.Connection(host=hostname, username=username, password=password) as sftp:
print("Connection successfully established with server... ")
sftp.cwd('/')
list_directory = sftp.listdir_attr()
for directory in list_directory:
print(directory.filename, directory)- Parameters:
username: SFTP username.password: SFTP password.hostname(optional): SFTP server hostname (default is ‘localhost’).port(optional): SFTP server port (default is 22).- Functionality:
- Establishes an SFTP connection to the server using the provided credentials.
- Prints a success message if the connection is established.
- Changes the working directory to the root directory (‘/’).
- Retrieves a list of file and directory attributes in the root directory.
- Prints the name and attributes of each file and directory.
Main Block
if __name__ == '__main__':
hostname = input("Enter the target hostname: ")
port = input("Enter the target port: ")
username = input("Enter your username: ")
password = getpass.getpass(prompt="Enter your password: ")
sftp_getfiles(username, password, hostname, port)- Execution:
- If the script is executed as the main program:
- Takes user input for the target hostname, port, username, and password.
- Calls the
sftp_getfilesfunction with the provided input values.
Input Gathering
- The script prompts the user to enter the target hostname, port, username, and password interactively using the
inputfunction. - The
getpass.getpassfunction is used to securely input the password without displaying it on the screen.
import pysftp
import getpass
HOSTNAME = 'localhost'
PORT = 22
def sftp_getfiles(username, password, hostname=HOSTNAME,port=PORT):
with pysftp.Connection(host=hostname, username=username, password=password) as sftp:
print("Connection successfully established with server... ")
sftp.cwd('/')
list_directory = sftp.listdir_attr()
for directory in list_directory:
print(directory.filename, directory)
if __name__ == '__main__':
hostname = input("Enter the target hostname: ")
port = input("Enter the target port: ")
username = input("Enter your username: ")
password = getpass.getpass(prompt="Enter your password: ")
sftp_getfiles(username, password, hostname, port)Note
- This script is a simple example and may need error handling for potential exceptions that could occur during the SFTP connection or file listing process.