1

在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 架构是 32 位的。

4

2 回答 2

0

Even in the error line is pointing to this line, the error is not located there. Executing this instruction in the Python interpreter produces no error:

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

This makes sense, as the error is complaining on the 'L' format code, so the error will be located in the ones which use this format.

Given that 'L' is used for unsigned long, and the message complains on being out of range, the error is because one (or more) of the variables used are negative, producing the out of range for unsigned long.

This can be verified in the Python interpreter:

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
于 2016-10-15T16:22:52.323 回答
-2

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

jsonmsg["flow_id"]
jsonmsg["app_src_ip"]
jsonmsg["app_dst_ip"]
于 2012-08-16T16:20:02.533 回答