Skip to main content

async_dns

PyPI

This is the documentation for v2.x. Click here for 1.x documentation.

Features

  • Built with asyncio in pure Python, no third party dependency is required
  • Support DNS over UDP / TCP
  • Support DNS over HTTPS
  • Support DNS over TLS

Prerequisite

  • Python >=3.6

Installation

$ pip3 install async_dns
# or
$ pip3 install git+https://github.com/gera2ld/async_dns.git

CLI

Resolver

usage: python3 -m async_dns.resolver [-h] [-n NAMESERVERS [NAMESERVERS ...]] [-t TYPES [TYPES ...]]
                                     hostnames [hostnames ...]

Async DNS resolver

positional arguments:
  hostnames             the hostnames to query

optional arguments:
  -h, --help            show this help message and exit
  -n NAMESERVERS [NAMESERVERS ...], --nameservers NAMESERVERS [NAMESERVERS ...]
                        name servers
  -t TYPES [TYPES ...], --types TYPES [TYPES ...]
                        query types, default as `any`

Examples:

# Resolve an IP
$ python3 -m async_dns.resolver www.google.com
$ python3 -m async_dns.resolver -t mx -- gmail.com

# Query via TCP
$ python3 -m async_dns.resolver -n tcp://127.0.0.1 -- www.google.com

# Query via TLS
$ python3 -m async_dns.resolver -n tcps://dns.alidns.com -- www.google.com

# Query from non-standard ports
$ python3 -m async_dns.resolver -n udp://127.0.0.1:1053 -- www.google.com

# Query from HTTPS
$ python3 -m async_dns.resolver -n https://dns.alidns.com/dns-query -- www.google.com

Note: -- is required before hostnames if the previous option can have multiple arguments.

Server

usage: python3 -m async_dns.server [-h] [-b BIND] [--hosts HOSTS] [-x [PROXY [PROXY ...]]]

DNS server by Gerald.

optional arguments:
  -h, --help            show this help message and exit
  -b BIND, --bind BIND  the address for the server to bind
  --hosts HOSTS         the path of a hosts file, `none` to disable hosts, `local` to read from
                        local hosts file
  -x [PROXY [PROXY ...]], --proxy [PROXY [PROXY ...]]
                        the proxy DNS servers, `none` to serve as a recursive server, `default` to
                        proxy to default nameservers

Note: TLS and HTTPS are not supported in async_dns server. Consider async-doh for DoH server support.

Examples:

# Start a DNS proxy server on :53
$ python3 -m async_dns.server -b :53 --hosts /etc/hosts

# Start a DNS server over TCP proxy
$ python3 -m async_dns.server -x tcp://114.114.114.114

# Start a DNS recursive server
$ python3 -m async_dns.server -x none

API

import asyncio
from async_dns.core import types
from async_dns.resolver import ProxyResolver

resolver = ProxyResolver()
res, cached = asyncio.run(resolver.query('www.baidu.com', types.A))
print(res)

Client

The client sends a request to a remote server and returns the message directly. Unlike resolvers, client does not have a cache and does not modify the response.

import asyncio
from async_dns.core import types, Address
from async_dns.resolver import DNSClient

async def query():
    client = DNSClient()
    res = await client.query('www.google.com', types.A,
                             Address.parse('8.8.8.8'))
    print(res)
    print(res.aa)

asyncio.run(query())

Routing

ProxyResolver supports routing based on domains:

resolver = ProxyResolver(proxies=[
    ('*.lan', ['192.168.1.1']),                             # query 'udp://192.168.1.1:53' for '*.lan' domains
    (lambda d: d.endswith('.local'), ['tcp://127.0.0.1']),  # query tcp://127.0.0.1:53 for domains ending with '.local'
    '8.8.8.8',                                              # equivalent to (None, ['8.8.8.8']), matches all others
])

DoH support

This library contains a simple implementation of DoH (aka DNS over HTTPS) client with partial HTTP protocol implemented.

If you need a more powerful DoH client based on aiohttp, or a DoH server, consider async-doh.

DNS Spoofing

You can easily add records to the cache with a hosts file or the cache API.

  • Start a server with a custom hosts file:

    $ python3 -m async_dns.server -b :53 --hosts /path/to/custom/hosts
    
  • Add some additional records to a resolver:

    from async_dns.core import parse_hosts_file, types
    
    for name, qtype, data in parse_hosts_file(hosts):
        resolver.cache.add(name, qtype, data)
    
    resolver.cache.add('www.example.com', types.A, ['127.0.0.1'])
    

Test

$ python3 -m unittest

# Or with tox
$ tox -e py

Logging

Logging does not work out of the box in v2. It requires at least minimal logging configuration.

logging.basicConfig(level=logging.INFO)

You can also add a formatter for the logger:

import logging
from async_dns.core import logger

logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
fmt = logging.Formatter('%(asctime)s %(levelname)s: %(message)s')
handler.setFormatter(fmt)
logger.addHandler(handler)

References

Download files

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

Source Distribution

async_dns-2.0.1.tar.gz (25.3 kB view details)

Uploaded Source

Built Distribution

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

async_dns-2.0.1-py3-none-any.whl (34.5 kB view details)

Uploaded Python 3

File details

Details for the file async_dns-2.0.1.tar.gz.

File metadata

  • Download URL: async_dns-2.0.1.tar.gz
  • Upload date:
  • Size: 25.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for async_dns-2.0.1.tar.gz
Algorithm Hash digest
SHA256 4067352e80000881a959f1ac9d3764cef16fdeb44beb356cdb4ec690f8c45f48
MD5 2bcacd5ff60518d752e39d132d2f9b8d
BLAKE2b-256 b656ee4e4848f9482306bcae2af10b3713c599ad441a7468dbaf35affc053a4e

See more details on using hashes here.

Provenance

The following attestation bundles were made for async_dns-2.0.1.tar.gz:

Publisher: publish.yml on gera2ld/async_dns

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file async_dns-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: async_dns-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 34.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for async_dns-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c16634333ebf83d0d61d28b386466f0ed07332eb17f4a071077faca1960bab4a
MD5 a16492f06cf74769998e1c3de5d9bf3d
BLAKE2b-256 6f6c8499a42b6bb2d184b1d1288d8d16b38df2292f6fa4896d58e81b6ea616e1

See more details on using hashes here.

Provenance

The following attestation bundles were made for async_dns-2.0.1-py3-none-any.whl:

Publisher: publish.yml on gera2ld/async_dns

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

2.0.1 This release

2 files

2.0.0

2 files

1.1.10

2 files

1.1.9

2 files

1.1.8

2 files

1.1.7

2 files

1.1.6

2 files

1.1.5

2 files

1.1.4

2 files

1.1.3

2 files

1.1.2

2 files

1.1.1

2 files

1.1.0

2 files

1.0.11

2 files

1.0.10

2 files

1.0.9

2 files

1.0.8

2 files

1.0.7

2 files

1.0.6

1 file

1.0.5

1 file

1.0.4

1 file

1.0.3

1 file

1.0.2

1 file

1.0.1

1 file

1.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page