Mock SFTP Server for Testing

raw JSON →
1.0.1 verified Sat May 09 auth: no python

Mocksftp (v1.0.1) provides a mock SFTP server for integration testing. It runs an in-process SFTP server backed by paramiko, enabling tests to exercise SFTP clients without a real SSH server. Low activity, no release schedule.

pip install mocksftp
error ModuleNotFoundError: No module named 'mocksftp'
cause Missing installation or wrong import name.
fix
pip install mocksftp and use import mocksftp
error AttributeError: 'MockSFTPServer' object has no attribute 'server_port'
cause Server not started; the server_port is only available after entering the context.
fix
Use MockSFTPServer inside a 'with' block, and access server_port from the context manager.
gotcha MockSFTPServer uses PF_LOCAL sockets on some systems; set use_pipe=False to force TCP.
fix MockSFTPServer(use_pipe=False) when running on platforms with PF_LOCAL issues.
gotcha From python 3.12+, paramiko may fail due to deprecation of Crypto. Ensure paramiko and cryptography are updated.
fix pip install paramiko[gssapi] cryptography>=41.0
gotcha MockSFTPServer does not persist files between sessions; each context creates a fresh temp directory.
fix Use the filesystem argument to set a persistent root directory if needed.

Basic usage: start mock SFTP server, connect and transfer file.

import os
from mocksftp import MockSFTPServer
import paramiko

with MockSFTPServer() as server:
    port = server.server_port
    username = 'testuser'
    password = 'testpass'
    transport = paramiko.Transport(('localhost', port))
    transport.connect(username=username, password=password)
    sftp = paramiko.SFTPClient.from_transport(transport)
    sftp.put('local.txt', 'remote.txt')
    sftp.close()
    transport.close()