[Tutor] problem with telnetlib, threading class and try/except
Chris Hallman
challman at gmail.com
Wed Jan 17 17:39:51 CET 2007
I'm writing a multithreaded program that initiates a dial backup connection
to check the B channels on ISDN BRI connections. I recently added logic to
check for offline devices (Operation timed out) and DNS errors (getaddrinfo
failed). When it encounters the exception, it doesn't appear to be exiting
(by way of the return command) the thread, hence the error.
The input file contains:
rtr3926
s0877ap01
Here is part of the program:
import os, os.path, random, re, socket, string, sys, telnetlib, threading,
time
from time import strftime
from threading import Thread
class ISDNTest(threading.Thread):
def __init__(self, host):
Thread.__init__(self)
self.host = host
try:
self.tn = telnetlib.Telnet(self.host)
except socket.error, err:
if "Operation timed out" in err:
print "1st if", self.host, err #progress checking
dialTestResult[self.host] = ["", "connection timed out"]
return
elif "getaddrinfo failed" in err:
print "2nd if", self.host, err #progress checking
dialTestResult[self.host] = ["", "DNS resolution failed"]
return
self.status = []
print self.tn #progress checking
def run(self):
connect_status = self.Logon()
...........(much more program not pasted here)
for entry in inFile:
if entry == '\n':
continue
elif "#" in entry:
continue
entry = entry.replace("\n", "")
while count_active() >= Max_Threads:
time.sleep(1)
threads = ISDNTest(entry)
tlist.append(threads)
threads.start()
for thread in tlist:
thread.join()
Here is there error:
>pythonw -u "new.isdn.test.py"
<telnetlib.Telnet instance at 0x00B21670>
log me on rtr3926 (progress checking message, this isn't an error)
1st if s0877ap01 (10060, 'Operation timed out') (progress checking message;
this indicates an exception occurred)
log me on s0877ap01 (progress checking message, this isn't an error)
Exception in thread Thread-2:
Traceback (most recent call last):
File "c:\python25\lib\threading.py", line 460, in __bootstrap
self.run()
File "new.isdn.test.py", line 35, in run
connect_status = self.Logon()
File "new.isdn.test.py", line 59, in Logon
self.tn.read_until("Username:", 7)
AttributeError: 'ISDNTest' object has no attribute 'tn'
>Exit code: 0
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070117/5f1724e3/attachment.html
More information about the Tutor
mailing list