Ruslan's Blog Ruslan's Blog
Search
SFTP server with Python and Paramiko in under a minute

I needed a simple server that could be used as a stub for testing Python SFTP clients so I whipped out one over the weekend.
It’s a simple single-threaded server so it’s not for production use but give it a try anyways, you might find it useful too.

Installation:

$ [sudo] pip install sftpserver

Running the server:


    $ sftpserver
    Usage: sftpserver [options]
    -k/--keyfile should be specified

    Options:
      -h, --help            show this help message and exit
      --host=HOST           listen on HOST [default: localhost]
      -p PORT, --port=PORT  listen on PORT [default: 3373]
      -l LEVEL, --level=LEVEL
                            Debug level: WARNING, INFO, DEBUG [default: INFO]
      -k FILE, --keyfile=FILE
                            Path to private key, for example /tmp/test_rsa.key

    $ sftpserver -k /tmp/test_rsa.key -l DEBUG

Connecting with a Python client to our server:

>>> import paramiko
>>> pkey = paramiko.RSAKey.from_private_key_file('/tmp/test_rsa.key')
>>> transport = paramiko.Transport(('localhost', 3373))
>>> transport.connect(username='admin', password='admin', pkey=pkey)
>>> sftp = paramiko.SFTPClient.from_transport(transport)
>>> sftp.listdir('.')
['loop.py', 'stub_sftp.py']

The server code itself is pretty minimal, check it out on GitHub.

arrow5 Responses

  1. 34 mos, 3 wks ago

    Nice work! How about making it use PyFilesystem? It could be useful for testing to be able to use an in-memory filesystem.

    [1] http://code.google.com/p/pyfilesystem/

  2. Ruslan Spivak
    34 mos, 3 wks ago

    Hi Andrei,

    Thanks for pointing out PyFilesystem. I’ll look into it.

  3. 34 mos, 3 wks ago

    The ‘fsserve’ command in PyFilesystem can serve a directory over sftp.

    For example, to serve the current working directory:

    $ sudo fsserve -t sftp

    And this to get a directory listing:

    $ fsls sftp://127.0.0.1

    You might want to join the PyFilesystem mailing list!

  4. Ruslan Spivak
    34 mos, 3 wks ago

    I didn’t know that, really neat! Thanks for the information, Will.

  5. horizon
    13 mos, 4 wks ago

    why not use paramiko common class ,such as from sftp_server import SFTPServer

Leave A Comment