{"library":"py3dns","title":"Py3DNS - Python 3 DNS library","description":"Py3DNS is a Python 3 library for making DNS queries and parsing responses. It provides a flexible API for various record types (A, AAAA, MX, NS, SOA, etc.) and is a port of the older `pydns` library, specifically adapted for Python 3. The current version is 4.0.2, with updates primarily focusing on Python compatibility, bug fixes, and feature enhancements like `asyncio` support.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install py3dns"],"cli":null},"imports":["import DNS"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import DNS\nimport os\n\n# Configure name servers, typically from system settings\n# For testing, you can explicitly set them or rely on system discovery\n# Example using Google's DNS for demonstration if system discovery fails or for specific testing\n# DNS.defaults['server'] = ['8.8.8.8', '8.8.4.4']\n\n# Discover name servers from /etc/resolv.conf or common system locations\nDNS.DiscoverNameServers()\n\n# Ensure name servers were found or set\nif not DNS.defaults.get('server'):\n    print(\"Warning: No DNS name servers discovered. Queries might fail.\")\n    print(\"Attempting to use public DNS servers as fallback.\")\n    DNS.defaults['server'] = ['8.8.8.8', '8.8.4.4'] # Fallback to Google DNS\n\n# Make an A record query for google.com\ntry:\n    req = DNS.Request(name='google.com', qtype='A')\n    response = req.req()\n\n    print(f\"DNS Query for google.com (A records):\\n\")\n    if response.answers:\n        for answer in response.answers:\n            print(f\"  Name: {answer.name}, Type: {answer.qtype}, Data: {answer.data}, TTL: {answer.ttl}\")\n    else:\n        print(\"  No A records found for google.com.\")\n\n    # Example: MX record query\n    req_mx = DNS.Request(name='example.com', qtype='MX')\n    response_mx = req_mx.req()\n    print(f\"\\nDNS Query for example.com (MX records):\\n\")\n    if response_mx.answers:\n        for answer in response_mx.answers:\n            print(f\"  Name: {answer.name}, Type: {answer.qtype}, Priority: {answer.data[0]}, Exchange: {answer.data[1]}\")\n    else:\n        print(\"  No MX records found for example.com.\")\n\nexcept DNS.DNSError as e:\n    print(f\"DNS Error: {e}\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")","lang":"python","description":"This quickstart demonstrates how to perform basic A and MX record DNS queries using Py3DNS. It includes name server discovery and a fallback to public DNS if discovery fails, making the example robust for various environments.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}