python module for nslookup

后端 未结 9 826
渐次进展
渐次进展 2020-12-10 00:27

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

相关标签:
9条回答
  • 2020-12-10 01:27

    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
    
    0 讨论(0)
  • 2020-12-10 01:31

    You should use socket library http://docs.python.org/2/library/socket.html

    System function call is not a good practice in this case.

    0 讨论(0)
  • 2020-12-10 01:31

    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!

    0 讨论(0)
提交回复
热议问题