开发者

How do I make a simple DNS server listening two ports simultaneously in python

I'm trying to build a DNS server in python. It must listen two ports (8007 - client, 8008 - admin). The client only send an URL and receives the respective IP. The admin has permissions to change the DNS table (add, remove,.. doesn't matter to this right now).

So my question is: how do I implement the server listening continuously on the two ports for any eventual request (we can have several clients at the same time but only one admin when he is operating)

my Server with one listening port:

from SocketServer import * from threading import * from string import * import socket

    class Server(ForkingMixIn, TCPServer): pass  #fork for each client

    class Handler(StreamRequestHandler):

        def handle(self):
            addr = self.request.getpeername()
            print 'Got conne开发者_运维技巧ction from', addr
            data=(self.request.recv(1024)).strip()

            if data not in dic: #dic -> dictionary with URL:IP
                self.wfile.write('0.0.0.0')
            else:
                self.wfile.write(dic.get(data))


    server = Server(('', 8007), Handler)
    server.serve_forever()


No need to use threads.

Use twisted.

TwistedNames has support out of the box for a dns server. You can customize it as needed or read its source as base when you build yours.


You can use non-blocking sockets, and use the select call to read from the socket. This Sockets Programming HOWTO for Python article has a section on non-blocking sockets in Python that will help.

See also:

  • select (Python) | select (UNIX)
  • socket (Python) | socket (UNIX)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜