[Tutor] Send binary/hex data to a TCP socket

Adam Bark adam.jtm30 at gmail.com
Thu Jan 25 03:05:27 CET 2007


On 25/01/07, Tod Haren <ke7fxl at gmail.com> wrote:
>
> I need to send a server at the other end of a socket a 36 byte
> "frame", where each byte represents a specific field in the custom
> data structure.    (If there are any Ham operators reading this, I'm
> talking about the AGW Packet Engine)
>
> The documentation for the server says to initialize each field to a
> binary zero(0x00).  How do I get a binary zero in Python?  The next
> step is to send this all to the server and subsequently receive a
> similarly formated frame.
>
> A very basic frame looks something like this(modified from documentation):
>
>     |00 00 00 00 4D 00 00 00 00 00 00 00 00 00 00 00 |....M...........
>     |00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................
>     |00 00 00 00 |....
>
> My problem is that hex(0) return '0x0' and hex(ord('M')) returns
> '0x4d'.  If I concatonate these all together then the socket receives
> 109 bytes instead of 36.  The documentation examples are in C++, so
> they are little help to me.  The examples make use of the MoveMemory
> function for building the frames, but I'm clueless what that does.
>
> Pseudo code for the frame above:
>
>   l = []
>   for i in xrange(36):
>     l.append(hex(0))
>
>   l[4]=hex(ord('M'))
>
>   d=''
>   d=d.join(l)  #I need some other data type to send besides a string!
>   sock.send(d)  #send the 36 bytes to the socket
>
> Any pointers or references would be greatly appreciated.


 This should do what you need:
http://docs.python.org/lib/module-struct.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070125/c9251b7b/attachment.html 


More information about the Tutor mailing list