import os import re from typing import List def replace(a,a_swp): a_bak=a+'.bak' os.rename(a,a_bak) os.rename(a_swp,a) os.remove(a_bak) def fetch(domain): domain=domain if not domain: domain=input('please input your domain:').strip() result=[] with open('remap.config','r') as read_f: for read_line in read_f: tmpdata=read_line.strip() if re.search(domain,tmpdata) : result.append(tmpdata) print(tmpdata) else: continue if not result: print("not found") return result def change(): data=input('please input your data:').strip() data_split=data.split() #分割輸入數據取domain,數據格式爲map http://domain http://srcip ,則分割後取1下標的數據 olddata=fetch(data_split[1]) if not olddata: with open('remap.config', 'r') as read_f,open('remap.config.swp', 'w') as write_f: for read_line in read_f: write_f.write(read_line) write_f.write(data.strip('"')+'\n') else: with open('remap.config', 'r') as read_f,open('remap.config.swp', 'w') as write_f: for read_line in read_f: tmpdata = read_line.strip() if re.search(data_split[1], tmpdata): write_f.write(data.strip('"')+'\n') else: write_f.write(read_line) replace("remap.config","remap.config.swp") def add(): data=input('please input your data:').strip() with open('remap.config', 'r') as read_f, open('remap.config.swp', 'w') as write_f: for read_line in read_f: write_f.write(read_line) write_f.write(data.strip('"') + '\n') replace("remap.config", "remap.config.swp") def delete(): data = input('please input your data:').strip() olddata=fetch(data) if olddata: with open('remap.config', 'r') as read_f, open('remap.config.swp', 'w') as write_f: for read_line in read_f: tmpdata = read_line.strip() if re.search(data, tmpdata): continue else: write_f.write(read_line) replace("remap.config", "remap.config.swp") else: print("needn't delete") list='''what do you want ?: 1.fetch 2.change 3.add 4.delete 5.exit ''' list_dic={ 1:fetch, 2:change, 3:add, 4:delete, 5:exit } while True: print(list) choice=input('you choice>>: ').strip() if not choice or not choice.isdigit(): continue if choice == '5': break list_dic[int(choice)]()
如圖:remap.config格式以下
html
map http://baidu.com/ http://1.1.1.17/ map http://www.baidu.com/ http://1.1.1.17/ regex_map http://[a-z0-9].baidu.com/ http://1.1.1.11/ map http://285166825.cdn.baidu.com/ http://1.1.1.11/ map http://1731178188.cdn.baidu.com/ http://1.1.1.11/ map http://907607712.cdn.baidu.com/ http://1.1.1.11/ map http://jmbaihacker.cdn.baidu.com/ http://1.1.1.11/ map http://13991833.cdn.baidu.com/ http://1.1.1.11/ map http://89802850.cdn.baidu.com/ http://1.1.1.11/ map http://jbsdnsn.cdn.baidu.com/ http://1.1.1.11/ map http://myl666.cdn.baidu.com/ http://1.1.1.11/ map http://cdn-w.cdn.baidu.com/ http://1.1.1.11/
本程序意在熟悉python的函數、文件處理、控制模塊功能python
我的思路歷程以及遇到的問題:
git
定義四個功能模塊,add()等用pass佔位bash
打印詢問菜單,爲了簡潔用'''形式app
用while true 循環,發現沒退出功能,返回列表,增長一個退出選項5dom
用choice變量保存input輸入的值,發現沒法直接從原有詢問列表list提取對應的操做選項,因而使用一個字典list_dic保存選項與操做的對應關係ide
發現用戶可能不輸入數字,因而用str.isdigit()判斷客戶輸入,非數字則跳出本次循環函數
開始處理查詢功能fetch,發現字符串匹配判斷須要用到強大的re模塊,此處我用fetch
if re.search(domain,tmpdata):
意爲:若是domain在tmpdata中,則返回匹配對象,加上if則可判斷對象是否存在,也就是有沒有tmpdata裏面有沒有匹配到domain 更多參考:https://docs.python.org/zh-cn/3/library/re.html
cdn
處理change功能,發現必先查詢,由於我這裏默認限制修改功能數據格式爲"map http://domain http://srcip「格式,而fetch功能默認只能查找domain,則對change功能的輸入用str.split()進行分割提取domain,此處發現,input輸入帶空格的字符串必須用引號引發來,不然報錯
對fetch功能進行修改:1.若是有參數值則查找的值(即data變量)爲參數值,不然輸入一個值 2.構建一個空列表result,若查找到值,則用append()方法追加,最後return返回
文件寫入時發現沒換行,用+'\n'形式解決
write_f.write(data.strip('"')+'\n')
12.從change()功能模塊中摘取對應功能代碼略加修改生成add和delete模塊
13.發現文件覆蓋功能屢次使用,提取出來作成replace函數調用