# 解析web端提交的bytes信息,返回str信息(可以解析中文信息)
def parse_data(msg):
global g_code_length
g_code_length = msg[1] & 127
if g_code_length == 126:
g_code_length = struct.unpack('>H', msg[2:4])[0]
masks = msg[4:8]
data = msg[8:]
elif g_code_length == 127:
g_code_length = struct.unpack('>Q', msg[2:10])[0]
masks = msg[10:14]
data = msg[14:]
else:
masks = msg[2:6]
data = msg[6:]
en_bytes = b""
cn_bytes = []
for i, d in enumerate(data):
nv = chr(d ^ masks[i%4])
nv_bytes = nv.encode()
nv_len = len(nv_bytes)
if nv_len == 1:
en_bytes += nv_bytes
else:
en_bytes += b'%s'
cn_bytes.append(ord(nv_bytes.decode()))
if len(cn_bytes) > 2:
cn_str = ""
clen = len(cn_bytes)
count = int(clen / 3)
for x in range(count):
i = x * 3
b = bytes([cn_bytes[i], cn_bytes[i + 1], cn_bytes[i + 2]])
cn_str += b.decode()
new = en_bytes.replace(b'%s%s%s', b'%s')
new = new.decode()
res = (new % tuple(list(cn_str)))
else:
res = en_bytes.decode()
return res
说明
此函数需要着重关注这个地方:
for i, d in enumerate(data):
nv = chr(d ^ masks[i%4])
nv_bytes = nv.encode()
nv_len = len(nv_bytes)
if nv_len == 1:
en_bytes += nv_bytes
else:
en_bytes += b'%s'
cn_bytes.append(ord(nv_bytes.decode()))
nv是解除掩码后的数据,通过nv.encode()的方式编码后计算编码后的bytes长度,如果长度不为1,则作为非英文字符处理,考虑到中英文混杂的情况,需要添加占位符。