Skip to content

Commit 6987253

Browse files
committed
asyncssh-server test: update asyncio usage
1 parent 7f79dc1 commit 6987253

File tree

1 file changed

+39
-33
lines changed

1 file changed

+39
-33
lines changed

src/test/resources/asyncssh-server/server.py

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
# private key in it to use as a server host key. An SSH host certificate
33
# can optionally be provided in the file ``ssh_host_key-cert.pub``.
44

5-
import asyncio, asyncssh, sys, time, logging
5+
import asyncio, asyncssh, sys, logging
66

77
passwords = {
88
'user123': 'secretpw'
99
}
1010

11-
def handle_client(process):
11+
async def handle_client(process):
1212
process.stdout.write('success\n')
13-
time.sleep(10)
13+
await asyncio.sleep(10)
1414
process.exit(0)
1515

1616
class MySSHServer(asyncssh.SSHServer):
@@ -47,33 +47,39 @@ def public_key_auth_supported(self):
4747
async def start_server():
4848
asyncssh.set_log_level('DEBUG')
4949
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

Comments
 (0)