Skip to main content

A Python library for secure DNS resolution (DoH, DoT, mTLS) with sync & async support.

Project description

PrivyDNS Python Test Suite

Pytest Python Python Version PyPi MIT License

PrivyDNS is a Python library designed to securely query DNS records over encrypted protocols including DNS over HTTPS (DoH), DNS over TLS (DoT), and DNS over Mutual TLS (mTLS). It supports both synchronous and asynchronous DNS queries with features such as caching, retry mechanisms, logging, and encryption. It provides developers with an easy-to-use interface to enhance DNS security, reliability, and performance.

Features

  • Protocols Supported:
    • DNS over HTTPS (DoH)
    • DNS over TLS (DoT)
    • DNS over Mutual TLS (mTLS)
  • Cache Support: Caches DNS responses to improve performance.
  • Retry Mechanism: Automatically retries failed DNS queries.
  • Logging: Provides detailed logs for better debugging.
  • Async & Sync Support: Supports both asynchronous and synchronous operations.

Architecture

You can learn more about the architecture, protocols, and features of PrivyDNS in the ARCHITECTURE.md file.

Background

For an in-depth explanation of DNS over HTTPS (DoH), DNS over TLS (DoT), and DNSCrypt, refer to the BACKGROUND.md file.

Installation

pip install privydns

Usage

DNS Query using DoH

import asyncio
from privydns import DNSResolver

async def main():
    resolver = DNSResolver()
    response = await resolver.query("example.com", protocol="doh")
    print(response)

asyncio.run(main())

# 2025-03-22 07:10:57,479 - INFO - Resolving example.com (A) using doh
# [<DNS example.com. IN A RRset: [<96.7.128.198>, <96.7.128.175>, <23.192.228.84>, <23.192.228.80>, <23.215.0.138>, <23.215.0.136>]>]
# 2025-03-22 07:10:57,592 - INFO - HTTP Request: POST https://dns.google/dns-query "HTTP/1.1 200 OK"

DNS Query using DoT

import asyncio
from privydns import DNSResolver

async def main():
    resolver = DNSResolver()
    response = await resolver.query("example.com", protocol="dot")
    print(response)

asyncio.run(main())

# 2025-03-22 07:16:47,292 - INFO - Resolving example.com (A) using dot
# [<DNS example.com. IN A RRset: [<23.215.0.136>, <23.192.228.80>, <23.192.228.84>, <23.215.0.138>, <96.7.128.198>, <96.7.128.175>]>]

DNS Query using mTLS

import asyncio
from privydns import DNSResolver

async def main():
    resolver = DNSResolver(
        mtls_server="mtls.example.com",
        certfile="/path/to/cert.pem",
        keyfile="/path/to/key.pem"
    )
    response = await resolver.query("example.com", protocol="mtls")
    print(response)

asyncio.run(main())

# 2025-03-22 07:21:07,292 - INFO - Resolving example.com (A) using mtls
# [<DNS example.com. IN A RRset: [<23.215.0.136>, <23.192.228.80>, <23.192.228.84>, <23.215.0.138>, <96.7.128.198>, <96.7.128.175>]>

Testing

To run the tests, install the required packages and run the following command:

pip install -r requirements.txt
pytest .

Running Tests in Docker 🐳

This project includes a Dockerfile and docker-compose.yml file to run the tests inside a Docker container. It was build to mimic the GitHub Actions workflow.

To build the Docker image, run the following command:

docker compose build

To run the tests inside the Docker container, execute the following command:

docker compose up

Publishing to PyPI

To publish this project to PyPI, follow these steps:

  1. Install Required Tools Ensure you have the latest versions of setuptools, wheel, and twine installed.

    pip install --upgrade setuptools wheel twine build
    
  2. Build the Distribution Files Run the following command to build the source distribution (.tar.gz) and the wheel distribution (.whl).

    python -m build
    

    The distribution files will be available in the dist directory.

  3. Upload to Test PyPI (Optional) Before uploading to the official PyPI, you can test the package on Test PyPI:

    twine upload --repository testpypi dist/*
    

    You can verify your package at the Test PyPI URL (https://test.pypi.org/project/your-package-name).

  4. Upload to PyPI If everything looks good, upload the package to the official PyPI repository:

    twine upload dist/*
    

    Make sure you have your PyPI credentials ready—refer to the PyPI website documentation if needed.

  5. Verify the Package After publishing, verify that your package is listed on PyPI. You can install it like any other package to confirm the installation is working:

    pip install privydns
    
  6. Important Notes:

    • Ensure the setup.py file has all required metadata filled in (e.g., name, version, author, description, etc.).
    • Keep the version number updated for every new release.

License

This project is licensed under the MIT License—see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

privydns-0.1.3.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

privydns-0.1.3-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file privydns-0.1.3.tar.gz.

File metadata

  • Download URL: privydns-0.1.3.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.0

File hashes

Hashes for privydns-0.1.3.tar.gz
Algorithm Hash digest
SHA256 a314f25adc03fa3e1a7fc0f1d40702c21d43f0082ce81f038adfa2535e5a56fb
MD5 f61a24ffec6b1c25f4ca4a9aa25dae8f
BLAKE2b-256 ebf73bd1918a17869d780136119f44942c1392b612bac7137d20897c62d2668f

See more details on using hashes here.

File details

Details for the file privydns-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: privydns-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.0

File hashes

Hashes for privydns-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1c242c0139c9dbec3bdc0489e056fd6cddf5e4191f0a0566144ea586f06a953f
MD5 3ccb6286e10aa9489f3d29996fc16011
BLAKE2b-256 c4cf0f9850953a2e479d63a597fb8ffa03c67af5006b572c2bac2fff9fa58e45

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page