Is there a python-module that\'s doing the same stuff as nslookup does? I am planning to use nslookup on digging some information regarding the domain of a URL to be scrappe
You need to use DNSPython
import dns.resolver
answers = dns.resolver.query('dnspython.org', 'MX')
for rdata in answers:
print 'Host', rdata.exchange, 'has preference', rdata.preference
You should use socket library http://docs.python.org/2/library/socket.html
System function call is not a good practice in this case.
Note that socket.getfqdn()
can return the full-qualified name of a hostname. See: http://docs.python.org/2/library/socket.html?highlight=socket.getaddrinfo#socket.getfqdn
For example:
python -c 'import socket; print(socket.gethostname()); print(socket.getfqdn());'
myserver
myserver.mydomain.local
But the result depends the /etc/hosts
configuration. If you have:
$ cat /etc/hosts
127.0.0.1 myserver localhost.localdomain localhost
The result of socket.getfqdn()
will be:
python -c 'import socket; print(socket.getfqdn());'
localhost.localdomain
Oooops! To solve that, the only solution I know is to change the /etc/hosts
as follow:
$ cat /etc/hosts
127.0.0.1 myserver myserver.mydomain.local localhost.localdomain localhost
Hope it helps!