dnspython will do my DNS lookups very nicely, but it entirely ignores the contents of /etc/hosts
.
Is there a python library call which will do the right thing? ie check first in etc/hosts
, and only fall back to DNS lookups otherwise?
Use the command nslookup (this stands for Name Server Lookup) followed by the domain name or IP address you want to trace. Press enter. This command will simply query the Name Service for information about the specified IP address or domain name.
Go to Start and type cmd in the search field to open the command prompt. Alternatively, go to Start > Run > type cmd or command. Type nslookup and hit Enter. The displayed information will be your local DNS server and its IP address.
You can use the getaddrinfo() function in the socket module to perform a DNS lookup of a of a domain name, or retrieve information about a domain name or IP address.
I'm not really sure if you want to do DNS lookups yourself or if you just want a host's ip. In case you want the latter,
/!\ socket.gethostbyname is deprecated, prefer socket.getaddrinfo
from man gethostbyname
:
The gethostbyname*(), gethostbyaddr*(), [...] functions are obsolete. Applications should use getaddrinfo(3), getnameinfo(3),
import socket print(socket.gethostbyname('localhost')) # result from hosts file print(socket.gethostbyname('google.com')) # your os sends out a dns query
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With