python:L格式cod的整数超出范围

2024-09-24 02:16:18 发布

您现在位置:Python中文网/ 问答频道 /正文

在python中,代码如下

envimsg = struct.pack("!LHL", 1, 0, int(jsonmsg["flow_id"], 16)) + \
          struct.pack("!HQH", 1, int(flow["src id"],16), 0) + \
          struct.pack("!HQH", 1, int(flow["dst id"],16), int(flow["dst port"],16)) + \
          struct.pack("!H", 0) + \
          struct.pack("!HHHLL", int(jsonmsg["app_src_port"],10), int(jsonmsg["app_dst_port"],10), int(jsonmsg["app_proto"],10), int(jsonmsg["app_src_ip"],10), int(jsonmsg["app_dst_ip"],10))

排队

struct.pack("!H", 0) + \

我遇到这个错误:

  File "./Translate_2503.py", line 205, in lavi2envi
    struct.pack("!H", 0) + \
struct.error: integer out of range for 'L' format code

这很奇怪,因为我试着用H(无符号的短)。

有线索吗?

我的python版本2.7.3。 CPU archi是32位的。


Tags: 代码ipsrcidappportflowstruct
2条回答

最有可能的问题在于其中一项的价值:

jsonmsg["flow_id"]
jsonmsg["app_src_ip"]
jsonmsg["app_dst_ip"]

即使在错误行指向此行时,错误也不在此处。在Python解释器中执行此指令不会产生错误:

import struct
struct.pack("!H", 0)
>>> '\x00\x00'

这是有意义的,因为错误正在“L”格式代码上抱怨,所以错误将位于使用此格式的代码中。

假设“L”用于无符号long,并且消息抱怨超出范围,则错误是因为使用的一个(或多个)变量为负,从而产生无符号long的超出范围。

这可以在Python解释器中验证:

import struct

struct.pack("!HHHLL", 1, 2, 3, 4, 5)
>>> '\x00\x01\x00\x02\x00\x03\x00\x00\x00\x04\x00\x00\x00\x05'

struct.pack("!HHHLL", 1, 2, 3, -4, 5)
>>> Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
struct.error: integer out of range for 'L' format code

相关问题 更多 >