【网络安全带你练爬虫-100练】第22练:数据包中参数提取与处理

目录

一、目标1:GET数据包的处理

1、GET数据包中参数的提取

2、GET请求中 统计参数个数

二、目标2:POST数据包的处理

1、post中参数个数的提取

2、POST请求中 统计参数个数


一、目标1:GET数据包的处理

1、GET数据包中参数的提取

import re

def extract_get_parameters(request):
    # 查找GET请求中的参数部分
    match = re.search(r'GET\s+/.*\?(.*)\s+HTTP', request)
    if match:
        parameters = match.group(1)
        # 将参数部分按照 '&' 分割成键值对
        parameter_list = parameters.split('&')
        # 将键值对解析为字典形式
        parameters_dict = {}
        for parameter in parameter_list:
            key, value = parameter.split('=')
            parameters_dict[key] = value
        return parameters_dict

    return {}

# 示例请求
request = "GET /xxxx/xxxx HTTP/1.1\nHost: x.x.x.x.cn\n……{此处省略一万字}"

parameters = extract_get_parameters(request)
print(parameters)

扫描二维码关注公众号,回复: 16442456 查看本文章


2、GET请求中 统计参数个数

import re

def count_get_parameters(request):
    # 查找GET请求中的参数部分
    match = re.search(r'GET\s+/.*\?(.*)\s+HTTP', request)
    if match:
        parameters = match.group(1)
        # 将参数部分按照 '&' 分割成键值对
        parameter_list = parameters.split('&')
        # 统计参数个数
        return len(parameter_list)

    return 0

# 示例请求
request = "GET /xxxx/xxxx HTTP/1.1\nHost: x.x.x.x.cn\n……{此处省略一万字}"

count = count_get_parameters(request)
print(count)



二、目标2:POST数据包的处理

1、post中参数个数的提取

import re

def count_post_parameters(post_data):
    # 使用正则表达式提取JSON数据
    pattern = r"\{.*\}"
    match = re.search(pattern, post_data)
    if match:
        json_data = match.group()
        parsed_data = json.loads(json_data)
        parameter_count = len(parsed_data)
        return parameter_count
    else:
        return 0

# 示例用法
post_data = '''POST /xxxx/xxxx HTTP/1.1\nHost: x.x.x.x.cn\n……{此处省略一万字}'''

parameter_count = count_post_parameters(post_data)
print(parameter_count)  # 输出:4

2、POST请求中 统计参数个数

(与GET类似,就不再做叙述了)

猜你喜欢

转载自blog.csdn.net/qq_53079406/article/details/132232192