欢迎来到天天文库
浏览记录
ID:32184177
大小:46.00 KB
页数:7页
时间:2019-02-01
《北邮计算机网络课程设计dns服务器》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、北邮计算机网络课程设计DNS服务器1环境操作系统随意,语言python2.72.文件Socket.py说明完成缓存检测,确定是回应,还是转发,如果是回应,确定是本地回应还是外部应答转发importsocketimportmydicimportcharhandleimportmakeframes=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)port=53the_dic=mydic.get_web_ip()client_request={}client_request_index={}reverse_map={}key_re
2、cord=0client_wait=[]s.bind(('',port))time_rest=0request_general=''print'running'whileTrue:try:msg,(client,port)=s.recvfrom(1024)except:print'Timeout!'continuerequest=[]request=list(msg)requre_web=charhandle.get_request(request[12:])website=''.join(requre_web)if(port==53):answer=[]answer=
3、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_ipfre=mydic.storeForUpdate(website,char_ip)print'withthefrequenceof'+str(fre)###real_request=client_request[request[0]+re
4、quest[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]breakelse:print"Type:ClientRequest"print"
5、ipandport:"print(client,port)###requre_web=charhandle.get_request(request[12:])###website=''.join(requre_web)print"Requestwebsite:"+websiteif(the_dic.get(website)!=None):print"Foundinlocalcache:"re_ip=the_dic.get(website)printre_ipfre=mydic.storeForUpdate(website)printre_ip[0]+'withfrequ
6、ence'+str(fre)zhen=makeframe.make(re_ip[0],msg)s.sendto(zhen,(client,port))else:print"needtoaskremoteserver"key_record=key_record+1request_general=key_recordclient_request[request[0]+request[1]+str(client)]=request_generalclient_request_index[request_general]=(client,port)ifclientnotincl
7、ient_wait:client_wait.append(client)#selectauthorityDNSserverasyouwishs.sendto(msg,('10.3.9.4',53))time_rest=time_rest+1try:if(time_rest==50):print'payattention'print'######################'mydic.updateCache()print'######################'the_dic=mydic.get_web_ip()time_res
此文档下载收益归作者所有