1、

#!/user/bin/python
#-*- coding: utf-8 -*-
import socket

class DNSQuery:
def __init__ (self, data):
    self.data=data
    self.dominio=''
    tipo = (ord(data[2]) >> 3) & 15 # Opcode bits
    if tipo == 0: # Standard query
      ini=12
      lon=ord(data[ini])
      while lon != 0:
        self.dominio+=data[ini+1:ini+lon+1]+'.'
        ini+=lon+1
        lon=ord(data[ini])

def respuesta(self, ip):
    packet=''
    if self.dominio:
      packet+=self.data[:2] + "\x81\x80"
      packet+=self.data[4:6] + self.data[4:6] + '\x00\x00\x00\x00' # Questions and Answers Counts
      packet+=self.data[12:] # Original Domain Name Question
      packet+='\xc0\x0c' # Pointer to domain name
      packet+='\x00\x01\x00\x01\x00\x00\x00\x3c\x00\x04' # Response type, ttl and resource data length -> 4 bytes
      packet+=str.join('',map(lambda x: chr(int(x)), ip.split('.'))) # 4bytes of IP
    return packet

if __name__ == ' __main__':
udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udps.bind(('',53))
try:
    while 1:
      try:
        data, addr = udps.recvfrom(1024)
        p=DNSQuery(data)
        ip=socket.gethostbyname(p.dominio)
      except socket.error, e:
          continue
          print "Error Socket.error %s" % e
      udps.sendto(p.respuesta(ip), addr)
      print 'Respuesta: %s -> %s' % (p.dominio, ip)
except KeyboardInterrupt:
    print 'Finalizando'
    udps.close()

2、

#!/user/bin/python
#-*- coding: utf-8 -*-
import socket
import threading
class Prce(threading.Thread):
    def __init__ (self,data , addr, sock):
        threading.Thread. __init__ (self)
        self.data = data
        self.addr = addr
        self.sock = sock
    def run(self):
        p = DNSQuery(self.data)
        ip = socket.gethostbyname(p.dominio)
        self.sock.sendto(p.respuesta(ip), self.addr)
        print 'Respuesta: %s -> %s' % (p.dominio, ip)
class DNSQuery:
def __init__ (self, data):
    self.data=data
    self.dominio=''
    tipo = (ord(data[2]) >> 3) & 15 # Opcode bits
    if tipo == 0: # Standard query
      ini=12
      lon=ord(data[ini])
      while lon != 0:
        self.dominio+=data[ini+1:ini+lon+1]+'.'
        ini+=lon+1
        lon=ord(data[ini])

def respuesta(self, ip):
    packet=''
    if self.dominio:
      packet+=self.data[:2] + "\x81\x80"
      packet+=self.data[4:6] + self.data[4:6] + '\x00\x00\x00\x00' # Questions and Answers Counts
      packet+=self.data[12:] # Original Domain Name Question
      packet+='\xc0\x0c' # Pointer to domain name
      packet+='\x00\x01\x00\x01\x00\x00\x00\x3c\x00\x04' # Response type, ttl and resource data length -> 4 bytes
      packet+=str.join('',map(lambda x: chr(int(x)), ip.split('.'))) # 4bytes of IP
    return packet

if __name__ == ' __main__':
udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udps.bind(('',53))
try:
    while 1:
      try:
        data, addr = udps.recvfrom(1024)
        tmp = Prce(data,addr,udps)
        tmp.start()
        tmp.join()
      except socket.error, e:
          continue
except error:
    print 'Finalizando'
    udps.close()
udps.close()