From c6e1b5b74d574db5ab8dc37bc76bf8fbbc93f959 2011-08-16 22:51:48 From: MinRK Date: 2011-08-16 22:51:48 Subject: [PATCH] split open_tunnel part of tunnel_connection into separate method This allows connection forwarding without establishing the final connection (needed if the final connection is delayed, e.g. heartbeats) --- diff --git a/IPython/external/ssh/tunnel.py b/IPython/external/ssh/tunnel.py index fc073b3..871b557 100644 --- a/IPython/external/ssh/tunnel.py +++ b/IPython/external/ssh/tunnel.py @@ -110,6 +110,22 @@ def tunnel_connection(socket, addr, server, keyfile=None, password=None, paramik selected local port of the tunnel. """ + new_url, tunnel = open_tunnel(addr, server, keyfile=keyfile, password=password, paramiko=paramiko) + socket.connect(new_url) + return tunnel + + +def open_tunnel(addr, server, keyfile=None, password=None, paramiko=None): + """Open a tunneled connection from a 0MQ url. + + For use inside tunnel_connection. + + Returns + ------- + + (url, tunnel): The 0MQ url that has been forwarded, and the tunnel object + """ + lport = select_random_ports(1)[0] transport, addr = addr.split('://') ip,rport = addr.split(':') @@ -121,8 +137,7 @@ def tunnel_connection(socket, addr, server, keyfile=None, password=None, paramik else: tunnelf = openssh_tunnel tunnel = tunnelf(lport, rport, server, remoteip=ip, keyfile=keyfile, password=password) - socket.connect('tcp://127.0.0.1:%i'%lport) - return tunnel + return 'tcp://127.0.0.1:%i'%lport, tunnel def openssh_tunnel(lport, rport, server, remoteip='127.0.0.1', keyfile=None, password=None, timeout=15): """Create an ssh tunnel using command-line ssh that connects port lport