Search code examples
pythonpython-3.xdnspython

Will DNSpython use Cache?


I'm using DNSPython library to get DNS resolution time in my script. I've a query on the caching part, that whether this library uses Cache on its own. My requirement is to check how much it takes to resolve DNS from a endpoint.

Here's the part of script I'm using to get resolution time now,

resolver = dns.resolver.Resolver()
resolver.timeout = 1
resolver.lifetime = 1
return float("{0:.3f}".format(dns.resolver.query(url).response.time*1000))

If DNSPython uses caching, suggest me a way to flush it. Also suggest me some other idea if available, to get DNS resolution time without caching.


Solution

  • If DNSPython uses caching, suggest me a way to flush it. Also suggest me some other idea if available, to get DNS resolution time without caching.

    dns.resolver.Cache.flush()
    

    See the source here and the documentation here.

    Most (virtually all?) operating systems have command line utilities that drop DNS cache. Use in conjunction with time for performance benchmarking. Perhaps also consider a timing decorator in your python code for benchmarking.