[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.
This commit is contained in:
Robey Pointer 2004-01-27 02:04:59 +00:00
parent 27869f1d7a
commit d599570905
1 changed files with 20 additions and 0 deletions

View File

@ -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