Skip to content

Instantly share code, notes, and snippets.

@nsuan
Created October 1, 2009 13:12
Show Gist options
  • Save nsuan/198953 to your computer and use it in GitHub Desktop.
Save nsuan/198953 to your computer and use it in GitHub Desktop.
import socket, select
import threading, Queue, random
#import _mysql as mysql
import time
UDP_IP="0.0.0.0"
UDP_IP6 = "::"
UDP_PORT=53
WORKERS=100
RESOLVERADDRESGOESHERE=
class dnsWorker(threading.Thread):
def __init__(self, queue):
self.__queue = queue
threading.Thread.__init__(self)
self.setDaemon(True)
#self.ident = i
def run(self):
while True:
try:
data, addr, serverSocket = self.__queue.get()
startTime = time.time()
#msg = message.from_wire(data) #make method to parse request attributes
s = socket.socket( socket.AF_INET, socket.SOCK_DGRAM ) # UDP
s.sendto(data, (RESOLVERADDREGOESHERE, 53))
buf = s.recvfrom(2048)[0]
serverSocket.sendto(buf, addr)
s.close()
#reply = message.make_response(msg)
#rdtype = reply.question[0].rdtype
#print time.time() - startTime
except KeyboardInterrupt:
raise
except:
pass
def main():
global serverSocket
#start listening
v4Socket = socket.socket( socket.AF_INET, # Internet
socket.SOCK_DGRAM ) # UDP
v4Socket.bind( (UDP_IP,UDP_PORT) )
v6Socket = socket.socket( socket.AF_INET6, # Internet
socket.SOCK_DGRAM ) # UDP
v6Socket.bind( (UDP_IP6,UDP_PORT) )
#add the queue
queue = Queue.Queue()
input = [v6Socket, v4Socket]
#launch a lot of workers
for i in range(WORKERS):
dnsWorker(queue).start()
#aaand now we loop
while True:
try:
readSockets,outputready,exceptready = select.select(input,[],[])
for serverSocket in readSockets:
data, addr = serverSocket.recvfrom(2048) # buffer size is 1024 bytes
#print "received message:", data
packet = (data, addr, serverSocket)
queue.put(packet)
except KeyboardInterrupt:
raise
except:
pass
if(__name__ == "__main__"):
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment