diff options
author | Robey Pointer <robey@lag.net> | 2004-01-27 02:04:59 +0000 |
---|---|---|
committer | Robey Pointer <robey@lag.net> | 2004-01-27 02:04:59 +0000 |
commit | d5995709055c6319b4f02c322bb5b0bfc2c584b7 (patch) | |
tree | c428c7e4a931a8db312c20329ab06ace412c60b5 | |
parent | 27869f1d7ad953d2262b36e2692d784c906a40d2 (diff) |
[project @ Arch-1:robey@lag.net--2003-public%secsh--dev--1.0--patch-26]
Transport constructor can take hostname or address tuple
part of an ongoing attempt to make "simple" versions of some of the API calls,
so you can do common-case operations with just a few calls:
Transport's constructor will now let you pass in a string or tuple instead
of a socket-like object. if you pass in a string, it assumes the string is
a hostname (with optional ":port" segment) and turns that into an address
tuple. if you pass in a tuple, it assumes it's an address tuple. in both
cases, it then creates a socket, connects to the given address, and then
continues as if that was the socket passed in.
the idea being that you can call Transport('example.com') and it will do
the right thing.
-rw-r--r-- | paramiko/transport.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/paramiko/transport.py b/paramiko/transport.py index 92590340..69669aaa 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -134,9 +134,29 @@ class BaseTransport (threading.Thread): string. Returns 0 or raises C{EOFError} if the stream has been closed. + For ease of use, you may also pass in an address (as a tuple) or a host + string as the C{sock} argument. (A host string is a hostname with an + optional port (separated by C{":"}) which will be converted into a + tuple of C{(hostname, port)}.) A socket will be connected to this + address and used for communication. Exceptions from the C{socket} call + may be thrown in this case. + @param sock: a socket or socket-like object to create the session over. @type sock: socket """ + if type(sock) is str: + # convert "host:port" into (host, port) + hl = sock.split(':', 1) + if len(hl) == 1: + sock = (hl[0], 22) + else: + sock = (hl[0], int(hl[1])) + if type(sock) is tuple: + # connect to the given (host, port) + hostname, port = sock + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.connect((hostname, port)) + # okay, normal socket-ish flow here... threading.Thread.__init__(self, target=self._run) self.randpool = randpool self.sock = sock |