Socket - send message

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bierny
    New Member
    • Feb 2007
    • 12

    Socket - send message

    Hi,
    I would like to write a script which will connect to a server. I know I have to set up a tcp connection and after that send a registration message which consists of tag, length and value fields. All fields are bytes.
    I have found a package "struct" which translates string to binary format but unfortunatelly I do not see appropriate format.

    How can i do that?

    Thanks in advance for any help/hints!

    Best regards,
  • Bierny
    New Member
    • Feb 2007
    • 12

    #2
    Hi,
    Ok, I found it :-)

    Let's say i want to send a message with following bytes "00201":

    Code:
    import struct
    
    msg = struct.pack("!bbbbb", 0, 0, 2, 0, 1)
    mySocket.send(msg)
    
    ...
    and later in order to unpack a received message:

    Code:
    unpacked_msg = struct.unpack
    That should work ;-)

    Best regards,

    Comment

    Working...