TimeoutSocket exceptions handling

Bill Bell bill-bell at bill-bell.hamilton.on.ca
Fri Mar 23 13:11:45 EST 2001


I'm new to Python. In an effort to understand why I can't get hold of 
an exception from O'Malley's TimeoutSocket I wrote the following two 
teeny-tiny scripts.

Script2.py:

	import Script1

	try:
	    myFunc()
	except myErr, (reason):
	    print 'myErr trapped: ', reason

Script1.py:

	class myErr ( Exception ):
	    pass

	def myFunc():
	    raise myErr ( 'myErr reason' )

Couldn't be much simpler, huh? And they even seem to work.

Timeoutsocket.py (looks fantastic, thanks Alex Martelli), contains the 
following:

...
class Timeout(Exception):
    pass
...
        raise Timeout("Attempted connect to %s timed out." % 
str(addr) )

My code that uses TimeoutSocket has

        try:        
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((self.whoisserver, 43))
            s.send("%s\n" % self.domain)
            page = ""
            while 1:
                data = s.recv(8196)
                if not data: break
                page = page + data
                pass
            s.close()
        except Timeout, (reason):
            raise 'TimedOut', 'Whois server timedout'

If my sanity has not departed me this combination appears similar to 
my would-be autodidactic efforts.

However, not so: Python says "NameError: There is no variable 
named 'Timeout'" for my 'except' statement.

I'd appreciate a word of advice, or a pointer to code that works.

Bill




More information about the Python-list mailing list