unpack requires a string argument of length 24

John

I am not sure what I am doing wrong here but I am trying to open a file, trace1.flow, read the header information then throw the source IP and destination IP into dictionaries. This is done in Python running on a Fedora VM. I am getting the following error:

(secs, nsecs, booted, exporter, mySourceIP, myDestinationIP) = struct.unpack('IIIIII',myBuf)
struct.error: unpack requires a string argument of length 24

Here is my code:

import struct
import socket


#Dictionaries
uniqSource = {}
uniqDestination = {}

def int2quad(i):
        z = struct.pack('!I', i)
        return socket.inet_ntoa(z)


myFile = open('trace1.flow')
myBuf = myFile.read(8)

(magic, endian, version, headerLen) = struct.unpack('HBBI', myBuf)
print "Magic: ", hex(magic), "Endian: ", endian, "Version: ", version, "Header Length: ", headerLen
myFile.read(headerLen - 8)

try:
        while(True):
                myBuf = myFile.read(24)
                (secs, nsecs, booted, exporter, mySourceIP, myDestinationIP) = struct.unpack('IIIIII',myBuf)
                mySourceIP = int2quad(mySourceIP)
                myDestinationIP = int2quad(myDestinationIP)

                if mySourceIP not in uniqSource:
                        uniqSource[mySourceIP] = 1
                else:
                        uniqSource[mySourceIP] += 1

                if myDestinationIP not in uniqDestination:
                        uniqDestination[myDestinationIP] = 1
                else:
                        uniqDestination[myDestinationIP] += 1

                myFile.read(40)

except EOFError:
        print "END OF FILE"
interjay

You seem to assume that file.read will raise EOFError on end of file, but this error is only raised by input() and raw_input(). file.read will simply return a string that's shorter than requested (possibly empty).

So you need to check the length after reading:

myBuf = myFile.read(24)
if len(myBuf) < 24:
    break

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Error unpack requires a string argument of length 16

分類Dev

tar: option requires an argument -- 'f'

分類Dev

Python - Unpack lists and join into a string

分類Dev

Python - Unpack lists and join into a string

分類Dev

The length of a string

分類Dev

Use gsub to remove pattern from a string: argument 'pattern' has length > 1 and only the first element will be used

分類Dev

Java multiple variable length argument

分類Dev

Retrieve the length of variable argument list

分類Dev

How to unpack a list of tuple in various length in a panda dataframe?

分類Dev

How can I unpack an arbitrary length list of IO Bool

分類Dev

unpackには、長さ24の文字列引数が必要です

分類Dev

Manually compute the length of a string

分類Dev

String length data for DynamoDB

分類Dev

Length in bytes of a String

分類Dev

Replace a string of defined length

分類Dev

String length always 0

分類Dev

Argument parsing inside a string

分類Dev

Overriding with object or string as argument

分類Dev

Passing a template which requires a comma to a single-argument macro

分類Dev

Not using a class in python, but my function requires a "self" argument?

分類Dev

How to unpack a string - created with sprintf - with fixed width in Perl

分類Dev

elasticsearch filter by length of a string field

分類Dev

Get length of string from scanf

分類Dev

Getting the length of a string in C++

分類Dev

Add string depending on counter length?

分類Dev

python input prompt string length

分類Dev

Get length of Substring from string

分類Dev

C++ Length of a string in an Array

分類Dev

Bash remove quotes for String argument

Related 関連記事

ホットタグ

アーカイブ