[Tutor] problem with telnetlib, threading class and try/except

Kent Johnson kent37 at tds.net
Wed Jan 17 19:57:53 CET 2007


Chris Hallman wrote:
> 
> 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 <http://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

You need an else: clause here; what if the error doesn't match either of 
your tests?

Kent

>         self.status = []
>         print self.tn <http://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 <http://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 <http://new.isdn.test.py>", line 35, in run
>     connect_status = self.Logon()
>   File "new.isdn.test.py <http://new.isdn.test.py>", line 59, in Logon
>     self.tn.read_until("Username:", 7)
> AttributeError: 'ISDNTest' object has no attribute 'tn'
> 
>  >Exit code: 0
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor




More information about the Tutor mailing list