Take a look at the struct module (http://docs.python.org/lib/module-struct.html), it does what you want.