zoukankan      html  css  js  c++  java
  • python正则-字符串处理,主要用于处理请求参数格式为application/x-www-form-urlencoded的表单数据

    #当提交的表单数据格式为application/x-www-form-urlencoded,直接从浏览器复制出来的格式是str_lin(chrome,也是最常见的)或者str_in2(火狐)这两种格式
    你会发现直接复制出来用python进行请求时不行的,所以需要对其进行处理,最终的格式key1=value1&key2=value2...这种格式
    #下面是实现代码,第三个方法是没事的时候花了点时间帮开发写的一个协助脚本,用于php调试请求
    
    import re
    #request_data_change_to_StrEncode
    str_in='''customer_type: 1
                source: 1
                course_name_id: 3
                tel: 18883612485
                customer_name: 测试
                sex: 0
                school: ce'w's
                intro_id: 0
                keys[0]: 0
                transfer_time: 2019-04-03 17:32:39''' #谷歌浏览器请求参数格式
    str_in2='''card_manage_id  5
            keys[0]  0
            study_card_name  孵化营9800
            price  9800.00
            number  9
            least_price  1.00
            org_id  3
            meth_id  2
            year  3
            month  0
            ticket[0][ticket_manage_id]  1
            ticket[0][ticket_num]  10
            period  36
            transfer_time  2019-04-03 17:32:39''' #火狐浏览器请求参数格式
    
    str_in3='''customer_type:2
        source:1
        course_name_id:1
        tel:13500000136
        customer_name:测试-意向
        sex:1
        customer_card_afterfour:1234
        sc_code_id:2
        school:5555
        address[0]:140000
        address[1]:140100
        sc_ty_id:2
        student_number:2
        team_number:2
        turnover:2
        intro_id:1
        age_group:2S
        customer_card:500241199601020215
        is_fellow_study:2
        fellow_hr:223
        study_goal:2
        qq:2455
        wechat_number:wew
        email:225@qq.com
        school_area:222
        intrdu_id:312
        transfer_time:2019-04-03 17:32:39'''
    str_in4='''member_finance_id[0]: 27803
            total_price: 59800
            state: 1
            finan_rmk: 543
            pay_fees_ascribed: 1'''
    class request_data_change_to_str(object):
        def requestDataToStr_firefoxAndChrome(self,str_in):
            str_colon=re.search('[W][s]{1,1}|([s]){1,1}|[W]{1,1}',str_in).group() #匹配出字符串中所有的冒号
            str_equal=re.sub(str_colon,'=',str_in) #将字符串中的冒号替换为等于号(: >>> =)
            str_lin=re.search("(s
    *){2,}|(s
    *)",str_equal).group() #匹配出字符串中所有的换行符与空格,不写表示不限定匹配次数
            str_give=re.sub(str_lin,'&',str_equal) #将字符串中的换行符替换为& (
     >>> &)
            str_lin2=re.search('s.*',str_give)
            if str_lin2 is not None:
                str_lin2=str_lin2.group()
                str_lin3=re.search('=',str_lin2)
                if str_lin3 is not None  and 'time' in str_give: #对请求参数含有时间字段进行特殊处理
                    try:
                        str_lin3=str_lin3.group()
                        str_give2=re.sub(str_lin3,':',str_lin2)
                        str_give3=re.sub(str_lin2,str_give2,str_give)
                        print(str_give3)
                        return str_give3.encode() #返回字符串,并对数据进行编码处理
                    except Exception as error:
                        print(error)
                        # pass
                else:
                    print(str_give)
                    return str_give.encode()
            else:
                print(str_give)
                return str_give.encode()
                
    
        def requestDataToStr_firefoxAndChrome_teShu(self,str_in): #特殊处理
            str_colon=re.search('[^S?]{2,}',str_in).group() #匹配出字符串中所有的冒号
            str_equal=re.sub(str_colon,'=',str_in) #将字符串中的冒号替换为等于号(: >>> =)
            str_lin=re.search("(s
    *){2,}|(s
    *)",str_equal).group() #匹配出字符串中所有的换行符与空格,不写表示不限定匹配次数
            str_give=re.sub(str_lin,'&',str_equal) #将字符串中的换行符替换为& (
     >>> &)
            print('---'+str_colon+'---')
            # print(str_equal)
            str_lin2=re.search('s.*',str_give)
            # print(str_lin2)
            if str_lin2 is not None:
                str_lin2=str_lin2.group()
                str_lin3=re.search('=',str_lin2)
                print(str_lin3)
                if str_lin3 is not None  and 'time' in str_give: #对请求参数含有时间字段进行特殊处理
                    try:
                        str_lin3=str_lin3.group()
                        str_give2=re.sub(str_lin3,':',str_lin2)
                        str_give3=re.sub(str_lin2,str_give2,str_give)
                        print(str_give3)
                        return str_give3.encode() #返回字符串,并对数据进行编码处理
                    except Exception as error:
                        print(error)
                        # pass
                else:
                    print(str_give)
                    return str_give.encode()
            else:
                print(str_give)
                return str_give.encode()
    
        def requestDataToStr_firefoxAndChrome_teShu2(self,str_in): #特殊处理2
            str_colon=re.search(':W?|s*:W?',str_in).group() #匹配出字符串中所有的冒号
            str_equal=re.sub(str_colon,'=',str_in) #将字符串中的冒号替换为等于号(: >>> =)
            str_lin=re.search("(s
    *){2,}|(s
    *)",str_equal).group() #匹配出字符串中所有的换行符与空格,不写表示不限定匹配次数
            str_give=re.sub(str_lin,'&',str_equal) #将字符串中的换行符替换为& (
     >>> &)
            # print('---'+str_colon+'---')
            # print(str_equal)
            str_lin2=re.search('s.*',str_give)
            print(str_lin2)
            if str_lin2 is not None:
                str_lin2=str_lin2.group()
                str_lin3=re.search('=',str_lin2)
                print(str_lin3)
                if str_lin3 is not None  and 'time' in str_give: #对请求参数含有时间字段进行特殊处理
                    try:
                        str_lin3=str_lin3.group()
                        str_give2=re.sub(str_lin3,':',str_lin2)
                        str_give3=re.sub(str_lin2,str_give2,str_give)
                        print(str_give3)
                        return str_give3.encode() #返回字符串,并对数据进行编码处理
                    except Exception as error:
                        print(error)
                        # pass
                else:
                    # print(str_give)
                    return str_give.encode()
            else:
                print(str_give)
                return str_give.encode()
    
    
        def requestDataTotr_custom(self,str_in,str_custom='=>'):
            str_colon=re.search('s*:W?',str_in).group() #匹配出字符串中所有的冒号
            str_tihuan='"'+str_custom+'"'
            str_equal=re.sub(str_colon,str_tihuan,str_in) #将字符串中的冒号替换为目标符号即定义的str_custom的值
            str_lin=re.search("(s
    *){2,}|(s
    *)",str_equal).group() #匹配出字符串中所有的换行符与空格,不写表示不限定匹配次数
            str_give=re.sub(str_lin,'"'+str_lin+'"',str_equal) #将字符串中的换行符替换为& (
     >>> &)
            str_lin2=re.search('^',str_give).group() #匹配字符串开头
            str_give2=re.sub('^','"'+str_lin2,str_give) #替换结果为'"'+匹配结果加
            str_lin3=re.search('$',str_give2).group() #匹配字符串末尾
            str_give3=re.sub('$',str_lin3+'"',str_give2)#替换结果为匹配结果加+'"'
            # print('---'+str_colon+'---')
            # print(str_equal)
            # print(str_give2)
            # print(str_give3)
            return str_give.encode() #返回字符串,并对数据进行编码处理
    
    
        def requestDataTostr_postman(self,str_in):
            str_colon=re.search('s*:W*',str_in).group() #匹配出字符串中所有的冒号
            str_tihuan=':'
            str_equal=re.sub(str_colon,str_tihuan,str_in) #将字符串中的冒号替换为目标符号即定义的str_custom的值
            str_lin=re.search("(s
    *){2,}|(s
    *)",str_equal).group() #匹配出字符串中所有的换行符与空格,不写表示不限定匹配次数
            str_give=re.sub(str_lin,'
    ',str_equal)
            print(str_give)
    
    if __name__=="__main__":
        request_data_to_str=request_data_change_to_str()
        request_data_to_str.requestDataToStr_firefoxAndChrome(str_in3)
        # request_data_to_str.requestDataToStr_firefoxAndChrome_teShu(str_in2)
        # request_data_to_str.requestDataTotr_custom(str_in4)
        # request_data_to_str.requestDataTostr_postman(str_in3)
    输出:
    
    str_in 方法1 requestDataToStr_firefoxAndChrome
    
    >>>>> 
    
    customer_type=>1&source=>1&course_name_id=>3&tel=>18883612485&customer_name=>测试&sex=>0&school=>ce'w's&intro_id=>2340
    
    str_in方法2 requestDataToStr_firefoxAndChrome_teShu2
    
    >>>
    customer_type=>1&source=>1&course_name_id=>3&tel=>18883612485&customer_name=>测试&sex=>0&school=>ce'w's&intro_id=>2340
    
    str_in方法3 requestDataTotr_custom
    
    >>>>
    
    "customer_type"=>"1"
    "source"=>"1"
    "course_name_id"=>"3"
    "tel"=>"18883612485"
    "customer_name"=>"测试"
    "sex"=>"0"
    "school"=>"ce'w's"
    "intro_id"=>"2340"

    #优化对时间的处理

  • 相关阅读:
    计算机网络通信
    javap查看class文件
    JDK动态代理与CGLib动态代理
    error the @annotation pointcut expression is only supported at Java 5 compliance
    redis清空缓存
    利用HttpURLConnection发送请求
    linux下用命令导出mysql表数据
    adb push和pull使用
    mysqld.exe占比cpu 100% 解决方案
    养成好习惯
  • 原文地址:https://www.cnblogs.com/qtclm/p/10726500.html
Copyright © 2011-2022 走看看