python struct pack with space padding

pack, python, struct

Solution

struct has a placeholder (`x`) for a padding byte you can use:

 # pack 2 16 bit values plus one pad byte
 from struct import pack
 packedStrWithOneBytePad = pack("hhx", 1000, 2000)

Problem

I need to create/send binary data in python using a given protocol. The protocol calls for fixed width fields , with space padding thrown in. Using python's struct.pack, the only thing I can think of is, calculating the space padding and adding it in myself. Is there a better way to achieve this? thanks

Original source