{"id":2484,"library":"dnslib","title":"dnslib","description":"dnslib is a simple Python library designed to encode and decode DNS wire-format packets. It provides comprehensive support for converting DNS packets between wire format, Python objects, and human-readable Zone/DiG textual representations. Additionally, it offers a server framework that facilitates the creation of custom DNS resolvers. The library is currently at version 0.9.26 and is in maintenance mode, with no further active development planned.","status":"maintenance","version":"0.9.26","language":"en","source_language":"en","source_url":"https://github.com/paulc/dnslib","tags":["dns","networking","packet","protocol","resolver"],"install":[{"cmd":"pip install dnslib","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"DNSRecord","correct":"from dnslib import DNSRecord, DNSHeader, DNSQuestion, QTYPE, RR, A, CNAME, MX, NS, SOA"},{"note":"While `from dnslib import *` is often seen in examples, importing specific classes is generally better practice. The `dnslib.dns` submodule is part of the package structure, but the core classes are typically exposed directly under `dnslib`.","wrong":"from dnslib.dns import *","symbol":"*","correct":"from dnslib import *"}],"quickstart":{"code":"from dnslib import DNSRecord, DNSHeader, DNSQuestion, QTYPE, RR, A, CNAME\nimport binascii\n\n# --- Example 1: Creating a simple DNS A query ---\nq = DNSRecord(q=DNSQuestion('example.com', QTYPE.A))\nprint('--- DNS Query (example.com A) ---\\n', q)\n\n# Pack the query to wire format\nquery_packet = q.pack()\n# print(f'Wire format: {binascii.hexlify(query_packet).decode()}')\n\n# --- Example 2: Parsing a DNS response (simulated) ---\n# Example DNS response packet (e.g., for google.com A record)\n# In a real scenario, this would come from a network socket.\n# d5ad818000010005000000000377777706676f6f676c6503636f6d0000010001c00c0005000100000005000803777777016cc010c02c0001000100000005000442f95b68c02c0001000100000005000442f95b63c02c0001000100000005000442f95b67c02c0001000100000005000442f95b93\n# A simplified, small response for 'test.com' to 1.2.3.4\n# Real packets are more complex, this is just for demonstration.\nresponse_hex = b'\\x01\\x00\\x81\\x80\\x00\\x01\\x00\\x01\\x00\\x00\\x00\\x00\\x04test\\x03com\\x00\\x00\\x01\\x00\\x01\\xc0\\x0c\\x00\\x01\\x00\\x01\\x00\\x00\\x00\\x3c\\x00\\x04\\x01\\x02\\x03\\x04'\nresponse_packet = response_hex\n\n# Parse the response packet\ntry:\n    r = DNSRecord.parse(response_packet)\n    print('\\n--- Parsed DNS Response (test.com A to 1.2.3.4) ---\\n', r)\nexcept Exception as e:\n    print(f\"Error parsing response: {e}\")\n\n# --- Example 3: Creating a reply from a query (simplified) ---\nq_example = DNSRecord(q=DNSQuestion('host.example.com'))\nreply = q_example.reply()\nreply.add_answer(RR('host.example.com', QTYPE.A, rdata=A('192.168.1.1')))\nprint('\\n--- Simple DNS Reply ---\\n', reply)","lang":"python","description":"This quickstart demonstrates how to construct a basic DNS A-record query and how to parse a (simulated) DNS response packet. It also shows a minimal example of how to create a reply to a query. Key classes like `DNSRecord`, `DNSQuestion`, and `QTYPE` are used for this."},"warnings":[{"fix":"Be aware that new features are unlikely. For active development or a different approach, consider alternatives like `dnspython`.","message":"The dnslib library is in maintenance mode and no longer undergoes active development. While minor bug fixes may be applied, significant new features or major architectural changes are not expected. Users should factor this into long-term project planning.","severity":"deprecated","affected_versions":"0.9.25+"},{"fix":"Migrate your application to Python 3.7 or newer. If Python 2.7 support is strictly required, pin the `dnslib` version to `0.9.24` or earlier.","message":"Python 2.7 support has been officially dropped. Version 0.9.24 was the last release to support Python 2.7 and Python <3.7. Later versions require Python 3.7+ (though the 0.9.25 release notes refer to 'Python2 support' in the context of maintenance mode for users reliant on the old API).","severity":"breaking","affected_versions":"0.9.25+"},{"fix":"Review your code for usage of the 'Bimap' interface and hostname parsing/generation logic. Consult the changelog or source for exact API differences if migrating from very old versions.","message":"Significant API changes occurred around version 0.9.0 and were further referenced in 0.9.25. Specifically, the 'Bimap' interface for lookups changed, and hostnames are now returned with a trailing dot by default (RFC compliant). Updating from older `dnslib` versions might require code adjustments for these interface changes.","severity":"breaking","affected_versions":"0.9.0+, potentially impacting migrations from <0.9.0 and potentially again with 0.9.25+"},{"fix":"If your application handles RFC2136 delete UPDATEs, test thoroughly after upgrading to 0.9.25+. You may need to adjust your error handling or packet construction/parsing logic for these specific types of updates.","message":"There's a reported issue (Issue #76) where RFC2136 delete UPDATEs are parsed differently between versions. Version 0.9.24 would parse them with `rdata=''`, while 0.9.25+ rejects them, raising an error. This can affect specific DNS update operations.","severity":"gotcha","affected_versions":"0.9.25+"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}