Looking at your code, you have some byte order issues:
First, the i2w function. I'm too tired to actually fix this, so I went ahead and used the struct module
(you should look into using it, it makes life a lot easier):
"""Convert integer to 4 byte word in a string.
Will raise exception if overflows."""
#the < stands for little endian, and I for unsigned int
return struct.pack("<I", integer)
Then, when assembling the header, you still need to reverse p and m:
tmp = i2w(v) + p.decode('hex')[::-1] + m.decode('hex')[::-1] + i2w(t) + i2w(b) + i2w(n)
And lastly, when printing out the result, you need to reverse it too: