Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 分析数据包时出错:解包需要长度为的字符串参数_Python_Structure_Packet - Fatal编程技术网

Python 分析数据包时出错:解包需要长度为的字符串参数

Python 分析数据包时出错:解包需要长度为的字符串参数,python,structure,packet,Python,Structure,Packet,我正在解析一个写在二进制文件中的数据包。我必须检索不同变量中的每个字段,因为我必须验证它们。在某些字段上使用struct后出现错误: (pk_dst,)=struct.unpack('!I', line[16:20])#ip_dst struct.error: unpack requires a string argument of length 4 我已经搜索过这个错误,但在每种情况下,它似乎都是一个独特的问题。我不明白为什么struct.unpack可以与IP地址源(src)完美地配合使用

我正在解析一个写在二进制文件中的数据包。我必须检索不同变量中的每个字段,因为我必须验证它们。在某些字段上使用struct后出现错误:

(pk_dst,)=struct.unpack('!I', line[16:20])#ip_dst struct.error: unpack
requires a string argument of length 4
我已经搜索过这个错误,但在每种情况下,它似乎都是一个独特的问题。我不明白为什么struct.unpack可以与IP地址源(src)完美地配合使用,但如果我有错误,就不能与目标(dst)配合使用

我的代码是:

with open(filename, 'rb') as f:
        for line in f.readlines():
            mask1=0b1110000
            mask2=0b0001111
            ba = bytearray(line)
            byte=ba[0]
            frstb=byte & mask1 
            pk_ver=frstb >> 4 #ip_vr
            print 'Ver:',pk_ver
            pk_hl=byte & mask2 #ip_hl
            print 'HL:',pk_hl
            #packet= struct.unpack('BBHHHBBHLL140s', line)
            (pk_tos,)= struct.unpack('!B', line[1]) #ip_tos
            print 'TOS:',pk_tos
            (pk_len,)=struct.unpack('!H', line[2:4])#ip_len
            print 'LEN:',pk_len
            (pk_id,)= struct.unpack('!H', line[4:6])#ip_id
            print 'ID=',pk_id
            (pk_off,)= struct.unpack('!H', line[6:8])#ip_off
            print 'OFF:',pk_off
            (pk_ttl,)=struct.unpack('@B', line[8])#ip_ttl
            print 'TTL:',pk_ttl
            (pk_p,)=struct.unpack('@B', line[9])#ip_p
            print'PROT:',pk_p
            (pk_chsum,)=struct.unpack('!H', line[10:12])#ip_sum
            print'CHSUM:',pk_chsum
            (pk_src,)=struct.unpack('!I', line[12:16])#ip_src
            print'SRC:',pk_src
            (pk_dst,)=struct.unpack('!I', line[16:20])#ip_dst
数据包的结构采用不同代码的C语言:

struct cip {
   uint8_t        ip_hl:4, /* both fields are 4 bits */
                  ip_v:4;
   uint8_t        ip_tos;
   uint16_t       ip_len;
   uint16_t       ip_id;
   uint16_t       ip_off;
   uint8_t        ip_ttl;
   uint8_t        ip_p;
   uint16_t       ip_sum;
   struct in_addr ip_src;
   struct in_addr ip_dst;
   char       head[100];
};
希望有人能告诉我我的代码有什么问题

这是数据包中的数据(从生成打包的程序中提取),因此您将看到数据不大于或小于4字节

HL:10 
Ver:4 
TOS:0 
Len:15360 
id:14082 
offset:64 
ttl:5 
Prot:6 
SUM:62141
src:167880896 
dst:184658112 
header:QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ


没有人能帮我吗?我什么都试过了。。。我想。我知道是什么问题!我正在读取的行数小于我试图解压缩的数据量。我猜我没有用readline()读取全部数据