性能测试URL自动转码

最近做性能测试,写了个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 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=%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%2Bz8CQA1npn5OYp8ZRNq2xArtx7HuL2qiPKl19gGkN7VQ
bxFXL12%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=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

转换后就可以更方便的查找对比,方便测试进行。

应该还有需要改进的地方,如果测试需要,再做改进。

 

转载于:https://www.cnblogs.com/ryansunyu/p/3974727.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值