|
2 | 2 | # private key in it to use as a server host key. An SSH host certificate |
3 | 3 | # can optionally be provided in the file ``ssh_host_key-cert.pub``. |
4 | 4 |
|
5 | | -import asyncio, asyncssh, sys, time, logging |
| 5 | +import asyncio, asyncssh, sys, logging |
6 | 6 |
|
7 | 7 | passwords = { |
8 | 8 | 'user123': 'secretpw' |
9 | 9 | } |
10 | 10 |
|
11 | | -def handle_client(process): |
| 11 | +async def handle_client(process): |
12 | 12 | process.stdout.write('success\n') |
13 | | - time.sleep(10) |
| 13 | + await asyncio.sleep(10) |
14 | 14 | process.exit(0) |
15 | 15 |
|
16 | 16 | class MySSHServer(asyncssh.SSHServer): |
@@ -47,33 +47,39 @@ def public_key_auth_supported(self): |
47 | 47 | async def start_server(): |
48 | 48 | asyncssh.set_log_level('DEBUG') |
49 | 49 | asyncssh.set_debug_level(2) |
50 | | - await asyncssh.create_server(MySSHServer, '', 8022, |
51 | | - server_host_keys=[ |
52 | | - '/app/etc/ssh/ssh_host_ecdsa_key', |
53 | | - '/app/etc/ssh/ssh_host_rsa_key', |
54 | | - ], |
55 | | - process_factory=handle_client) |
56 | | - |
57 | | -print("SETTING LOGGER") |
58 | | -root = logging.getLogger() |
59 | | -root.setLevel(logging.DEBUG) |
60 | | - |
61 | | -ch = logging.StreamHandler(sys.stdout) |
62 | | -ch.setLevel(logging.DEBUG) |
63 | | -formatter = logging.Formatter('%(message)s') |
64 | | -ch.setFormatter(formatter) |
65 | | -root.addHandler(ch) |
66 | | - |
67 | | -print("STARTING UP") |
68 | | -loop = asyncio.get_event_loop() |
69 | | - |
70 | | -try: |
71 | | - loop.run_until_complete(start_server()) |
72 | | -except (OSError, asyncssh.Error) as exc: |
73 | | - sys.exit('Error starting server: ' + str(exc)) |
74 | | - |
75 | | -print("LISTENER READY") |
76 | | - |
77 | | -# Only run the loop once for testing |
78 | | -#loop.call_soon(loop.stop) |
79 | | -loop.run_forever() |
| 50 | + server = await asyncssh.create_server(MySSHServer, '', 8022, |
| 51 | + server_host_keys=[ |
| 52 | + '/app/etc/ssh/ssh_host_ecdsa_key', |
| 53 | + '/app/etc/ssh/ssh_host_rsa_key', |
| 54 | + ], |
| 55 | + process_factory=handle_client) |
| 56 | + return server |
| 57 | + |
| 58 | +async def main(): |
| 59 | + print("SETTING LOGGER") |
| 60 | + root = logging.getLogger() |
| 61 | + root.setLevel(logging.DEBUG) |
| 62 | + |
| 63 | + ch = logging.StreamHandler(sys.stdout) |
| 64 | + ch.setLevel(logging.DEBUG) |
| 65 | + formatter = logging.Formatter('%(message)s') |
| 66 | + ch.setFormatter(formatter) |
| 67 | + root.addHandler(ch) |
| 68 | + |
| 69 | + print("STARTING UP") |
| 70 | + |
| 71 | + try: |
| 72 | + server = await start_server() |
| 73 | + except (OSError, asyncssh.Error) as exc: |
| 74 | + sys.exit('Error starting server: ' + str(exc)) |
| 75 | + |
| 76 | + print("LISTENER READY") |
| 77 | + |
| 78 | + async with server: |
| 79 | + await server.wait_closed() |
| 80 | + |
| 81 | +if __name__ == '__main__': |
| 82 | + try: |
| 83 | + asyncio.run(main()) |
| 84 | + except KeyboardInterrupt: |
| 85 | + print("\nServer shutting down...") |
0 commit comments