北邮计算机网络课程设计DNS服务器

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

北邮计算机网络课程设计DNS服务器

1 环境

操作系统随意,语言python2.7

2.文件

Socket.py 说明完成缓存检测,确定是回应,还是转发,如果是回应,确定是本地回应还是外部应答转发

import socket

import mydic

import charhandle

import makeframe

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

port = 53

the_dic = mydic.get_web_ip()

client_request = {}

client_request_index = {}

reverse_map = {}

key_record = 0

client_wait = []

s.bind(('',port))

time_rest = 0

request_general = ''

print 'running'

while True:

try:

msg , (client,port) = s.recvfrom(1024)

except:

print 'Time out! '

continue

request = []

request = list(msg)

requre_web = charhandle.get_request(request[12:])

website = ''.join(requre_web)

if(port == 53):

answer = []

answer = list(msg)

print "Type:Remote Response"

print "remote answer is: "

response_ip = msg[-4]+msg[-3]+msg[-2]+msg[-1]

char_ip = socket.inet_ntoa(response_ip)

print website + ' has the ip : '+char_ip

fre = mydic.storeForUpdate(website,char_ip )

print 'with the frequence of '+ str(fre)

###real_request = client_request[request[0]+request[1]]

for each_client in client_wait:

my_key = client_request[request[0]+request[1]+str(each_client)]

if client_request_index.get(my_key) != None :

s.sendto(msg,client_request_index[my_key])

print "Response to ip and Client port:"

print client_request_index[my_key]

break

else:

print "Type: Client Request"

print "ip and port:"

print (client,port)

### requre_web = charhandle.get_request(request[12:])

### website = ''.join(requre_web)

print "Request website:"+website

if(the_dic.get(website) != None):

print "Found in local cache:"

re_ip = the_dic.get(website)

print re_ip

fre = mydic.storeForUpdate(website)

print re_ip[0]+' with frequence '+str(fre)

zhen = makeframe.make(re_ip[0],msg)

s.sendto(zhen,(client,port))

else:

print "need to ask remote server"

key_record = key_record + 1

request_general = key_record

client_request[request[0]+request[1]+str(client)] = request_general

client_request_index[request_general] = (client,port)

if client not in client_wait:

client_wait.append(client)

# select authority DNS server as you wish

s.sendto(msg,('10.3.9.4',53))

time_rest = time_rest+1

try:

if(time_rest == 50):

print 'pay attention'

print '######################'

mydic.updateCache()

print '######################'

the_dic = mydic.get_web_ip()

time_rest = 0

except:

print 'not valid frequence'

print '--------------------'

s.close()

Mydic.py 完成本地查询,LRU算法维护本地缓存,socket.py第97行的time_request值可以确定更新缓存的频率,可以直接输出在dnsrelay.txt内,但是为了方便观测变化,我输出到了dnsrelaycopy.txt里面,运行一段时间之后,可以去看看学习到的新条目

import pickle

import os

import threading

A=[]

d_web_ip = {}

d_ip_web = {}

相关文档
最新文档