async def run(self, **kwargs): lcs = {"context": self.context} async def interact(connection=None): global_dict = {**globals(), "print": print_formatted_text} await embed(return_asyncio_coroutine=True, globals=global_dict, locals=lcs) port = kwargs.get("port", 23) host = kwargs.get("host", "127.0.0.1") telnet_server = TelnetServer(interact=interact, port=port, host=host) stop_event = asyncio.Event(loop=self.context.loop) async def stop(**kwargs): if not stop_event.is_set(): try: await telnet_server.stop() finally: stop_event.set() for sig in ("SIGINT", "SIGTERM"): await self.context.add_signal_handler(sig, stop) telnet_server.start() await stop_event.wait()
async def main(ssh_port=8022, telnet_port=8023): ssh_server = PromptToolkitSSHServer(interact=interact) await asyncssh.create_server( lambda: ssh_server, "", ssh_port, server_host_keys=[ensure_key()] ) print(f"Running ssh server on port {ssh_port}...") telnet_server = TelnetServer(interact=interact, port=telnet_port) telnet_server.start() print(f"Running telnet server on port {telnet_port}...") while True: await asyncio.sleep(60)
class ExampleApplication(TelnetApplication): def client_connected(self, telnet_connection): # When a client is connected, erase the screen from the client and say # Hello. telnet_connection.erase_screen() telnet_connection.send('Welcome!\n') # Set CommandLineInterface. animal_completer = WordCompleter(['alligator', 'ant']) telnet_connection.set_cli( create_cli(telnet_connection.eventloop, message='Say something: ', lexer=HtmlLexer, completer=animal_completer), self.handle_command) def handle_command(self, telnet_connection, document): # When the client enters a command, just reply. if document.text == 'exit': telnet_connection.close() else: telnet_connection.send('You said: %s\n\n' % document.text) def client_leaving(self, telnet_connection): # Say 'bye' when the client quits. telnet_connection.send('Bye.\n') if __name__ == '__main__': TelnetServer(application=ExampleApplication(), port=2323).run()
def main(): server = TelnetServer(interact=interact, port=2323) server.start() get_event_loop().run_forever()