性能测试URL自动转码

    xiaoxiao2022-07-16  141

    最近做 性能测试,写了个python程序自动将URL里面的‘+’,‘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 encode def 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_list def relace_url_encode(strPri,dicEncode): items = dicEncode.items() for (key,value) in items: if strPri.find(key): strPri = strPri.replace(key,value) return strPri def 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={"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+z8CQA1npn5OYp8ZRNq2xArtx7HuL2qiPKl19gGkN7VQ bxFXL12+HD3k5H1iGmsosfQYtyJnO5gb9W6YlYCFVx73ZzKScUUIV2ncOfBLtMabmdgXjRKhkkyQSeigYZwGTg+R1qsP/j1xBLsBSOV7/N7s8hypSI6Lw/fj0275cstL97CYONDF++QFvW4dVIi46t8Xxm6+r/iKcx3/TRn8XowEAAA==&javax.faces.source=color:1&javax.faces.partial.event=change&javax.faces.partial.execute=color color:1&javax.faces.partial.render=productImage rom timeId dateId locationId&javax.faces.behavior.event=change&javax.faces.partial.ajax=true   我把转码过的结果全部放在result文件夹里面,双击运行,所有的txt文件都会被转码。并且该文件夹随便放在哪里,代码均可以执行。   转码过后: selectForm=selectForm publishId=iphone6SapptR color={"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+z8CQA1npn5OYp8ZRNq2xArtx7HuL2qiPKl19gGkN7VQ bxFXL12+HD3k5H1iGmsosfQYtyJnO5gb9W6YlYCFVx73ZzKScUUIV2ncOfBLtMabmdgXjRKhkkyQSeigYZwGTg+R1qsP/j1xBLsBSOV7/N7s8hypSI6Lw/fj0275cstL97CYONDF++QFvW4dVIi46t8Xxm6+r/iKcx3/TRn8XowEAAA== javax.faces.source=color:1 javax.faces.partial.event=change javax.faces.partial.execute=color_color:1 javax.faces.partial.render=productImage_rom_timeId_dateId_locationId javax.faces.behavior.event=change javax.faces.partial.ajax=true   转换后就可以更方便的查找对比,方便测试进行。   应该还有需要改进的地方,如果测试需要,再做改进。 最新内容请见作者的GitHub页:http://qaseven.github.io/ 相关资源:敏捷开发V1.0.pptx
    最新回复(0)