sftpserver (rspivak)

0.3 · maintenance · verified Thu Apr 16

sftpserver is a simple, single-threaded SFTP server built upon Paramiko's SFTPServer, primarily designed for testing Python SFTP clients rather than production environments. The current version is 0.3, with its last release in April 2017, indicating a maintenance-only status with no active development or new features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to start the sftpserver programmatically, generating a temporary RSA key if one doesn't exist. It will listen on 127.0.0.1:3373 by default. Connect with a client using any username/password (e.g., 'admin'/'admin') as the default handler accepts all credentials.

import os
import paramiko
import sftpserver

# Generate a test private key if it doesn't exist
key_file = 'test_rsa.key'
if not os.path.exists(key_file):
    print(f"Generating new RSA key for the server: {key_file}")
    key = paramiko.RSAKey.generate(2048)
    key.write_private_key_file(key_file)
    print("Key generated. DO NOT use this key for production.")

# Define SFTP server parameters
SFTP_HOST = os.environ.get('SFTP_HOST', '127.0.0.1')
SFTP_PORT = int(os.environ.get('SFTP_PORT', '3373'))
SFTP_KEY_FILE = os.environ.get('SFTP_KEY_FILE', key_file)
SFTP_LOG_LEVEL = os.environ.get('SFTP_LOG_LEVEL', 'INFO')

print(f"Starting SFTP server on {SFTP_HOST}:{SFTP_PORT}")
print(f"Using key file: {SFTP_KEY_FILE}")
print(f"Log level: {SFTP_LOG_LEVEL}")
print("Connect with username 'admin', password 'admin' (or any credential if no auth callback is set).")
print("Press Ctrl+C to stop the server.")

try:
    sftpserver.start_server(SFTP_HOST, SFTP_PORT, SFTP_KEY_FILE, SFTP_LOG_LEVEL)
except KeyboardInterrupt:
    print("\nSFTP server stopped.")

view raw JSON →