www.libssh2.org | Daily snapshots | Mailing list archive | Docs | Examples | github

Archive Index This month's Index

Subject: Re: New Python bindings for libssh2

Re: New Python bindings for libssh2

From: D Smith via libssh2-devel <libssh2-devel_at_cool.haxx.se>
Date: Fri, 01 Sep 2017 17:30:03 +0100
Hello again,
Quick follow up on this - most of the API has now been implemented with OpenSSH server integration tests and working well.
The Github releases page has system packages for the major distributions using shared system library.
There are also Python binary wheel packages for each release for Linux, OSX and Windows, all Python versions, with latest version of libssh2 included. Windows uses the WinCNG backend, latest OpenSSL included in the wheels for the others.
pip install ssh2-python
will install a binary package on recent versions of pip.
Thanks again for the great library.
29.07.2017, 18:34, "Pan via libssh2-devel" <libssh2-devel@cool.haxx.se>:
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, example_echo.py:
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 = channel.read()
while size > 0:
    size, data = channel.read()
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.

libssh2-devel https://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel

libssh2-devel https://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Received on 2017-09-01

the libssh2 team