diff options
author | Facundo Batista <facundobatista@gmail.com> | 2007-03-29 18:22:35 +0000 |
---|---|---|
committer | Facundo Batista <facundobatista@gmail.com> | 2007-03-29 18:22:35 +0000 |
commit | b6a5c9d605588e962f1cd242d2a52065f4c141db (patch) | |
tree | b462b11e559d04c2eab3bc4e4332bcc77e6e218a /Lib/telnetlib.py | |
parent | 30712ab82f7c545cfe89ddf56de135bfbf0ccb67 (diff) | |
download | cpython-git-b6a5c9d605588e962f1cd242d2a52065f4c141db.tar.gz |
Added timout parameter to telnetlib.Telnet. Also created
test_telnetlib.py with a basic test and timeout ones.
Docs are also updated.
Diffstat (limited to 'Lib/telnetlib.py')
-rw-r--r-- | Lib/telnetlib.py | 26 |
1 files changed, 8 insertions, 18 deletions
diff --git a/Lib/telnetlib.py b/Lib/telnetlib.py index a13e85cc98..f4c01e428c 100644 --- a/Lib/telnetlib.py +++ b/Lib/telnetlib.py @@ -184,7 +184,7 @@ class Telnet: """ - def __init__(self, host=None, port=0): + def __init__(self, host=None, port=0, timeout=None): """Constructor. When called without arguments, create an unconnected instance. @@ -195,6 +195,7 @@ class Telnet: self.debuglevel = DEBUGLEVEL self.host = host self.port = port + self.timeout = timeout self.sock = None self.rawq = '' self.irawq = 0 @@ -205,9 +206,9 @@ class Telnet: self.sbdataq = '' self.option_callback = None if host is not None: - self.open(host, port) + self.open(host, port, timeout) - def open(self, host, port=0): + def open(self, host, port=0, timeout=None): """Connect to a host. The optional second argument is the port number, which @@ -221,20 +222,9 @@ class Telnet: port = TELNET_PORT self.host = host self.port = port - msg = "getaddrinfo returns an empty list" - for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM): - af, socktype, proto, canonname, sa = res - try: - self.sock = socket.socket(af, socktype, proto) - self.sock.connect(sa) - except socket.error, msg: - if self.sock: - self.sock.close() - self.sock = None - continue - break - if not self.sock: - raise socket.error, msg + if timeout is not None: + self.timeout = timeout + self.sock = socket.create_connection((host, port), self.timeout) def __del__(self): """Destructor -- close the connection.""" @@ -661,7 +651,7 @@ def test(): port = socket.getservbyname(portstr, 'tcp') tn = Telnet() tn.set_debuglevel(debuglevel) - tn.open(host, port) + tn.open(host, port, timeout=0.5) tn.interact() tn.close() |