[Tutor] telnetlib unable to cath gaierror

spir denis.spir at free.fr
Sun Dec 28 20:25:37 CET 2008


Le dimanche 28 décembre 2008 à 10:17 -0800, Steve Willoughby a écrit :
> On Sun, Dec 28, 2008 at 07:09:51PM +0100, Sander Sweers wrote:
> > I am having issues cathing exceptions from telnetlib. What I am doing is:
> > except gaierror:
> > Which gives me "NameError: name 'gaierror' is not defined" :-(
> 
> This error message contains your clue.  If the name you're referencing is not defined, you usually forgot to either explicitly import it, or to name it with its namespace attached.
> 
> So you either want to say:
> 
> 	from telnetlib import gaierror
> 
> at the top of your script, or if you earlier said simply "import telnetlib",
> 
> then your try/except clause would be like:
> 
> 	except telnetlib.gaierror:
> 
Well, actually, gaierror is an Exception of the socket module:

import telnetlib,socket
tc = telnetlib.Telnet()
try:
    tc.open("abc")
except socket.gaierror:
    print "Invalid hostname"
    raise

==>

spir at o:~/prog/parse$ python "___test.py"
Invalid hostname
Traceback (most recent call last):
  File "___test.py", line 6, in <module>
    tc.open("abc")
  File "/usr/lib/python2.5/telnetlib.py", line 225, in open
    for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
socket.gaierror: (-2, 'Name or service not known')

Denis



More information about the Tutor mailing list