1、介绍
关于标记的一些函数。
2、代码
from api.PropertiesClasses import Params
"""
自动标记
@url: str类型,表示url
@message: str类型,表示请求报文
@name_list: str类型,根据url的直接参数的名称标记其对应的值,name_list参数可以通过空格分隔多个参数名称
@text_list: str类型,也是根据空格分隔,匹配首次结果被标记
@return: list[int]类型,元素个数必须为偶数,依次表示各组标记的起止索引
url和message这两者参数必须且只能设置一个不为空字符串
"""
def mark(url: str = '', message: str = '', name_list: str = '', text_list: str = ''):
"""mark(url = '', message = '', name_list = '', text_list = '') -> list[int]"""
result = list()
if url == '' and message == '':
return result
elif url != '':
# 排除可能的hash部分干扰
url_2 = url.split('#')[0]
else:
arr = message.split('\n', maxsplit=1)
arr1 = arr[0].split(' ')
# 完全自动
if name_list == '' and text_list == '':
index = 0
# 存在直接参数
if url_2 != '' and '?' in url_2:
arr2 = url_2.split('?')
index = len(arr2[0])
elif message != '' and '?' in arr1[1]:
arr2 = arr1[1].split('?')
index = len(arr1[0] + ' ' + arr2[0])
if index != 0:
params = Params(arr2[1])
for name, value in params.property_list:
# 前面的+1是?或者&字符,后面的+1是=字符
index = index + 1 + len(name) + 1
if value != '':
result.extend([index, index + len(value)])
index = index + len(value)
else:
index = 0
# 不存在直接参数,但存在路径参数
if url_2 != '' and url_2.count('/')>2:
# 0、1、2非路径部分
arr2 = url.split('/')
index = len(arr2[0] + arr2[1] + arr2[2])
arr2 = arr2[2:]
elif message != '' and '/' in arr1[1]:
arr2 = arr1[1].split('/')
index = len(arr1[0] + ' ')
if index != 0:
for i in range(1, len(arr2)):
# 添加各级的/
index = index + 1
if arr2[i] != '':
result.extend([index, index + len(arr2[i])])
index = index + len(arr2[i])
# 根据url直接参数
elif name_list != '':
# 假设name_list是空格作为分隔符
name_list: list = name_list.split(' ')
index = 0
# 存在直接参数
if url_2 != '' and '?' in url_2:
arr2 = url_2.split('?')
index = len(arr2[0])
elif message != '' and '?' in arr1[1]:
arr2 = arr1[1].split('?')
index = len(arr1[0] + ' ' + arr2[0])
if index != 0:
params = Params(arr2[1])
for name, value in params.property_list:
# 前面的+1是?或者&字符,后面的+1是=字符
index = index + 1 + len(name) + 1
if value != '':
if name in name_list:
result.extend([index, index + len(value)])
index = index + len(value)
# 根据直接文本
elif text_list != '':
if url != "":
s = url
else:
s = message
index = 0
text_list = text_list.split(' ')
for text in text_list:
if text != '' and text in s[index:]:
index = s.find(text, __start=index)
result.extend([index, index + len(text)])
index = index + len(s)
if index >= len(s):
break
return result
标签:index,name,url,MarkModule,list,len,api,arr2
From: https://www.cnblogs.com/wd404/p/17432588.html