nslookup

raw JSON →
1.8.1 verified Fri May 01 auth: no python

Sensible high-level DNS lookups in Python, using DNSpython resolver. Version 1.8.1 provides an easy-to-use wrapper around dnspython for common DNS queries (A, AAAA, MX, NS, TXT, etc.). It is released under MIT license and maintained on GitHub. Release cadence is low, with occasional updates.

pip install nslookup
error AttributeError: 'NsLookup' object has no attribute 'dns_lookup'
cause In version 1.8.0+, the generic dns_lookup method was removed.
fix
Use specific methods like .a(), .mx(), .aaaa() instead.
error TypeError: __init__() missing required positional argument: 'dns_servers'
cause The NsLookup class requires a list of DNS servers as argument.
fix
Initialize with NsLookup(dns_servers=['8.8.8.8']).
error dnspython.exception.Timeout: The DNS operation timed out.
cause Default dnspython timeout may be too short for some queries.
fix
Specify a longer timeout via dnspython custom resolver, or use faster DNS servers. The nslookup library does not expose timeout settings directly; consider using dnspython directly.
gotcha The default DNS server list is empty; you must provide at least one server. Failing to do so will raise an error on query.
fix Always pass a list of DNS servers, e.g., NsLookup(dns_servers=['8.8.8.8']).
breaking In version 1.8.0, the module's internal API changed: previously, `ns_lookup = NsLookup()` then `ns_lookup.dns_lookup()` was used. This was removed. Now you must call specific methods like `.a()`, `.mx()`, etc.
fix Use the new specific methods (e.g., .a(), .mx(), .aaaa(), .ns(), .txt()) instead of the generic .dns_lookup().
gotcha Some DNS server responses may be empty or cause exceptions. The library does not always handle timeouts gracefully (depends on dnspython defaults).
fix Wrap calls in try/except blocks or implement retry logic with alternative servers.

Initialize NsLookup with a list of DNS servers, then call methods like .a() or .mx() to perform lookups. The result object contains .answer, .response_full, etc.

from nslookup import NsLookup

domain = "example.com"
dns_query = NsLookup(dns_servers=["8.8.8.8"])
resp = dns_query.a(domain)
print(resp.answer)

# For MX records:
mx_resp = dns_query.mx(domain)
print(mx_resp.answer)