Subject: New Python bindings for libssh2

New Python bindings for libssh2

From: Pan via libssh2-devel <>
Date: Sat, 29 Jul 2017 18:29:10 +0100
Hello list,
New member on the list, first off hello and thank you for the awesome library, it has been a real pleasure using it. Website also very useful, good documentation and examples.
I wanted to write to let you know of new Python bindings for libssh2 - ssh2-python.
Here's a short example, also attached a script,
To install run the below in a shell, needs python and libssh2 headers:
pip install ssh2-python
from __future__ import print_function

import os
import socket

from ssh2 import Session

host = b'localhost'
user = os.getlogin()

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, 22))

session = Session()

channel = session.open_session()
channel.execute(b'echo me; exit 2')
size, data =
while size > 0:
    size, data =
print("Exit status: %s" % channel.get_exit_status())
Most of the API has been implemented. Currently not implemented are:
* libssh2_publickey_* methods
* SFTP VFS methods
* Most of SFTPHandle methods except read and close
* SFTP attributes
It is purposefully a thin wrapper over libssh2 and directly maps its API. The examples from the libssh2 site can be ported over to python with minimal changes.
The python extensions are written in Cython and are also available to use from Cython via its cimport.
Now, as to motivation, you are probably aware that the only other available Python bindings for libssh2, pylibssh2, have not been maintained for years. The last release of that package was in 2011.
Have been looking for alternatives for a while now for use in an asynchronous parallel SSH client I have written. Pylibssh2 kind of worked after some patches were applied for async support among other things but it still needed a lot more work to be viable. It completely lacks SFTP support for one, and only has basic agent implementation. I also found it leaks memory in long term use.
So due to lack of any other options as well as, judging by the number of issues, forks and PRs on the pylibssh2 github page, interest from others on exactly the same thing, I started from scratch to map the entire libssh2 API in python, via Cython extensions.
Most the API is implemented, barring exceptions above, as direct mapping of the libssh2 API with some python semantics, like SFTPHandles can be iterated on to read them and exceptions raised for common errors.
The objects will also safely and automatically clear their C allocations, in correct order.
Pylibssh2 did not seem to do any de-allocation, hence the memory leakage. It also seems to be quite a bit faster on reading large buffers from a channel - pylibssh2 had some inefficiencies in its handling there.
Any feedback and/or reviews would be greatly appreciated.
Still quite a bit to do especially documentation and binary packages, other than implementing the missing parts, but the library is usable right now. Also have tests in the client library I mentioned which are passing, having originally used pylibssh2.
I would be very happy for this to be useful to others as well. Unfortunately the choice of SSH libraries in Python is severely limited and those that do exist, especially the current de-facto standard, paramiko, are quite lacking in performance and stability while also lacking in maintainers willing to review/merge changes and fix reported issues.
One question though, is LIBSSH2_CHANNEL_WINDOW_DEFAULT appropriate to use as an SFTP handle read default buffer length? Have noticed the examples on the website use a much larger value but my testing did not show a discernible performance benefit to anything larger than LIBSSH2_CHANNEL_WINDOW_DEFAULT.


Received on 2017-07-29