windows Python DNS 服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3774919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python DNS Server
提问by Zac Brown
I am adding a feature to my current project that will allow network admins to install the software to the network. I need to code a DNS server in Python that will allow me to redirect to a certain page if the request address is in my list. I was able to write the server, just not sure how to redirect.
我正在向我当前的项目添加一项功能,该功能将允许网络管理员将软件安装到网络。我需要用 Python 编写一个 DNS 服务器,如果请求地址在我的列表中,它将允许我重定向到某个页面。我能够编写服务器,只是不确定如何重定向。
Thank you. I am using Python 2.6 on Windows XP.
谢谢你。我在 Windows XP 上使用 Python 2.6。
回答by Alex Martelli
There's little, simple example herethat can easily be adapted to make all kinds of "mini fake dns servers". Note that absolutely no "redirect" is involved (that's nothow DNS works): rather, the request is for a domain name, and the result of that request is an IP address. If what you want to do is drastically different from translating names to addresses, then maybe what you need is notactually a DNS server...?
还有一点,简单的例子,在这里,可以很容易地适应做出各种“小假的DNS服务器”。请注意,绝对不涉及“重定向”(这不是DNS 的工作方式):相反,请求是针对域名的,而该请求的结果是IP 地址。如果您想要做的与将名称转换为地址完全不同,那么也许您需要的实际上并不是DNS 服务器......?
回答by James Mills
Using circuitsand dnslibhere's a full recursive dns server written in Python in only 143 lines of code:
使用电路和dnslib,这里是一个完整的递归 dns 服务器,用 Python 编写,仅用 143 行代码:
#!/usr/bin/env python
from __future__ import print_function
from uuid import uuid4 as uuid
from dnslib import CLASS, QR, QTYPE
from dnslib import DNSHeader, DNSQuestion, DNSRecord
from circuits.net.events import write
from circuits import Component, Debugger, Event
from circuits.net.sockets import UDPClient, UDPServer
class lookup(Event):
"""lookup Event"""
class query(Event):
"""query Event"""
class response(Event):
"""response Event"""
class DNS(Component):
def read(self, peer, data):
record = DNSRecord.parse(data)
if record.header.qr == QR["QUERY"]:
return self.fire(query(peer, record))
return self.fire(response(peer, record))
class ReturnResponse(Component):
def response(self, peer, response):
return response
class Client(Component):
channel = "client"
def init(self, server, port, channel=channel):
self.server = server
self.port = int(port)
self.transport = UDPClient(0, channel=self.channel).register(self)
self.protocol = DNS(channel=self.channel).register(self)
self.handler = ReturnResponse(channel=self.channel).register(self)
class Resolver(Component):
def init(self, server, port):
self.server = server
self.port = port
def lookup(self, qname, qclass="IN", qtype="A"):
channel = uuid()
client = Client(
self.server,
self.port,
channel=channel
).register(self)
yield self.wait("ready", channel)
self.fire(
write(
(self.server, self.port),
DNSRecord(
q=DNSQuestion(
qname,
qclass=CLASS[qclass],
qtype=QTYPE[qtype]
)
).pack()
)
)
yield (yield self.wait("response", channel))
client.unregister()
yield self.wait("unregistered", channel)
del client
class ProcessQuery(Component):
def query(self, peer, query):
qname = query.q.qname
qtype = QTYPE[query.q.qtype]
qclass = CLASS[query.q.qclass]
response = yield self.call(lookup(qname, qclass=qclass, qtype=qtype))
record = DNSRecord(
DNSHeader(id=query.header.id, qr=1, aa=1, ra=1),
q=query.q,
)
for rr in response.value.rr:
record.add_answer(rr)
yield record.pack()
class Server(Component):
def init(self, bind=("0.0.0.0", 53)):
self.bind = bind
self.transport = UDPServer(self.bind).register(self)
self.protocol = DNS().register(self)
self.handler = ProcessQuery().register(self)
class App(Component):
def init(self, bind=("0.0.0.0", 53), server="8.8.8.8", port=53,
verbose=False):
if verbose:
Debugger().register(self)
self.resolver = Resolver(server, port).register(self)
self.server = Server(bind).register(self)
def main():
App().run()
if __name__ == "__main__":
main()
Usage:
用法:
By default this example binds go 0.0.0.0:53
so you will need to do something like:
默认情况下,此示例绑定 go,0.0.0.0:53
因此您需要执行以下操作:
sudo ./dnsserver.py
Otherwise change the bind
parameter.
否则更改bind
参数。
回答by Sam Liao
Here is a dns serer/proxy that works for me written in python:
这是一个用 python 编写的对我有用的 dns serer/proxy:
回答by akarca
I wrote a DNS Server using Python Twistedlibrary for NameOcean.net. You can see examples on https://twistedmatrix.com/documents/16.5.0/names/howto/custom-server.html.
我使用Python Twisted库为NameOcean.net编写了一个 DNS 服务器。您可以在https://twistedmatrix.com/documents/16.5.0/names/howto/custom-server.html上查看示例。