Over the years, there have been some changes in how some functions and commands provide an output. Which is why, it is difficult to follow older tutorials, which sometimes do not conform with the latest revisions to software and its commands.
One such change happened whilst I was using nslookup
and python
to lookup ip addresses
on windows, as I do not primarily own a mac or linux.
How, can we fetch, only the IP address of a 'Top Level Url' (tld) using python and nslookup, as of 2019?
Do not use external tools for needs that can be done completely inside your programming language using the proper library, which can be dnspython
in your case.
In [2]: import dns
In [3]: import dns.resolver
In [5]: import dns.rdataclass
In [7]: import dns.rdatatype
In [9]: ans = dns.resolver.query('www.example.com', rdtype=dns.rdatatype.A, rdclass=dns.rdataclass.IN)
In [10]: print ans.rrset
www.example.com. 43193 IN A 93.184.216.34
In [12]: print ans.rrset[0]
93.184.216.34
Read the full featured documentation at http://www.dnspython.org/docs/1.16.0/ for more details, especially input and output parameters.
Two important points:
query
will always succeed; make sure to handle its errorsquery
will necessarily gives you back records of the type you expected, you may get something completely different, so you have to check and not blindly assume.