【问题标题】:Python message partition errorPython消息分区错误
【发布时间】:2011-08-04 08:37:29
【问题描述】:

我通过 TCP 收到以下消息:

{"message": "Start", "client": "134.106.74.21", "type": 1009}<EOM>

但是当我尝试分区时

msg.partition( "<EOM>" )

我得到以下数组:

('{\x00\x00\x00"\x00\x00\x00m\x00\x00\x00e\x00\x00\x00s\x00\x00\x00s\x00\x00\x00a\x00 \x00\x00g\x00\x00\x00e\x00\x00\x00"\x00\x00\x00:\x00\x00\x00 \x00\x00\x00"\x00\x00\x00# \x00\x00\x00B\x00\x00\x00E\x00\x00\x00G\x00\x00\x00I\x00\x00\x00N\x00\x00\x00;\x00\x00 \x00A\x00\x00\x00l\x00\x00\x00l\x00\x00\x00;\x00\x00\x000\x00\x00\x00;\x00\x00\x001\x00\x00 \x00;\x00\x00\x000\x00\x00\x00;\x00\x00\x001\x00\x00\x003\x00\x00\x004\x00\x00\x00.\x00\x00 \x001\x00\x00\x000\x00\x00\x006\x00\x00\x00.\x00\x00\x007\x00\x00\x004\x00\x00\x00.\x00\x00 \x001\x00\x00\x002\x00\x00\x005\x00\x00\x00:\x00\x00\x003\x00\x00\x000\x00\x00\x000\x00\x00 \x000\x00\x00\x000\x00\x00\x00;\x00\x00\x00#\x00\x00\x00E\x00\x00\x00N\x00\x00\x00D\x00\x00 \x00"\x00\x00\x00,\x00\x00\x00 \x00\x00\x00"\x00\x00\x00c\x00\x00\x00l\x00\x00\x00i\x00\x00 \x00e\x00\x00\x00n\x00\x00\x00t\x00\x00\x00"\x00\x00\x00:\x00\x00\x00\x00\x00\x00"\x00 \x00\x001\x00\x00\x003\x00\x00\x004\x00\x00\x00.\x00\x00\x001\x00\x00\x000\x00\x00\x006 \x00\x00\x00.\x00\x00\x007\x00\x00\x004\x00\x00\x00.\x00\x00\x001\x00\x00\x002\x00\x00 \x005\x00\x00\x00"\x00\x00\x00,\x00\x00\x00 \x00\x00\x00"\x00\x00\x00t\x00\x00\x00y\x00 \x00\x00p\x00\x00\x00e\x00\x00\x00"\x00\x00\x00:\x00\x00\x00 \x00\x00\x002\x00\x00\x000 \x00\x00\x000\x00\x00\x005\x00\x00\x00}\x00\x00\x00\x00\x00\x00{“消息”:“开始”,“客户端”:“134.106.74.21”,“类型”:1009}', '', '')

更新

try:                    
    #Check if there are messages, if don't than throwing an exception otherwise continue
    ans = self.request.recv( 20480 )                        
    if( ans ):                        
          recv = self.getMessage( recv + ans )
    else:
          #Master client disconnected
          break
except:
 ...

def getMessage( self, msg ):
        print( "masg:" + msg );   
        aSplit = msg.partition( "<EOM>" )                                                                  
        while( aSplit[ 1 ] == "<EOM>" ):
            self.recvMessageHandler( json.loads( aSplit[ 0 ] ) )            
            #Get the new message id any
            msg = aSplit[ 3 ]      
            aSplit = msg.partition( "<EOM>" )
        return msg;  

当我尝试添加两个字符串时出现问题。

recv + ans

【问题讨论】:

标签: python data-partitioning


【解决方案1】:

如果您print msg.encode("hex"),那么您可能会看到这正是字符串中的内容。

无论如何,您可能已经注意到结果的每 4 个字节就是您所期望的字符之一。这表明您有一个未正确处理的 UCS4 Unicode 字符串。

您收到 UCS4 编码的字节了吗?如果是这样,那么您应该将它们填充到 unicode 字符串 u"".append(stuff) 中。但是,如果您正在接收 UCS4 编码的字节并且您对发送者有任何影响,那么您确实应该更改传输和接收 UTF-8 编码的字符串,因为这在网络连接上更为正常。

您确定 5 个文字字节 确实是您需要用于分区的分隔符。还是应该是名为 EOM 的单字节 ASCII 代码?还是 UCS4 编码的 u"&lt;EOM&gt;"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    • 2017-04-21
    • 2018-11-11
    • 2016-06-20
    • 2020-10-16
    • 1970-01-01
    相关资源
    最近更新 更多