Python struct.error:结构格式错误的char [英] Python struct.error: bad char in struct format

查看:757
本文介绍了Python struct.error:结构格式错误的char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想知道这个python语句的作用:

first of all I would like to know what this python sentence does:

struct.unpack("!%sH" % (len(data) / 2), data))

其次,我必须使用python发出ICMP请求消息,而且事实是我已经得到了老师给我帮助的一些代码:

And secondly, I have to make an ICMP request message using python, and the thing is that I already got some code the teacher gave me to help:

def step4(code, server, port):
    s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname('icmp'))

    CHARMAP = nonprintable_to_dots()

    packet = create_packet(int(code))

    s.sendto(packet, (server, port))

    while True:
        msg = s.recv(1600)

        if not msg:
            break

        ihl = (ord(msg[0]) & 0x0F) * 4
        icmp = msg[ihl:]

        hexdump(icmp, True)

        if cksum(icmp[:]) != 0:
            print("wrong ckecksum!!")

def nonprintable_to_dots():
    return str.join('', [c if c in printable[:-5] else '.' for c in map(chr, range(256))])

def hexdump(frame, with_time=False):
    def to_chr(bytes):
        retval = bytes.translate(CHARMAP)
        return retval[:8] + ' ' + retval[8:]

    def to_hex(bytes):
        retval = str.join(' ', ["%02X" % ord(x) for x in bytes])
        return retval[:23] + ' ' + retval[23:]

    if with_time:
        print('--' + time.strftime("%H:%M:%s"))

    for i in range(0, len(frame), 16):
        line = frame[i:i + 16]
        print('%04X  %-49s |%-17s|' % (i, to_hex(line), to_chr(line)))

    print

def cksum(data):

    def sum16(data):
        "sum all the the 16-bit words in data"
        if len(data) % 2:
            data += '\0'

        return sum(struct.unpack("!%sH" % (len(data) / 2), data))

    retval = sum16(data)                       # sum
    retval = sum16(struct.pack('!L', retval))  # one's complement sum
    retval = (retval & 0xFFFF) ^ 0xFFFF        # one's complement

    return retval

问题是,从理论上讲所有这些代码都是正确的,所以我只应该执行"create_packet"功能,该功能包括在这里:

The thing is that theoretically all this code is right, so I only should do the "create_packet" function, which I include here:

def create_packet(code):
    ICMP_REQUEST = 8
    checksum = 0
    identifier = 7
    sequence_number = 7

    message_header = struct.pack("!BBHHH", ICMP_REQUEST, 0, checksum, identifier, sequence_number)

    message_payload = struct.pack("!I", code)

    checksum = cksum(message_header + message_payload)

    message_header = struct.pack("!BBHHH", ICMP_REQUEST, 0, checksum, identifier, sequence_number)

    return message_header + message_payload

每次执行脚本时,总是会出现此错误:

Whenever I execute the script, I always get this error:

Traceback (most recent call last):
    File "gymkhana.py", line 256, in <module>
        main()                                                                                                                
    File "gymkhana.py", line 19, in main
        step4(identifier, server, ginkana_port)
    File "gymkhana.py", line 181, in step4
        packet = create_packet(int(code))
    File "gymkhana.py", line 211, in create_packet
        checksum = cksum(message_header + message_payload)
    File "gymkhana.py", line 248, in cksum
        retval = sum16(data)                       # sum
    File "gymkhana.py", line 246, in sum16
        return sum(struct.unpack("!%sH" % (len(data) / 2), data))                                                           
struct.error: bad char in struct format

推荐答案

此行的第一个问题的答案

As answer to your first question, this line

struct.unpack("!%sH" % (len(data) / 2), data)    # in Python 2
struct.unpack("!%sH" % (len(data) // 2), data)   # in Python 3

说,取data中的 n 个字节(示例: 4个字节),并将其解释为 n /2个无符号短整数(示例: 2个整数),每个2字节.开头的!大约是字节顺序,表示big-endian. %s会根据len(data) / 2的值转换为一个或多个数字,因此在示例中,它与操作相同

says, take the n bytes in data (example: 4 bytes) and interpret them as n/2 unsigned short integers (example: 2 integers) of 2 bytes each. The ! at the beginning is about byte order and means big-endian. %s gets translated to one or more digits depending on the value of len(data) / 2, so in the example, it is the same as doing

struct.unpack("!2H", data)

您的bad char in struct format异常是因为您提供的代码使用/除法运算符.这在Python 2中有效.但是在Python 3中,它必须为//.那是因为/在Python 2中表示整数除法,而在Python 3中表示浮点除法.因此在Python 3中,

Your bad char in struct format exception is because the code you present uses the / division operator. That is valid in Python 2. But in Python 3 it needs to be //. That is because / means integer division in Python 2, but floating-point division in Python 3. So in Python 3,

"!%sH" % (len(data) / 2)

出现为

struct.unpack("!2.0H", data)

这说明了您的bad char错误.

这篇关于Python struct.error:结构格式错误的char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆