sftpserver (rspivak)
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
-
sftpserver: error: -k/--keyfile should be specified
cause The server was started without providing a path to a private SSH key file.fixRun the server with the `-k` or `--keyfile` option followed by the path to a private key, e.g., `sftpserver -k my_server_key.key` or `sftpserver.start_server(..., keyfile='/path/to/key')`. -
The authenticity of host '[localhost]:3373' can't be established. RSA key fingerprint is ... Are you sure you want to continue connecting (yes/no)?
cause This is a standard SSH client prompt when connecting to a server for the first time, or if the server's host key has changed.fixFor a trusted local test server, type `yes` and press Enter. For automated testing, configure the client to automatically add host keys or disable strict host key checking (e.g., `ssh -o StrictHostKeyChecking=no ...` or `client.set_missing_host_key_policy(paramiko.AutoAddPolicy())`). -
paramiko.ssh_exception.SSHException: No authentication methods available
cause The client attempted to connect but no valid credentials (username/password or key) were provided or accepted by the server. The default `sftpserver` accepts any credentials, so this usually indicates a client-side misconfiguration.fixEnsure your client provides a username and password (e.g., 'admin'/'admin') or a private key that matches the server's configuration. Verify the client's `paramiko.Transport.connect()` call includes correct `username` and `password` or `pkey` arguments. -
ssh_exchange_identification: Connection closed by remote host
cause The SFTP server process either crashed, was not running, or immediately rejected the connection attempt at a very early stage (e.g., due to port or firewall issues).fixVerify the `sftpserver` process is running. Check for any errors in the server's console output. Ensure no firewall is blocking the specified port (default 3373) on the server machine. Double-check the host and port in your client configuration.
Warnings
- breaking The library is explicitly stated as 'simple single-threaded' and 'not for production use'. Relying on it for high-traffic or critical production environments will lead to performance bottlenecks and instability.
- gotcha The server requires a private key file (`-k/--keyfile`) to operate, which must be specified either via command-line arguments or when calling `start_server`. Failure to provide one will result in an error.
- deprecated Given the last update in 2017, `sftpserver` likely uses older versions of `paramiko` and may support only outdated SSH/SFTP ciphers and algorithms, which are considered insecure by modern standards.
Install
-
pip install sftpserver
Imports
- start_server
import sftpserver sftpserver.start_server(...)
Quickstart
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.")