0

I have a program, in python, that need to do a reverse DNS for like 1000 IP. To do this I'm using socket.gethostbyaddr, but sometimes, for some reasons, I can't do reverse DNS on my computer so it will take like 3000 seconds to do this because each IP will get me an exception after 3 seconds ( timeout ).

So my problem is that I don't want this, I want to know how I can check that I can't do reverse DNS, so I no longer need to do this and lose 3000 seconds.

I first tried watching the returned exception in the case of a timeout ( because I'm not able to do reverse DNS ), but socket.gethostbyaddr return the same exception as if the IP have an unknown host ( in the case if I'm able to do reverse DNS ).

So how can I do this ?

Thanks.

1 Answer 1

2

Lots of the Python functions that end up making more low level system calls return the same exception class for all their errors. But they usually set the errno or other attributes, and this may be different depending on the case.

try: hostname = socket.gethostbyaddr(an_ip_address)[0] except Exception as exc: if exc.errno == 2: raise Exception('Not able to do reverse DNS at this time, aborting') 

Maybe something like that will help. Though it is entirely possible that both error cases will have the same errno.

In my experiments (under Linux) I got an errno of 2 for a lookup when I had turned off my network connection, and an errno of 1 when I asked for a lookup on an address that didn't exist. You could also use strerror for the same purposes. In my experiment e.strerror is set to "Host name lookup failure" and "Unknown Host" respectively. If you are not sure what attributes are set on an exception you can always just do a dir(e) to get an idea.

Sign up to request clarification or add additional context in comments.

2 Comments

That's exactly what I searched, in my case due to some reasons I've got the same errno but that's another problem, but it helps me a lot ! Thanks you.
Ugh, that's annoying. Are you using windows? If both exceptions are exactly the same then you would have to do a secondary test. There has to be something different with the machine in each case. But identifying what that might be is hard.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.