beginner question
Jeff Epler
jepler at unpythonic.net
Thu Dec 26 23:06:29 EST 2002
If one of the hostnames doesn't exist, gethostbyaddr() "raises an
exception". You must handle this exception if you want to continue the for
loop:
for i in range(1, 255):
st="199.184.119.%d" % (i,)
try:
print gethostbyaddr(st)
except socket.herror, detail:
print "%s: %s" % (st, detail[1])
The first few lines of the output for the network I selected look like
this:
('condor.inebraska.com', [], ['199.184.119.1'])
199.184.119.2: Unknown host
('new-pigeon.inebraska.com', [], ['199.184.119.3'])
199.184.119.4: Unknown host
199.184.119.5: Unknown host
Exceptions are Python's way of signalling some sort of error. It forces
functions to exit until the highest level is reached (ending the program)
or until an 'except' block that matches is encountered. If you put the
'except' block in the right place, you can deal with the error condition in
an appropriate way and continue with the rest of your program.
Jeff
More information about the Python-list
mailing list