[Tutor] Socket Module
Martin A. Brown
martin at linux-ip.net
Mon Jul 27 00:59:39 CEST 2015
Hello Nym,
> Here is the updated code: https://bpaste.net/show/358583e1a0bd
It's short. I have included inline here:
import socket
ListOfIPAddresses = []
with open('top500ips.csv', 'r') as f:
for line in f:
line = line.strip()
ListOfIPAddresses.append(line)
for address in ListOfIPAddresses:
try:
ResolvedAddresses = socket.gethostbyaddr(address)
except:
print('No Resolution Available')
print(ResolvedAddresses)
> The issue that I am running into now is that for some reason, the
> script is not resolving known-public IP addresses that I am
> passing through. For example, some of the IPs, that I have used
> are for sites like facebook (173.252.120.6) github
> (207.97.227.239), however the script is not able to resolve them.
>
> But its interesting that I am able to resolve them using nslookup
> on windows command prompt. Which means my laptop's DNS setting is
> fine.
The apparent (?) DNS lookup failure
-----------------------------------
At time X, you run your Python program and something (perhaps in the
DNS resolution process) fails and you see "No Resolution Available",
but you do not know what has failed, nor for which address lookup.
At time Y, you run 'nslookup' at the shell prompt, receive an answer
and conclude that your script is operating properly. While this is
may appear logical, it is an incorrect conclusion.
One coding error (a dangerous habit to perpetuate)
--------------------------------------------------
When performing the DNS lookup, you are using something called
a 'bare except'. This will catch any and all errors, even if it's
something unrelated like, for example, a signal. This is a bad and
dangerous habit. In general, you should catch only the exceptions
that you can do something about.
In addition, this will offer you more information about the problem.
Here's a simple example, where I'm only changing two lines:
for address in ListOfIPAddresses:
try:
ResolvedAddresses = socket.gethostbyaddr(address)
except socket.herror as e:
print("No resolution available for %s: %s" % (address, e))
This will give you a little more information about what,
specifically, the failure is in your call to socket.gethostbyaddr()
Comment on NXdomain responses
-----------------------------
I picked at random an address that had no PTR record and tried to
call socket.gethostbyaddr('8.97.227.2'). What do you think I got
for my trouble? When running through the code block above, I saw
the following output to my terminal:
No resolution available for 8.97.227.2: [Errno 0] Resolver Error 0 (no error)
In short, there is no guarantee that anybody has properly set up
reverse DNS entries (DNS PTR records) for the addresses you are
looking up. Although the vast majority of lookups occur
successfully and smoothly, there are many things that can go wrong
in the network and on an end host which can cause transient errors
during DNS lookups, and it is possible that you have already
encountered some of these problems (even though I would not expect
to hit very many errors looking up PTR records for only 500 IPs).
May I wish you good luck resolving not just your addresses, but also
your problem!
-Martin
--
Martin A. Brown
http://linux-ip.net/
More information about the Tutor
mailing list