標籤:style blog http color io os java ar for
最近做效能測試,寫了個python程式自動將URL裡面的‘%2B’,‘20%’,‘3B‘等轉換成正常字元,方便查看。
import os,sys;path = sys.path[0]os.chdir(path)encode_list = ‘encode_list.txt‘result = path + ‘\\results‘def get_encode(): encode_file = open(path + ‘\\‘+ encode_list) encode = dict() for line in encode_file: if line!=‘\n‘ and len(line) >1: if line.find(‘read me‘) <0: encode[line[1:].strip()] = line[0] return encodedef get_files(): files = os.listdir(path) file_list = list() for file in files: if file.endswith(‘.txt‘) and file!= encode_list: file_list.append(file) return file_listdef relace_url_encode(strPri,dicEncode): items = dicEncode.items() for (key,value) in items: if strPri.find(key): strPri = strPri.replace(key,value) return strPridef create_result(): if not os.path.isdir(result): os.makedirs(result) def write_result(filePri,strText): fp = open(result+‘\\‘+filePri,‘w+‘) fp.write(strText) fp.close()create_result() encode = get_encode()file_list = get_files()for ff in file_list: try: f = open(ff) text = f.read() finally: f.close() temp = relace_url_encode(text,encode) temp = temp.replace(‘&‘,‘\n‘) write_result(ff,temp)
下面是檔案夾結構:
encode.py的代碼貼在上面
encode_list.txt裡面裝的是轉換對照表,其中檔案名稱是hard code在python程式裡面的,最好不要改
前面的是正常字元,後面的是需要轉換的字元
需要轉碼的URL形如下面的形式:
selectForm=selectForm&publishId=iphone6SapptR&color=%7B%22colorDisplayText%22%3A%22Grey%22%2C%22colorId%22%3A%22Grey%22%2C%22publishId%22%3A%22iphone6SapptM%22%2C%22modelCode%22%3A%22iphone6SmodelM%22%2C%22available%22%3Atrue%7D&rom=%7B%22capacityDisplayText%22%3A%2216GB%22%2C%22capacityId%22%3A%2216GB%22%2C%22imageFileName%22%3A%22iPhoneX-gold.png%22%2C%22publishId%22%3A%22iphone6SapptM%22%2C%22modelCode%22%3A%22iphone6SmodelM%22%2C%22available%22%3Atrue%7D&locationId=%7B%22locationId%22%3A%22Marina_Bay_Sands_Exhibition_Hall_A%22%2C%22locationDisplayText%22%3A%22Marina%20Bay%20Sands%20Exhibition%20Hall%20A%22%2C%22publishId%22%3A%22iphone6SapptM%22%2C%22available%22%3Atrue%7D&dateId=&timeId=&javax.faces.ViewState=H4sIAAAAAAAAAE1QO0sDQRAeL7n4RGIEK9PZWLhgJ1hoQIOH8YGgCBa6uVuTC3e76z5ydxaBNFrYWGhhIVpY5k%2BIhZ2gpZXYW9u6F0LiBzvMst9jZjs%2FYHMpYKqBmxhp5QdoA8v6Fub28Ofzy8zJewasMowFDHtl7ComHBhVdUFknQVezFdWIcVENGJq3hxLwazLQiQ1RafYJRKtJRSHvltylc%2BoNFnTg6ySEDip%2BFLF7Y%2Fi3Su%2Bz8CQA1npn5OYp8ZRNq2xArtx7HuL2qiPKl19gGkN7VQbxFXL12%2BHD3k5H1iGmsosfQYtyJnO5gb9W6YlYCFVx73ZzKScUUIV2ncOfBLtMabmdgXjRKhkkyQSeigYZwGTg%2BR1qsP%2Fj1xBLsBSOV7%2FN7s8hypSI6Lw%2Ffj0275cstL97CYONDF%2B%2BQFvW4dVIi46t8Xxm6%2Br%2FiKcx3%2FTRn8XowEAAA%3D%3D&javax.faces.source=color%3A1&javax.faces.partial.event=change&javax.faces.partial.execute=color%20color%3A1&javax.faces.partial.render=productImage%20rom%20timeId%20dateId%20locationId&javax.faces.behavior.event=change&javax.faces.partial.ajax=true
我把轉碼過的結果全部放在result檔案夾裡面,雙擊運行,所有的txt檔案都會被轉碼。並且該檔案夾隨便放在哪裡,代碼均可以執行。
轉碼過後:
selectForm=selectFormpublishId=iphone6SapptRcolor={"colorDisplayText":"Grey","colorId":"Grey","publishId":"iphone6SapptM","modelCode":"iphone6SmodelM","available":true}rom={"capacityDisplayText":"16GB","capacityId":"16GB","imageFileName":"iPhoneX-gold.png","publishId":"iphone6SapptM","modelCode":"iphone6SmodelM","available":true}locationId={"locationId":"Marina_Bay_Sands_Exhibition_Hall_A","locationDisplayText":"Marina_Bay_Sands_Exhibition_Hall_A","publishId":"iphone6SapptM","available":true}dateId=timeId=javax.faces.ViewState=H4sIAAAAAAAAAE1QO0sDQRAeL7n4RGIEK9PZWLhgJ1hoQIOH8YGgCBa6uVuTC3e76z5ydxaBNFrYWGhhIVpY5k+IhZ2gpZXYW9u6F0LiBzvMst9jZjs/YHMpYKqBmxhp5QdoA8v6Fub28Ofzy8zJewasMowFDHtl7ComHBhVdUFknQVezFdWIcVENGJq3hxLwazLQiQ1RafYJRKtJRSHvltylc+oNFnTg6ySEDip+FLF7Y/i3Su+z8CQA1npn5OYp8ZRNq2xArtx7HuL2qiPKl19gGkN7VQbxFXL12+HD3k5H1iGmsosfQYtyJnO5gb9W6YlYCFVx73ZzKScUUIV2ncOfBLtMabmdgXjRKhkkyQSeigYZwGTg+R1qsP/j1xBLsBSOV7/N7s8hypSI6Lw/fj0275cstL97CYONDF++QFvW4dVIi46t8Xxm6+r/iKcx3/TRn8XowEAAA==javax.faces.source=color:1javax.faces.partial.event=changejavax.faces.partial.execute=color_color:1javax.faces.partial.render=productImage_rom_timeId_dateId_locationIdjavax.faces.behavior.event=changejavax.faces.partial.ajax=true
轉換後就可以更方便的尋找對比,方便測試進行。
應該還有需要改進的地方,如果測試需要,再做改進。
效能測試URL自動轉碼