版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、北郵計(jì)算機(jī)網(wǎng)絡(luò)課程設(shè)計(jì) DNS 服務(wù)器1 環(huán)境操作系統(tǒng)隨意,語(yǔ)言 python2.72. 文件Socket.py 說(shuō)明 完成緩存檢測(cè),確定是回應(yīng),還是轉(zhuǎn)發(fā),如果是回應(yīng),確定是本地回 應(yīng)還是外部應(yīng)答轉(zhuǎn)發(fā)import socketimport mydicimport charhandleimport makeframes = socket.socket(socket.AF_INET, socket.SOCK_DGRAMport = 53the_dic = mydic.get_web_ip(client_request = client_request_index = reverse_map = k
2、ey_record = 0client_wait = s.bind('',porttime_rest = 0request_general = ''print 'running'while True:try:msg , (client,port = s.recvfrom(1024except:print 'Time out! 'continuerequest = request = list(msgrequre_web = charhandle.get_request(request12:website = ''.
3、join(requre_webif(port = 53:answer = answer = list(msgprint "Type:Remote Response"print "remote answer is: "response_ip = msg-4+msg-3+msg-2+msg-1char_ip = socket.inet_ntoa(response_ipprint website + ' has the ip : '+char_ipfre = mydic.storeForUpdate(website,char_ip print
4、'with the frequence of '+ str(fre#real_request = client_requestrequest0+request1for each_client in client_wait:my_key = client_requestrequest0+request1+str(each_client if client_request_index.get(my_key != None :s.sendto(msg,client_request_indexmy_keyprint "Response to ip and Client por
5、t:"print client_request_indexmy_keybreakelse:print "Type: Client Request"print "ip and port:"print (client,port# requre_web = charhandle.get_request(request12:# website = ''.join(requre_webprint "Request website:"+websiteif(the_dic.get(website != None:print
6、 "Found in local cache:"re_ip = the_dic.get(websiteprint re_ipfre = mydic.storeForUpdate(websiteprint re_ip0+' with frequence '+str(frezhen = makeframe.make(re_ip0,msgs.sendto(zhen,(client,portelse:print "need to ask remote server"key_record = key_record + 1request_genera
7、l = key_recordclient_requestrequest0+request1+str(client = request_generalclient_request_indexrequest_general = (client,portif client not in client_wait:client_wait.append(client# select authority DNS server as you wishtime_rest = time_rest+1try:if(time_rest = 50:print 'pay attention'print &
8、#39;#'mydic.updateCache(print '#'the_dic = mydic.get_web_ip(time_rest = 0except:print 'not valid frequence'print '-'s.close(Mydic.py 完成本地查詢(xún), LRU 算法維護(hù)本地緩存, socket.py 第 97行的 time_request值可以 確定更新緩存的頻率,可以直接輸出在 dnsrelay.txt 內(nèi),但是為了方便觀測(cè)變化,我輸出到 了 dnsrelaycopy.txt 里面,運(yùn)行一段時(shí)間之后,可以去看
9、看學(xué)習(xí)到的新條目import pickleimport osimport threadingA=d_web_ip = d_ip_web = update_dic = data = open('dnsrelay.txt'for each_line in data:try:(ip,sitecopy = each_line.split(' ',1(site,nothing = str(sitecopy.split('n',1d_web_ipsite = ip,1d_ip_webip = siteexcept:print 'file error&
10、#39;data.close(try:with open('newdnsrelay.pickle','wb' as newdnsrelay_file:pickle.dump(d_web_ip,newdnsrelay_fileexcept IOError as err:print 'File error:'+str(errexcept pickle.PickleError as perr:print 'Pickling error:'+str(perrdef get_web_ip(:with open('newdnsrela
11、y.pickle','rb' as f:global update_dicupdate_dic = pickle.load(freturn update_dic.copy(return(Nonedef storeForUpdate(web_site, add = None:global update_dicif(update_dic.get(web_site != None:add_frequen = update_dicweb_siteadd_frequen1 = add_frequen1 + 1print web_site+' frequence incra
12、se 1,with ip '+add_frequen0 return add_frequen1else:update_dicweb_site = add,1print 'record for a new site'return 1def updateCache(:global update_dicm = update_dic.copy(t = threading.Thread(target = my_thread,kwargs = m print 'ready to update local cache't.start(t.join(def my_thr
13、ead(*argu,*arg:frequence = remain_dic = for each_key in arg:tmp = argeach_keyif(tmp1 not in frequence:frequence.append(tmp1print 'various frequence:'for each in frequence:print 'have '+ str(eachfor each_key in remain_dic:arg.pop(each_key#enimilate bani = 0while(i < 30:max_frequenc
14、e = max(frequencefor each_key in arg:tmp = argeach_keyif(tmp1 = max_frequence:remain_diceach_key = tmp0i = i + 1if(i = 30:break;frequence.remove(max_frequenceif(not frequence:#no morebreak;updateFile(remain_dicdef updateFile(new_dic:f = open('dnsrelaycopy.txt','w'for each_key in new_
15、dic:word = str(new_diceach_key + ' ' + str(each_key f.write(wordf.write('n' new_diceach_key = new_diceach_key,0 try: with open('newdnsrelay.pickle','wb' as newdnsrelay_file: pickle.dump(new_dic,newdnsrelay_file except IOError as err: print 'File error:'+str(er
16、r except pickle.PickleError as perr: print 'Pickling error:'+str(perr Makefram.py 成幀模塊,形成對(duì)外圍權(quán)威服務(wù)器的詢(xún)問(wèn)幀 import socket def make(re_ip,msg: tmp_list = for ch in msg: tmp_list.append(ch tmp_list4:12 = 'x00','x01','x00','x01','x00','x00','x00'
17、;,'x00' tmp_list = tmp_list+'xc0','x0c','x00','x01','x00 ','x01','x00','x00','x02','x58','x00','x04' dive_ip = socket.inet_aton(re_ip ch_ip = for each_ch in dive_ip: ch_ip.append(each_ch tmp_list
18、= tmp_list + ch_ip re_msg = ''.join(tmp_list return re_msg Charhandle.py 提取出報(bào)文中的 url def get_request(your_list: my_list = new_list = my_list.extend(your_list re_list = thelen = ord(my_list0 try: while thelen != 0: new_list = new_list + my_list1:thelen+1 my_list0:thelen+1 = thelen = ord(my_list0 new_list.append('.' except IndexError: print &qu
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年高新技術(shù)企業(yè)委托招聘與研發(fā)團(tuán)隊(duì)構(gòu)建合同3篇
- 2025年食材配送企業(yè)冷鏈物流服務(wù)合同3篇
- 2025版共享單車(chē)運(yùn)維服務(wù)與用戶(hù)租賃合同范本3篇
- 2025年度旅游汽車(chē)租賃及戶(hù)外活動(dòng)策劃合同4篇
- 二零二五版高端養(yǎng)殖牛場(chǎng)合伙經(jīng)營(yíng)合同3篇
- 二零二五版科技創(chuàng)新園區(qū)黨建資源共享合作協(xié)議3篇
- 2025年度煤礦設(shè)備采購(gòu)及安裝服務(wù)二零二五版合同4篇
- 鄭州城建職業(yè)學(xué)院《中國(guó)民事訴訟法》2023-2024學(xué)年第一學(xué)期期末試卷
- 2024藥品采購(gòu)及質(zhì)量管理體系認(rèn)證合同3篇
- 2025年度市政工程承攬合同建設(shè)施工合同模板4篇
- 課題申報(bào)書(shū):GenAI賦能新質(zhì)人才培養(yǎng)的生成式學(xué)習(xí)設(shè)計(jì)研究
- 駱駝祥子-(一)-劇本
- 全國(guó)醫(yī)院數(shù)量統(tǒng)計(jì)
- 《中國(guó)香文化》課件
- 2024年醫(yī)美行業(yè)社媒平臺(tái)人群趨勢(shì)洞察報(bào)告-醫(yī)美行業(yè)觀察星秀傳媒
- 第六次全國(guó)幽門(mén)螺桿菌感染處理共識(shí)報(bào)告-
- 2024-2030年中國(guó)商務(wù)服務(wù)行業(yè)市場(chǎng)現(xiàn)狀調(diào)查及投資前景研判報(bào)告
- 高一英語(yǔ)必修一試卷(含答案)(適合測(cè)試)
- 中國(guó)的世界遺產(chǎn)智慧樹(shù)知到期末考試答案2024年
- 中國(guó)綠色食品市場(chǎng)調(diào)查與分析報(bào)告
- 手衛(wèi)生依從性調(diào)查表
評(píng)論
0/150
提交評(píng)論