Python ZMQ发送消息乱码
Python ZMQ transmitting message garbled
我的服务器在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" )
我的 C 客户端:
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 );
当我从C客户端发送消息时,python打印的数据是乱码,像这样:
Received request: @c�SxH���C��
%�.075600;C�
%�;C �
%0.075600�C@�
%�`�
%���
%���
%0.075600%���
%���
%����C��
如何解码 Python 中的消息以正确打印出来?
C 端代码仅发送“1D”行纯字节:char[]
while
python3 在“2D”中思考,期待两者:{ bytes[], encoding }
这导致 "string{0:s}".format( message )
执行中的反解释,因为迷你模板期望 message
确实是 "fully-equipped" python3-字符串,它不是。
print( ":::{0:s}:::".format( str( message, 'utf-8' ) ) ) # ought fix the game
另一个想法是引入一些线路映射器(或更好的协议)
以便明确控制字节映射内容处理。
在 QuantFX 模块中,多方分布式 FX 引擎/ML 预测处理中的各方遵守协议规范,使用 struct.unpack()
python 边,一旦 aMiniRESOPONDER()
有 .recv()
-ed aMSG
.
这里的整个麻烦减少到只是协调协议版本控制,因此任何目标节点都可以使远程分布式处理即时适应适当的协议版本。
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
)
我的服务器在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" )
我的 C 客户端:
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 );
当我从C客户端发送消息时,python打印的数据是乱码,像这样:
Received request: @c�SxH���C��
%�.075600;C�
%�;C �
%0.075600�C@�
%�`�
%���
%���
%0.075600%���
%���
%����C��
如何解码 Python 中的消息以正确打印出来?
C 端代码仅发送“1D”行纯字节:char[]
while
python3 在“2D”中思考,期待两者:{ bytes[], encoding }
这导致 "string{0:s}".format( message )
执行中的反解释,因为迷你模板期望 message
确实是 "fully-equipped" python3-字符串,它不是。
print( ":::{0:s}:::".format( str( message, 'utf-8' ) ) ) # ought fix the game
另一个想法是引入一些线路映射器(或更好的协议)
以便明确控制字节映射内容处理。
在 QuantFX 模块中,多方分布式 FX 引擎/ML 预测处理中的各方遵守协议规范,使用 struct.unpack()
python 边,一旦 aMiniRESOPONDER()
有 .recv()
-ed aMSG
.
这里的整个麻烦减少到只是协调协议版本控制,因此任何目标节点都可以使远程分布式处理即时适应适当的协议版本。
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
)