zeromqpyzmqjzmq

Python ZMQ transmitting message garbled


My server in python:

import time
import zmq

context = zmq.Context()
socket  = context.socket( zmq.REP )
socket.bind( "tcp://*:5555" )

while True:
    #  Wait for next request from client
    message = socket.recv()
    print( "Received request: %s" % message )

    #  Do some 'work'
    time.sleep( 1 )

    #  Send reply back to client
    socket.send( b"World" )

My C client:

    std::string str = std::to_string(bar.open)   + ';'
                    + std::to_string(bar.high)   + ';'
                    + std::to_string(bar.low)    + ';'
                    + std::to_string(bar.close)  + ';'
                    + std::to_string(bar.volume) + ';'
                    + bar.time                   + ';'
                    + std::to_string(bar.hour)   + ';'
                    + std::to_string(bar.minute) + ';'
                    + bar.date                   + ';'
                    + bar.symbol;
    void *context   = zmq_ctx_new ();
    void *requester = zmq_socket ( context, ZMQ_REQ );
    zmq_connect ( requester, "tcp://localhost:5555" );

    char buffer [10];
    printf (  "Sending data to python module\n" );

    zmq_send ( requester, static_cast<void*>(&str), 1000, 0 );
    zmq_recv ( requester, buffer, 10, 0 );
    printf (  "Received %s\n", buffer );

    zmq_close ( requester );

When I send a message from C client, the data printed in python is garbled, like this:

Received request: @c�SxH���C��
                           %�.075600;C�
                                       %�;C �
                                             %0.075600�C@�
                                                          %�`�
                                                              %���
                                                                  %���
                                                                      %0.075600%���
   %���
       %����C��

How can I decode the message in Python to print out correctly?


Solution

  • C-side code sends just "1D" row-of-plain-bytes: char[]
    while

    python3 thinks in "2D", expecting both: { bytes[], encoding }

    this results in desinterpretation inside "string{0:s}".format( message ) execution, as the mini-template expects the message to be indeed a "fully-equipped" python3-string, which it fails to be.

    print( ":::{0:s}:::".format( str( message, 'utf-8' ) ) ) # ought fix the game
    

    Another idea is to introduce some wire-line mapper ( or better a protocol )

    so as to explicitly control byte-mapped content handling.

    In a QuantFX module, the parties in a multiparty distributed FX-engine / ML-predictions processing adhere to a protocol-specification, using struct.unpack() on python side, once aMiniRESOPONDER() has .recv()-ed aMSG.

    The whole trouble here reduces to just coordinated protocol-version-control, so any target node can adapt remote-distributed processing to the appropriate protocol version on-the-fly.

                pass;                     #aMSG_STRUCT_MASK = '!IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII'
                pass;                      aMSG_STRUCT_MASK = "!" + "I" * ( v41_HeaderSIZE + ( v41_nBarsDEPTH * 7 ) )
                #----DATA------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                aMSG_DATA = struct.unpack( aMSG_STRUCT_MASK, aMSG )
                #----INT-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                #DSegINT  = np.asarray(    aMSG_DATA[             2:],              # SKIP HEADER: Open_0, CurrentPRICE
                aDSegINT  = np.asarray(    aMSG_DATA[v41_HeaderSIZE:],              # SKIP HEADER: Open_0, CurrentPRICE [v412: MQL4_Digits, MQL4_GetTickCountOnSEND ]
                                           order = 'F'                              # FORTRAN-column-major-aligned / caching / speed
                                           )