Search code examples
python-3.xtornadoxtermjs

how to send commands to web terminal?


here is my code It only creates terminal on web using 'bash' command.
I want to add functionality to this code similar to the interactive labs work. Which has terminal on one side and content part on other side. I want to run command on mouseclick function to my web terminal, and I am searching for solution how to do that can you suggest something?

class WebmuxTermManager(terminado.SingleTermManager):
def get_terminal(self, port_number):
    self.shell_command = ["bash"]
    term = self.new_terminal()
    self.start_reading(term)
    return term

class TerminalPageHandler(tornado.web.RequestHandler):
def get_host(self, port_number):
    pass

def get(self, port_number):
    return self.render("term.html", static=self.static_url, ws_url_path="/_websocket/"+port_number, hostname=self.get_host(port_number))

if __name__ == "__main__":

term_manager = WebmuxTermManager(shell_command=('bash'))
handlers = [
    (r"/_websocket/(\w+)", terminado.TermSocket, {'term_manager': term_manager}),
    (r"/shell/([\d]+)/?", TerminalPageHandler),
    (r"/webmux_static/(.*)", tornado.web.StaticFileHandler, {'path':os.path.join(TEMPLATE_DIR,"webmux_static")}),
]
application = tornado.web.Application(handlers, static_path=STATIC_DIR,template_path=TEMPLATE_DIR,term_manager=term_manager,debug=True)
application.listen(8888)


try:
    IOLoop.current().start()
except KeyboardInterrupt:
    logging.info("\nShuttiing down")
finally:
    term_manager.shutdown()
    IOLoop.current().close()

Solution

  • First I dont know what terminado is, so I will stick to tornado's websockets,

    Make a websocket class to send and recieve messages as per your ruleset

    class WebsocketHandler(tornado.websocket.WebSocketHandler):
        def check_origin(self, origin):
            # deals with allowing certain ip address ranges or domain_names to
            # connect to your websocket
    
            pass
    
        def open(self):
            # perform some logic when the socket is connected to the client side
            # eg give it a unique id to and append it to a list etc, its up to you
    
            pass
    
        def on_message(self, command):
            # this is where your problem lies
            # act on the message
            send_back = self.runCommand(command)
            self.write_message(send_back)
    
       def on_close(self):
           # delete the socket
    
           pass
    
       def runCommand(self, command):
           # import shlex
           # import supbrocess
           cmd = shlex.split(command)
           process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
           stdout, stderr = process.communicate()
    
           return dict(
               stdout=stdout.decode(),
               stderr=stdout.decode()
           )
    

    The route for WebsocketHandler class is

    (r"/websocket_test", WebsocketHandler)
    

    Hook it up to your routes and start the tornado server

    On the client side

    Connect using javascript as follows:

    //secure or unsecure, up to you.
    
    unsecure_test_conn = new WebSocket('ws://ip_address_or_domain_name/websocket_test')
    secure_test_conn = new WebSocket('wss://ip_address_or_domain_name/websocket_test')
    
    unsecure_test_conn.onmessage = function(event){
        data = JSON.parse(event.data)
        //act on the data as you see fit and probably send it back to server
        send_back = parseMessage(data)
        unsecure_test_conn.send(send_back)
    }
    

    This should get you started on how to send information back and forth on the web.