Set specific DNS server using dns.resolver (pythondns)

后端 未结 4 1914
孤街浪徒
孤街浪徒 2020-12-03 02:22

I am using dns.resolver from dnspython.

Is it possible to set the IP address of the server to use for queries ?

4条回答
  •  醉梦人生
    2020-12-03 03:01

    Although this is somewhat of an old thread, I will jump in. I've bumped against the same challenge and I thought I would share the solution. So, basically the config file would populate the 'nameservers' instance variable of the dns.resolver.Resolver you are using. Hence, if you want to coerce your Resolver to use a particular nameserver, you can do it direcly like this:

    import dns.resolver
    
    my_resolver = dns.resolver.Resolver()
    
    # 8.8.8.8 is Google's public DNS server
    my_resolver.nameservers = ['8.8.8.8']
    
    answer = my_resolver.query('google.com')
    

    Hope someone finds it useful.

提交回复
热议问题