我将Windows计算机的DNS服务器配置为127.0.0.1,并在localhost上创建了一个基本的python server:

from socket import *
serverPort = 53
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind(('127.0.0.1', serverPort))
print "The server is ready to receive on port: {}".format(serverPort)
while 1:
    try:
        message, clientAddress = serverSocket.recvfrom(512)
    except:
        continue
    print clientAddress, message
    modifiedMessage = "127.0.0.1"
    serverSocket.sendto(modifiedMessage, clientAddress)

PS :我知道DNS是一个二进制协议(protocol),发送ASCII文本不会有任何好处,但是我不是想做一个解析器,而是想以透明的方式看一下前者是如何工作的。

当我出售服务器时,我对以下输出感到满意:
(('127.0.0.1', 53945), '.\x9c\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x03com\x00\x00\x01\x00\x01')
(('127.0.0.1', 53945), '.\x9c\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x03com\x00\x00\x01\x00\x01')
(('127.0.0.1', 53945), '.\x9c\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x03com\x00\x00\x01\x00\x01')
(('127.0.0.1', 61362), '\xefc\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x03com\x00\x00\x01\x00\x01')
(('127.0.0.1', 50065), '\xb5\xfc\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x06google\x03com\x00\x00\x01\x00\x01')
(('127.0.0.1', 61362), '\xefc\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x03com\x00\x00\x01\x00\x01')
(('127.0.0.1', 61362), '\xefc\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x03com\x00\x00\x01\x00\x01')
(('127.0.0.1', 52718), '\xc7\x15\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x05tiles\x08services\x07mozilla\x03com\x00\x00\x01\x00\x01')

但是与我所期望的不同,我仍然能够打开网站。 Wireshark告诉我,我正在连接到8.8.8.8(IDK如何?)。
我尝试从我的机​​器nada中清除DNS Cashe。
我想念什么?

PPS:如果删除try/catch子句,则会出现此错误(在程序执行后几秒钟):
error: [Errno 10054] An existing connection was forcibly closed by the remote host

最佳答案

您可能已将Google的8.8.8.8配置为后备DNS服务器。

而且,由于您要销毁DNS答案,因此,收到这些错误答案的任何人都将退回到辅助服务器。在典型的UN * X机器上,DNS查询的整个路径非常复杂,整个系统通常非常健壮。

09-08 02:21
查看更多