Reputation: 1
I have a network message of the format msgHdr+payload
. The msgHdr
has a couple of Longs and a couple of shorts. The Payload is a variable length text. I am not sure how to format the Struct, so that I can send a packed binary stream.
The format for a single message is,
s = struct.Struct('> L L I I 2110s')
s.pack(*mystruct) # Then I pack it
However, this only works for a fixed size string of 2110. How do I use it for a variable length payload? Should I be using something else?
Upvotes: 0
Views: 853
Reputation: 1889
Use struct
for the header and just add the payload afterwards.
message = struct.pack('> L L I I', *header) + payload
Upvotes: 1
Reputation: 17016
It seems like you could create the format string (which is, after all, just a string), and then use it with s.pack as you describe.
If your current syntax is
s = struct.Struct('> L L I I 2110s')
s.pack(*mystruct)
all you would need to do is
s = struct.Struct('> L L I I %ds' % size_of_data)
s.pack(*mystruct)
It might help to create the header first as one struct, then the variable length section, then concatenate them.
Upvotes: 0