self.remotehost = remotehost
self.daemon = True

def run(self):
    if subprocess.call(['ssh', '-N', '-L', str(self.localport) + ':' + self
        .remotehost + ':' + str(self.remoteport), self.remoteuser + '@' +
        self.remotehost]):
        raise Exception('ssh tunnel setup failed')
if __name__ == '__main__':
    tunnel = SshTunnel(2222, 80, 'karel', 'localhost')
tunnel.start()
time.sleep(1)
subprocess.call(['curl', 'http://localhost:2222'])
How to open an SSH tunnel using python?