Python实现DNS转发

Python实现DNS转发


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import socket, SocketServer

class MyUDPHandler(SocketServer.BaseRequestHandler):

    def handle(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        socket.setdefaulttimeout(3)
        sock.sendto(self.request[0], ('223.5.5.5', 53))
        result = sock.recv(512)
        sock.close()
        self.request[1].sendto(result, self.client_address)

if __name__ == "__main__":
    server = SocketServer.UDPServer(("0.0.0.0", 53), MyUDPHandler)
    server.serve_forever()