summaryrefslogtreecommitdiffstats
path: root/examples/asyncio-ssh-python-embed.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-15 16:33:15 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-15 16:33:15 +0000
commite5f7a41d988de298069eda636e823f374b973bd4 (patch)
treea6a97f4d2ed5e59ab11cbae6b403ddebc5effe00 /examples/asyncio-ssh-python-embed.py
parentInitial commit. (diff)
downloadptpython-e5f7a41d988de298069eda636e823f374b973bd4.tar.xz
ptpython-e5f7a41d988de298069eda636e823f374b973bd4.zip
Adding upstream version 3.0.26.upstream/3.0.26upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'examples/asyncio-ssh-python-embed.py')
-rwxr-xr-xexamples/asyncio-ssh-python-embed.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/examples/asyncio-ssh-python-embed.py b/examples/asyncio-ssh-python-embed.py
new file mode 100755
index 0000000..be0689e
--- /dev/null
+++ b/examples/asyncio-ssh-python-embed.py
@@ -0,0 +1,56 @@
+#!/usr/bin/env python
+"""
+Example of running the Python REPL through an SSH connection in an asyncio process.
+This requires Python 3, asyncio and asyncssh.
+
+Run this example and then SSH to localhost, port 8222.
+"""
+import asyncio
+import logging
+
+import asyncssh
+
+from ptpython.contrib.asyncssh_repl import ReplSSHServerSession
+
+logging.basicConfig()
+logging.getLogger().setLevel(logging.INFO)
+
+
+class MySSHServer(asyncssh.SSHServer):
+ """
+ Server without authentication, running `ReplSSHServerSession`.
+ """
+
+ def __init__(self, get_namespace):
+ self.get_namespace = get_namespace
+
+ def begin_auth(self, username):
+ # No authentication.
+ return False
+
+ def session_requested(self):
+ return ReplSSHServerSession(self.get_namespace)
+
+
+async def main(port: int = 8222) -> None:
+ """
+ Example that starts the REPL through an SSH server.
+ """
+ # Namespace exposed in the REPL.
+ environ = {"hello": "world"}
+
+ # Start SSH server.
+ def create_server() -> MySSHServer:
+ return MySSHServer(lambda: environ)
+
+ print("Listening on :%i" % port)
+ print('To connect, do "ssh localhost -p %i"' % port)
+
+ await asyncssh.create_server(
+ create_server, "", port, server_host_keys=["/etc/ssh/ssh_host_dsa_key"]
+ )
+ await asyncio.Future() # Wait forever.
+
+
+if __name__ == "__main__":
+ asyncio.run(main())