how to use xdrlib
thomas at jollybox.de
Sun Aug 22 12:41:55 CEST 2010
On Sunday 22 August 2010, it occurred to Alan Wilter Sousa da Silva to
> Hi there,
> I am trying to understand how xdrlib works as I want to read files in this
> format. The problem is I don't much about xdr (although I read
> http://docs.python.org/library/xdrlib.html and RFC 1832).
> Another problem is I don't know how the file I want to read was encoded.
> So when I do something like:
> import xdrlib
> f = open('file.xdr').read()
> data = xdrlib.Unpacker(f)
> Then, I don't know which "unpack_*" to use.
If you actually have read RFC 1832, then this surprises me: as far as I can
see, and I have only skimmed the RFC so I may be wrong, it includes no way to
specify the type of a piece of data -- you have to know what you're reading.
> If I use,
> sometimes it returns something meaningful like:
This happens when the data was actually a string -- so you correctly used
> but other times,
> r Dih.\x00'
Here, you read data that was not originally a string as if it were one.
What the xdrlib module did is: it read four bytes. Probably 00 00 00 24. And
it interpreted these to be the length of the string you're trying to read.
Actually, you probably should have read an int first.
After that, you could have called unpack_string, which would have read in
00 00 00 04 -- aha, a four-long string -- and then read another four bytes,
the actual string: "Bond". Similarly, "Angle" has length 0x00000005, it's
followed by padding unto 4-byte margins, followed by the length of "Proper
Dih.", which happens to be 0x0000000b.
> if not a error.
That might happen if the number xdrlib interprets as the string length is
larger than the length of the rest of the file.
> Well, as you see, I am a bit lost here and any hint would be very
Basically, you have to know which file format you're dealing with, and use the
right unpack functions in the correct order for the specific file you're
dealing with. So you need some documentation for the file format you're using
-- XDR (like Microsoft's StructuredStorage, or even XML) doesn't as of itself
make any claims about the nature or structure of the data it holds.
More information about the Python-list