北邮计算机网络课程设计DNS服务器.docx
- 文档编号:27393565
- 上传时间:2023-06-30
- 格式:DOCX
- 页数:8
- 大小:16.50KB
北邮计算机网络课程设计DNS服务器.docx
《北邮计算机网络课程设计DNS服务器.docx》由会员分享,可在线阅读,更多相关《北邮计算机网络课程设计DNS服务器.docx(8页珍藏版)》请在冰豆网上搜索。
北邮计算机网络课程设计DNS服务器
北邮计算机网络课程设计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''
data.close(
try:
withopen('newdnsrelay.pickle','wb'asnewdnsrelay_file:
pickle.dump(d_web_ip,newdnsrelay_file
exceptIOErroraserr:
print':
'+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;
update
defupdate:
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_IOErroraserr:
print':
'+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算法,他还说我做的好!
如果有不懂的联系作者:
学妹优先
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机网络 课程设计 DNS 服务器
![提示](https://static.bdocx.com/images/bang_tan.gif)