import requests
# 请求参数的十六进制字符串
hex_string = "00 00 00 00 1A 0A 16 0A 05 70 61 70 65 72 12 06 E4 B8 AD E8 8D AF 28 01 30 14 42 01 00 10 01"
# 移除空格并将十六进制字符串转换为字节串
byte_string = bytes.fromhex(hex_string.replace(" ", ""))
headers = {
"content-type": "application/grpc-web+proto",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36",
}
url = "https://s.wanfangdata.com.cn/SearchService.SearchService/search"
response = requests.post(url, data=byte_string, headers=headers)
print(response)
print(response.text)
得到的结果是序列化之后的数据
import blackboxprotobuf
import requests
# 给定的十六进制字符串
hex_string = "0A 16 0A 05 70 61 70 65 72 12 06 E4 B8 AD E8 8D AF 28 01 30 14 42 01 00 10 01"
# 移除空格并将十六进制字符串转换为字节串
byte_string = bytes.fromhex(hex_string.replace(" ", ""))
print(byte_string)
# 解析得到原始数据和消息类型
original_data, message_type = blackboxprotobuf.protobuf_to_json(byte_string)
print(original_data) # str类型
print(message_type) # dict类型
# 序列化数据
# 修改消息类型之后的数据
original_data = {
"1": {
"1": "paper",
"2": "中药",
"5": 1,
"6": 20,
"8": "\u0000"
},
"2": 1
}
form_data = bytes(blackboxprotobuf.encode_message(original_data, message_type))
print("序列化数据-->", form_data) # b'\n\x16\n\x05paper\x12\x06\xe4\xb8\xad\xe8\x8d\xaf(\x010\x14B\x01\x00\x10\x01'
print(len(form_data))
headers = {
"content-type": "application/grpc-web+proto",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36",
}
url = "https://s.wanfangdata.com.cn/SearchService.SearchService/search"
response = requests.post(url, data=bytes([0,0,0,0,len(form_data)]) + form_data, headers=headers)
print(response)
# 反序列化响应结果
response_data, message_type = blackboxprotobuf.protobuf_to_json(response.content[5:])
print(response_data)