Python中发送C中的Hex(16进制)的Buf流

转载:https://blog.csdn.net/wang2425559/article/details/79148816
Python中发送C中的Hex的Buf流
2018年01月24日 11:31:41 飞奔的蜗牛-Ryze 阅读数:295
由于经常从wireshark中抓数据包,需放在python进行发送,但socket中的send发送与c不同,终于找到一种可以将c数组转换为python socket send数据的方法,

特此记录! 从wireshark 中抓取的包,用c array表示:

char peer0_0[] = { /* Packet 40 */ 0x00, 0x00, 0x00, 0x2c, 0x63, 0x00, 0x00, 0x00, 0x75, 0x4b, 0xef, 0x0f, 0x00, 0x03, 0x00, 0x00, 0x01, 0x00, 0x00, 0x7f, 0x00, 0x01, 0x00, 0x00, 0x88, 0x9f, 0xfa, 0xfd, 0x9f, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };

转为python的列表或元组: a=[0x00, 0x00, 0x00, 0x2c, 0x63, 0x00, 0x00, 0x00, 0x75, 0x4b, 0xef, 0x0f, 0x00, 0x03, 0x00, 0x00, 0x01, 0x00, 0x00, 0x7f, 0x00, 0x01, 0x00, 0x00, 0x88, 0x9f, 0xfa, 0xfd, 0x9f, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]

在python中使用struct模块

import struct

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

s.connect((‘127.0.0.1’,8000))

*date=struct.pack("%dB"%(len(a)),a) (重点!!!)

s.send(date)

就可以直接用send发送了。

date数据如下: b’\x00\x00\x00,c\x00\x00\x00uK\xef\x0f\x00\x03\x00\x00\x01\x00\x00\x7f\x00\x01\x00\x00\x88\x9f\xfa\xfd\x9f"\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x01’

猜你喜欢

转载自blog.csdn.net/weixin_42404595/article/details/89886095