Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing binary data to a socket (or file) with Python

Tags:

python

Let's say I have a socket connection, and the 3rd party listener on the other side expects to see data flowing in a very structured manner. For example, it looks for an unsigned byte that denotes a type of message being sent, followed by an unsigned integer that denotes the length of message, then another unsigned byte which is really a bit field with some flags set or unset and etc...

How would I do this in python? I'm just wondering how to reliably generate this data and make sure I'm sending it correctly (i.e. that I'm really sending an unsigned byte rather than say a signed integer or worse, a string).

like image 814
Tuxmentat Avatar asked Mar 26 '09 15:03

Tuxmentat


1 Answers

Use the struct module to build a buffer and write that.

like image 54
S.Lott Avatar answered Oct 10 '22 08:10

S.Lott