• 性能测试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!='
    ' 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('&','
    ')
        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

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

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

  • 相关阅读:
    springboot拦截器的拦截配置和添加多个拦截器
    ASCII对照
    爬虫出现403错误解决办法
    PhantomJS在Selenium中被标记为过时的应对措施
    Selenium 之订制启动Chrome的选项(Options)
    Selenium+PhantomJS使用时报错原因及解决方案
    python爬虫之xpath的基本使用
    JSONObject类的引用必须jar包
    selenium之使用chrome浏览器测试(附chromedriver与chrome的对应关系表)
    PhantomJS 与python的结合
  • 原文地址:https://www.cnblogs.com/ryansunyu/p/3974727.html
Copyright © 2020-2023  润新知