Commits

paulc committed 6622f8d

Back-out Python3 patch (failed tests) & fix bit.py doctests

  • Participants
  • Parent commits d5b66e3
  • Tags 0.8.3

Comments (0)

Files changed (8)

dnslib/__init__.py

-from dnslib.dns import *
+
+from dns import *
     """
         Print hexdump of string
 
-        >>> print hexdump("abcd\x00" * 4)
-        0000  61 62 63 64 00 61 62 63  64 00 61 62 63 64 00 61  abcd.abc d.abcd.a
-        0010  62 63 64 00                                       bcd. 
+        >>> print hexdump("abc\xff" * 4)
+        0000  61 62 63 ff 61 62 63 ff  61 62 63 ff 61 62 63 ff  abc.abc. abc.abc.
+
     """
     n = 0
     left = length / 2 
     """
         Get specified bits from integer
 
-        >>> bin(get_bits(0b0011100,2)
-        0b1
+        >>> bin(get_bits(0b0011100,2))
+        '0b1'
         >>> bin(get_bits(0b0011100,0,4))
-        0b1100
+        '0b1100'
         
     """
     mask = ((1 << bits) - 1) << offset
         Set specified bits in integer
 
         >>> bin(set_bits(0,0b1010,0,4))
-        0b1010
+        '0b1010'
         >>> bin(set_bits(0,0b1010,3,4))
-        0b1010000
+        '0b1010000'
     """
     mask = ((1 << bits) - 1) << offset
     clear = 0xffff ^ mask
         optionally be reversed
 
         >>> binary(6789)
-        0001101010000101
+        '0001101010000101'
         >>> binary(6789,8)
-        10000101
+        '10000101'
         >>> binary(6789,reverse=True)
-        1010000101011000
+        '1010000101011000'
 
     """
     bits = [str((n >> y) & 1) for y in range(count-1, -1, -1)]
         bits.reverse()
     return "".join(bits)
 
+if __name__ == '__main__':
+    import doctest
+    doctest.testmod()
+
 
 import random,socket,struct 
 
-from dnslib.bit import get_bits, set_bits
-from dnslib.bimap import Bimap
-from dnslib.buffer import Buffer
-from dnslib.label import DNSLabel, DNSLabelError, DNSBuffer
+from bit import get_bits,set_bits
+from bimap import Bimap
+from buffer import Buffer
+from label import DNSLabel,DNSLabelError,DNSBuffer
 
 QTYPE =  Bimap({1:'A', 2:'NS', 5:'CNAME', 6:'SOA', 12:'PTR', 15:'MX',
                 16:'TXT', 17:'RP', 18:'AFSDB', 24:'SIG', 25:'KEY',
 
 import types
-from dnslib.bit import get_bits, set_bits
-from dnslib.buffer import Buffer
+from bit import get_bits,set_bits
+from buffer import Buffer
 
 class DNSLabelError(Exception):
     pass

dnslib/server/circuits_server.py

 #!/usr/bin/env python
-from __future__ import print_function
 
 from circuits.net.sockets import UDPServer
 
         id = request.header.id
         qname = request.q.qname
         qtype = request.q.qtype
-        print("------ Request (%s): %r (%s)" % (str(sock),
-              qname.label, QTYPE[qtype]))
+        print "------ Request (%s): %r (%s)" % (str(sock),
+                qname.label, QTYPE[qtype])
 
         reply = DNSRecord(DNSHeader(id=id, qr=1, aa=1, ra=1), q=request.q)
 

dnslib/server/gevent_server.py

 #!/usr/bin/env python
-from __future__ import print_function
 
 import gevent
 from gevent import socket
     id = request.header.id
     qname = request.q.qname
     qtype = request.q.qtype
-    print("------ Request (%s): %r (%s)" % (str(peer),
-          qname.label, QTYPE[qtype]))
-    print(request)
+    print "------ Request (%s): %r (%s)" % (str(peer),
+            qname.label, QTYPE[qtype])
+    print request
 
     reply = DNSRecord(DNSHeader(id=id, qr=1, aa=1, ra=1), q=request.q)
     if qtype == QTYPE.A:
     else:
         reply.add_answer(RR(qname, QTYPE.CNAME, rdata=CNAME(MSG)))
 
-    print("------ Reply")
-    print(reply)
+    print "------ Reply"
+    print reply 
 
     s.sendto(reply.pack(), peer)
 

dnslib/server/udp_proxy.py

 #!/usr/bin/env python
-from __future__ import print_function
 
 import optparse,socket,sys
 from dnslib import DNSHeader, DNSRecord, QTYPE
 parser.add_option("--bind",default="127.0.0.1",help="Proxy bind address (default: 127.0.0.1)")
 parser.add_option("--dns",default="8.8.8.8",help="DNS server (default: 8.8.8.8)")
 parser.add_option("--dns_port",type=int,default=53,help="DNS server port (default: 53)")
-options, args = parser.parse_args()
+options,args = parser.parse_args()
 
 proxy = socket.socket(AF_INET, SOCK_DGRAM)
 proxy.bind((options.bind,options.port))
 
 while True:
     # Wait for client connection
-    data, client = proxy.recvfrom(8192)
+    data,client = proxy.recvfrom(8192)
     # Parse and print request
     request = DNSRecord.parse(data)
     id = request.header.id
     qname = request.q.qname
     qtype = request.q.qtype
-    print("------ Request (%s): %r (%s)" % (str(client), qname.label, QTYPE[qtype]))
-    print(data.encode('hex'))
-    print("\n".join([ "  %s" % l for l in str(request).split("\n")]))
+    print "------ Request (%s): %r (%s)" % (str(client),qname.label,QTYPE[qtype])
+    print data.encode('hex')
+    print "\n".join([ "  %s" % l for l in str(request).split("\n")])
     # Send request to server
     s = socket.socket(AF_INET, SOCK_DGRAM)
     s.sendto(data,(options.dns,options.dns_port))
     # Wait for reply
-    data, server = s.recvfrom(8192)
+    data,server = s.recvfrom(8192)
     # Parse and print reply
     reply = DNSRecord.parse(data)
     id = reply.header.id
     qname = reply.q.qname
     qtype = reply.q.qtype
-    print("------ Reply (%s): %r (%s)" % (str(server), qname.label, QTYPE[qtype]))
-    print(data.encode('hex'))
-    print("\n".join(["  %s" % l for l in str(reply).split("\n")]))
-    print()
+    print "------ Reply (%s): %r (%s)" % (str(server),qname.label,QTYPE[qtype])
+    print data.encode('hex')
+    print "\n".join([ "  %s" % l for l in str(reply).split("\n")])
+    print
     # Send reply to client
-    proxy.sendto(data, client)
+    proxy.sendto(data,client)
 
 

dnslib/server/udp_server.py

 #!/usr/bin/env python
-from __future__ import print_function
 
 import socket
 
     id = request.header.id
     qname = request.q.qname
     qtype = request.q.qtype
-    print("------ Request (%s): %r (%s)" % (str(peer),
-          qname.label, QTYPE[qtype]))
-    print("\n".join([ "  %s" % l for l in str(request).split("\n")]))
+    print "------ Request (%s): %r (%s)" % (str(peer),
+            qname.label, QTYPE[qtype])
+    print "\n".join([ "  %s" % l for l in str(request).split("\n")])
 
     reply = DNSRecord(DNSHeader(id=id, qr=1, aa=1, ra=1), q=request.q)
     if qtype == QTYPE.A:
     else:
         reply.add_answer(RR(qname, QTYPE.CNAME, rdata=CNAME(MSG)))
 
-    print("------ Reply")
-    print("\n".join(["  %s" % l for l in str(reply).split("\n")]))
+    print "------ Reply"
+    print "\n".join([ "  %s" % l for l in str(reply).split("\n")])
 
     s.sendto(reply.pack(), peer)
 
 s.bind(('', 53))
 
 while True:
-    print("====== Waiting for connection")
+    print "====== Waiting for connection"
     data, peer = s.recvfrom(8192)
-    dns_handler(s, peer, data)
+    dns_handler(s,peer,data)