|
| 1 | +ssh2-python |
| 2 | +============ |
| 3 | + |
| 4 | +Super fast SSH2 protocol library. ``ssh2-python`` provides Python bindings for `libssh2`_. |
| 5 | + |
| 6 | +.. image:: https://img.shields.io/badge/License-LGPL%20v2-blue.svg |
| 7 | + :target: https://pypi.python.org/pypi/ssh2-python |
| 8 | + :alt: License |
| 9 | +.. image:: https://img.shields.io/pypi/v/ssh2-python.svg |
| 10 | + :target: https://pypi.python.org/pypi/ssh2-python |
| 11 | + :alt: Latest Version |
| 12 | + |
| 13 | +Features |
| 14 | +--------- |
| 15 | + |
| 16 | +Majority of the `libssh2`_ API has been implemented as Python native code extensions. ``ssh2-python`` is a thin wrapper of ``libssh2`` - ``libssh2`` code examples can be ported straight over to Python with only minimal changes. |
| 17 | + |
| 18 | +*Library is usable for testing purposes and at the moment available as source code only. API, module names and documentation not yet finalised. Contributions welcome.* |
| 19 | + |
| 20 | +SSH Functionality provided |
| 21 | +++++++++++++++++++++++++++++ |
| 22 | + |
| 23 | +* SSH channel operations (exec,shell,subsystem) |
| 24 | +* SSH agent |
| 25 | +* Public key authentication and management |
| 26 | +* SFTP |
| 27 | +* SCP |
| 28 | +* SSH port forwarding and tunnelling |
| 29 | +* Non-blocking mode |
| 30 | +* Listener for port forwarding |
| 31 | + |
| 32 | +And more, as per `libssh2`_ functionality. |
| 33 | + |
| 34 | + |
| 35 | +Native Code Extension Features |
| 36 | ++++++++++++++++++++++++++++++++ |
| 37 | + |
| 38 | +The library uses `Cython`_ based native code extensions as wrappers to ``libssh2``. |
| 39 | + |
| 40 | +Extension features: |
| 41 | + |
| 42 | +* Thread safe - GIL is released as much as possible |
| 43 | +* Very low overhead |
| 44 | +* Super fast as a consequence of the excellent C library it uses and that it uses native code prodigiously |
| 45 | +* Object oriented - memory freed automatically and safely as objects expire |
| 46 | +* Use Python semantics where applicable, such as iterator support for SFTP file handles |
| 47 | +* Expose errors as Python exceptions where possible |
| 48 | +* Provide access to ``libssh2`` error code definitions |
| 49 | + |
| 50 | + |
| 51 | +Example |
| 52 | +-------- |
| 53 | + |
| 54 | +A simple usage example looks very similar to ``libssh2`` `usage examples <https://www.libssh2.org/examples/>`_. |
| 55 | + |
| 56 | +As mentioned, ``ssh2-python`` is intentially a thin wrapper over ``libssh2`` and directly maps most of its API. |
| 57 | + |
| 58 | +Clients using this library can be much simpler to use than interfacing with the ``libssh2`` API directly. |
| 59 | + |
| 60 | +.. code-block:: python |
| 61 | +
|
| 62 | + from __future__ import print_function |
| 63 | +
|
| 64 | + import os |
| 65 | + import socket |
| 66 | +
|
| 67 | + from ssh2 import Session |
| 68 | +
|
| 69 | + host = 'localhost' |
| 70 | + user = os.getlogin() |
| 71 | +
|
| 72 | + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 73 | + sock.connect((host, 22)) |
| 74 | +
|
| 75 | + session = Session() |
| 76 | + session.handshake(sock) |
| 77 | + session.agent_auth(user) |
| 78 | +
|
| 79 | + channel = session.open_session() |
| 80 | + channel.execute('echo me; exit 2') |
| 81 | + size, data = channel.read() |
| 82 | + while size > 0: |
| 83 | + print(data) |
| 84 | + size, data = channel.read() |
| 85 | + channel.close() |
| 86 | + print("Exit status: %s" % channel.get_exit_status()) |
| 87 | +
|
| 88 | +
|
| 89 | +:Output: |
| 90 | + |
| 91 | + me |
| 92 | + |
| 93 | + Exit status: 2 |
| 94 | + |
| 95 | + |
| 96 | +Comparison with other Python SSH2 libraries |
| 97 | +--------------------------------------------- |
| 98 | + |
| 99 | +Performance of above example, compared with Paramiko. |
| 100 | + |
| 101 | +.. code-block:: shell |
| 102 | +
|
| 103 | + time python examples/example_echo.py |
| 104 | + time python examples/paramiko_comparison.py |
| 105 | +
|
| 106 | +:Output: |
| 107 | + |
| 108 | + ``ssh2-python``:: |
| 109 | + |
| 110 | + real 0m0.141s |
| 111 | + user 0m0.037s |
| 112 | + sys 0m0.008s |
| 113 | + |
| 114 | + ``paramiko``:: |
| 115 | + |
| 116 | + real 0m0.592s |
| 117 | + user 0m0.351s |
| 118 | + sys 0m0.021s |
| 119 | + |
| 120 | + |
| 121 | +See `examples directory < link >`_ for more complete example scripts. |
| 122 | + |
| 123 | +.. _libssh2: https://www.libssh2.org |
| 124 | +.. _Cython: https://www.cython.org |
0 commit comments