Reading and manipulating binary data

Dave davbucko at yahoo.com
Sat Aug 30 11:50:33 EDT 2003


Hi,

I am really confused as to how to use binary data with python.  I am
currently developing a lightweight RADIUS server, which gets UDP datagrams
from a client formatted like so:

    0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |     Code      |  Identifier   |            Length             |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                                                               |
   |                     Request Authenticator                     |
   |                                                               |
   |                                                               |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |  Attributes ...
   +-+-+-+-+-+-+-+-+-+-+-+-+-

I can read() the data easily enough from the socket.  However, when I have
read it, it is very difficult to manipulate.  I can't convert it to an
integer or a string which is driving me crazy...  I have seen a lot of posts
recommending struct for dealing with binary data, but I can't get it to like
mine.  I get all sorts of errors, like lengths don't match format etc.

Also part of the authentication system involves MD5 hashes.  For this I need
to be able to take a string, find its MD5 digest, and then xor it with
another string.  But since the MD5 digest is binary, and I don't know how to
change it, I can't xor it...  Please, I am sure I am missing something
really obvious but I am stumped as to what it is.

The basic RADIUS rfc is 2865.  Please say if I have seriously misinterpreted
it (as you can probably tell, this is my first time trying something more
than "Hello World" with sockets).

Thanks in advance

Dave









More information about the Python-list mailing list