Paramiko SSH Tunnel Shutdown Issue

前端 未结 4 894
萌比男神i
萌比男神i 2021-02-10 23:36

I\'m working on a python script to query a few remote databases over an established ssh tunnel every so often. I\'m fairly familiar with the paramiko library, so that was my cho

4条回答
  •  梦毁少年i
    2021-02-11 00:26

    Note that you don't have do the Subhandler hack as shown in the demo code. The comment is wrong. Handlers do have access to their Server's data. Inside a handler you can use self.server.instance_data.

    If you use the following code, in your Handler, you would use

    • self.server.chain_host
    • self.server.chain_port
    • self.server.ssh_transport

    class ForwardServer(SocketServer.ThreadingTCPServer):
        daemon_threads = True
        allow_reuse_address = True
    
        def __init__(
              self, connection, handler, chain_host, chain_port, ssh_transport):
            SocketServer.ThreadingTCPServer.__init__(self, connection, handler)
            self.chain_host = chain_host
            self.chain_port = chain_port
            self.ssh_transport = ssh_transport
    ...
    
    server = ForwardServer(('', local_port), Handler, 
                           remote_host, remote_port, transport)
    server.serve_forever()
    

提交回复
热议问题