~dustin-spy/twisted/dustin

« back to all changes in this revision

Viewing changes to doc/examples/dnstest.py

  • Committer: moshez
  • Date: 2001-09-23 01:44:48 UTC
  • Revision ID: vcs-imports@canonical.com-20010923014448-23d90217b748f28d
First prototype of twisted.names
* Changed udp.Port to simulate connections
  + We need to get rid of .dup() at some point
  + Added UDP into regular echoserv, to show how easy it is
* Added protocol implementation
  + Two different ones, UDP and TCP are not exactly the same
* Added twisted.names
  + Manages resolving

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
# Twisted, the Framework of Your Internet
 
3
# Copyright (C) 2001 Matthew W. Lefkowitz
 
4
 
5
# This library is free software; you can redistribute it and/or
 
6
# modify it under the terms of version 2.1 of the GNU Lesser General Public
 
7
# License as published by the Free Software Foundation.
 
8
 
9
# This library is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
# Lesser General Public License for more details.
 
13
 
14
# You should have received a copy of the GNU Lesser General Public
 
15
# License along with this library; if not, write to the Free Software
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 
 
18
from twisted.protocols import dns
 
19
from twisted.internet import main
 
20
 
 
21
def printMessage( message ):
 
22
    print "ID:", message.id
 
23
    print "IsAnswer:", message.answer
 
24
    print "rCode:", message.rCode
 
25
    print "Queries:"
 
26
    for q in message.queries:
 
27
        print q.name.name
 
28
    for list, title in ( ( message.answers, "Answers:" ),
 
29
                         ( message.ns, "NS:" ),
 
30
                         ( message.add, "Additional records:" ) ):
 
31
        print title
 
32
        for rr in list:
 
33
            print rr.name,
 
34
            for i in range( len( rr.data ) ):
 
35
                print ord( rr.data[ i ] ),
 
36
            print
 
37
 
 
38
 
 
39
class X:
 
40
    def __init__( self ):
 
41
        self.n = 0
 
42
        self.boss = dns.DNSBoss()
 
43
 
 
44
    def handleAnswer( self, message ):
 
45
        printMessage( message )
 
46
        self.n = self.n + 1
 
47
        print "N =", self.n
 
48
        if self.n == 2:
 
49
            self.boss.stopReadingBoth()
 
50
            main.shutDown()
 
51
 
 
52
x = X()
 
53
 
 
54
x.boss.queryUDP( ( "129.199.129.1", 53 ),
 
55
                 "clipper.ens.fr",
 
56
                 x.handleAnswer )
 
57
 
 
58
x.boss.queryTCP( ( "129.199.129.1", 53 ),
 
59
                 "clipper.ens.fr",
 
60
                 x.handleAnswer )
 
61
 
 
62
main.run()