北邮计算机网络课程设计DNS服务器doc.docx
《北邮计算机网络课程设计DNS服务器doc.docx》由会员分享,可在线阅读,更多相关《北邮计算机网络课程设计DNS服务器doc.docx(8页珍藏版)》请在冰豆网上搜索。
北邮计算机网络课程设计DNS服务器doc
北邮计算机网络课程设计DNS服务器
1环境
操作系统随意,语言python2.7
2.文件
Socket.py说明完成缓存检测,确定是回应,还是转发,如果是回应,确定是本地回应还是外部应答转发
importsocket
importmydic
importcharhandle
importmakeframe
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'
whileTrue:
try:
msg,(client,port=s.recvfrom(1024
except:
print'Timeout!
'
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:
RemoteResponse"
print"remoteansweris:
"
response_ip=msg[-4]+msg[-3]+msg[-2]+msg[-1]
char_ip=socket.inet_ntoa(response_ip
printwebsite+'hastheip:
'+char_ip
fre=mydic.storeForUpdate(website,char_ip
print'withthefrequenceof'+str(fre
###real_request=client_request[request[0]+request[1]]
foreach_clientinclient_wait:
my_key=client_request[request[0]+request[1]+str(each_client]ifclient_request_index.get(my_key!
=None:
s.sendto(msg,client_request_index[my_key]
print"ResponsetoipandClientport:
"
printclient_request_index[my_key]
break
else:
print"Type:
ClientRequest"
print"ipandport:
"
print(client,port
###requre_web=charhandle.get_request(request[12:
]
###website=''.join(requre_web
print"Requestwebsite:
"+website
if(the_dic.get(website!
=None:
print"Foundinlocalcache:
"
re_ip=the_dic.get(website
printre_ip
fre=mydic.storeForUpdate(website
printre_ip[0]+'withfrequence'+str(fre
zhen=makeframe.make(re_ip[0],msg
s.sendto(zhen,(client,port
else:
print"needtoaskremoteserver"
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
ifclientnotinclient_wait:
client_wait.append(client
#selectauthorityDNSserverasyouwish
s.sendto(msg,('10.3.9.4',53
time_rest=time_rest+1
try:
if(time_rest==50:
print'payattention'
print'######################'
mydic.updateCache(
print'######################'
the_dic=mydic.get_web_ip(
time_rest=0
except:
print'notvalidfrequence'
print'--------------------'
s.close(
Mydic.py完成本地查询,LRU算法维护本地缓存,socket.py第97行的time_request值可以确定更新缓存的频率,可以直接输出在dnsrelay.txt内,但是为了方便观测变化,我输出到了dnsrelaycopy.txt里面,运行一段时间之后,可以去看看学习到的新条目
importpickle
importos
importthreading
A=[]
d_web_ip={}
d_ip_web={}
update_dic={}
data=open('dnsrelay.txt'
foreach_lineindata:
try:
(ip,sitecopy=each_line.split('',1
(site,nothing=str(sitecopy.split('\n',1
d_web_ip[site]=[ip,1]
d_ip_web[ip]=site
except:
print'fileerror'
data.close(
try:
withopen('newdnsrelay.pickle','wb'asnewdnsrelay_file:
pickle.dump(d_web_ip,newdnsrelay_file
exceptIOErroraserr:
print'Fileerror:
'+str(err
exceptpickle.PickleErrorasperr:
print'Picklingerror:
'+str(perr
defget_web_ip(:
withopen('newdnsrelay.pickle','rb'asf:
globalupdate_dic
update_dic=pickle.load(f
returnupdate_dic.copy(
return(None
defstoreForUpdate(web_site,add=None:
globalupdate_dic
if(update_dic.get(web_site!
=None:
add_frequen=update_dic[web_site]
add_frequen[1]=add_frequen[1]+1
printweb_site+'frequenceincrase1,withip'+add_frequen[0]returnadd_frequen[1]
else:
update_dic[web_site]=[add,1]
print'recordforanewsite'
return1
defupdateCache(:
globalupdate_dic
m=update_dic.copy(
t=threading.Thread(target=my_thread,kwargs=mprint'readytoupdatelocalcache'
t.start(
t.join(
defmy_thread(*argu,**arg:
frequence=[]
remain_dic={}
foreach_keyinarg:
tmp=arg[each_key]
if(tmp[1]notinfrequence:
frequence.append(tmp[1]
if(tmp[0]=='0.0.0.0':
remain_dic[each_key]='0.0.0.0'
print'variousfrequence:
'
foreachinfrequence:
print'have'+str(each
foreach_keyinremain_dic:
arg.pop(each_key#enimilateban
i=0
while(i<30:
max_frequence=max(frequence
foreach_keyinarg:
tmp=arg[each_key]
if(tmp[1]==max_frequence:
remain_dic[each_key]=tmp[0]
i=i+1
if(i==30:
break;
frequence.remove(max_frequence
if(notfrequence:
###nomore
break;
updateFile(remain_dic
defupdateFile(new_dic:
f=open('dnsrelaycopy.txt','w'
foreach_keyinnew_dic:
word=str(new_dic[each_key]+''+str(each_keyf.write(word
f.write('\n'new_dic[each_key]=[new_dic[each_key],0]try:
withopen('newdnsrelay.pickle','wb'asnewdnsrelay_file:
pickle.dump(new_dic,newdnsrelay_fileexceptIOErroraserr:
print'Fileerror:
'+str(errexceptpickle.PickleErrorasperr:
print'Picklingerror:
'+str(perrMakefram.py成帧模块,形成对外围权威服务器的询问帧importsocketdefmake(re_ip,msg:
tmp_list=[]forchinmsg:
tmp_list.append(chtmp_list[4:
12]=['\x00','\x01','\x00','\x01','\x00','\x00','\x00','\x00']tmp_list=tmp_list+['\xc0','\x0c','\x00','\x01','\x00\','\x01','\x00','\x00','\x02','\x58','\x00','\x04']dive_ip=socket.inet_aton(re_ipch_ip=[]foreach_chindive_ip:
ch_ip.append(each_chtmp_list=tmp_list+ch_ipre_msg=''.join(tmp_listreturnre_msgCharhandle.py提取出报文中的urldefget_request(your_list:
my_list=[]new_list=[]my_list.extend(your_listre_list=[]thelen=ord(my_list[0]try:
whilethelen!
=0:
new_list=new_list+my_list[1:
thelen+1]my_list[0:
thelen+1]=[]thelen=ord(my_list[0]new_list.append('.'exceptIndexError:
print"baowengeshibudui"new_list.pop(returnnew_list原始的dnsrelay.txt老师给出了,自己去弄,把四个py文件放一个目录下,再放一个dnsrelay.txt然后,用pythonshell打开socket.py从pythonshell运行,或者直接F5,记得把本地dns手动指向本机。
P.S:
本人亲测,蒋老师没用过python,从头到尾就是我讲他听,从不为难我,我说有个LRU算法,他还说我做的好!
如果有不懂的联系作者:
302969722@学妹优先