File: dnsclient.py

package info (click to toggle)
circuits 3.2.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,980 kB
  • sloc: python: 17,583; javascript: 3,226; makefile: 100
file content (94 lines) | stat: -rwxr-xr-x 2,228 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python
"""
DNS Client Example

A simple little DNS Client example using
`dnslib <https://pypi.python.org/pypi/dnslib>`_
to handle the DNS protocol parsing and packet
deconstruction (*a really nice library btw with
great integration into circuits*).

Specify the server, port, and query as arguments
to perform a lookup against a server using UDP.

To run this example::

    pip install dnslib
    ./dnsclient.py 8.8.8.8 53 google.com
"""

import sys

from dnslib import DNSQuestion, DNSRecord

from circuits import Component, Debugger, Event
from circuits.net.events import write
from circuits.net.sockets import UDPClient


class reply(Event):
    """reply Event"""


class DNS(Component):
    """DNS Protocol Handling"""

    def read(self, peer, data):
        self.fire(reply(peer, DNSRecord.parse(data)))


class Dummy(Component):
    """
    A Dummy DNS Handler

    This just parses the reply packet and
    prints any RR records it finds.
    """

    def reply(self, peer, response):
        id = response.header.id
        qname = response.q.qname

        print(
            f'DNS Response from {peer[0]:s}:{peer[1]:d} id={id:d} qname={str(qname):s}',
            file=sys.stderr,
        )

        for rr in response.rr:
            print(f' {rr!s:s}')

        raise SystemExit(0)


class DNSClient(Component):
    """
    DNS Client

    This ties everything together in a nice
    configurable way with protocol, transport,
    and dummy handler as well as optional debugger.
    """

    def init(self, server, port, query, verbose=False):
        self.server = server
        self.port = int(port)
        self.query = query

        if verbose:
            Debugger().register(self)

        self.transport = UDPClient(0).register(self)
        self.protocol = DNS().register(self)
        self.dummy = Dummy().register(self)

    def started(self, manager):
        print('DNS Client Started!', file=sys.stderr)

    def ready(self, client, bind):
        print('Ready! Bound to {:s}:{:d}'.format(*bind), file=sys.stderr)

        request = DNSRecord(q=DNSQuestion(self.query))
        self.fire(write((self.server, self.port), request.pack()))


DNSClient(*sys.argv[1:], verbose=True).run()