python client端收不到server端构造的结构体数据

python thrift server开发中发现thrift结构体中存在中文字符的时候client端收不到,后来发现是本身数据是unicode编码,在经过./python2.7/site-packages/thrift/protocol/TBinaryProtocol.py 这个文件的121行处理字符串的地方需要改一下,对于unicode编码的地方应该强转一下utf8,因为默认走的是utf8

find -name TBinaryProtocol.py

找到/python2.7/site-packages/thrift/protocol/TBinaryProtocol.py 

def writeString(self, str):
  if type(str) is unicode:#
    str = str.encode('utf-8')#
  self.writeI32(len(str))
  self.trans.write(str)

之后在运行的时候client端对于中文编码问题完美解决~

对于非中文的地方不需要修改

 

#!/usr/bin/env python
#coding:utf-8

import sys
reload(sys)
sys.setdefaultencoding('utf-8')
_author_ = 'liqianqian@didichuxing.com'
import socket
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift import TSerialization
from thrift import Thrift
from thrift.server import TServer

sys.path.append("../data/datalib/gen-py")
import logging#这两行必须要添加,否则会报thrift找不到handler绑定错误
logging.basicConfig(level=logging.DEBUG)#这两行必须要添加,否则会报thrift找不到handler绑定错误
from data_connect import DataConnect
from data_connect.ttypes import *
from utils.es_data import *
from order.ttypes import *
from price.ttypes import *
from route.ttypes import *
from user_profile.ttypes import *

class DataConnectHandler:
  def GetObject(self,struct_name, product_id, objid):
    fieldname = fetchunikey(struct_name, product_id)
    data_id = fetchdataid(fieldname, objid, struct_name, product_id)
    data_obj = construct_data(data_id, product_id)
    return data_obj

 

handler = DataConnectHandler()
processor = DataConnect.Processor(handler)
transport = TSocket.TServerSocket("localhost", 9090)
#tfactory = TTransport.TBufferedTransportFactory()
tfactory = TTransport.TFramedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)

#用下面这两个函数可以查看未经过网络的时候在协议层编码是不是改变了

def serialize(obj):
  trans = TTransport.TMemoryBuffer()
  prot = pfactory.getProtocol(trans)
  obj.write(prot)
  return trans.getvalue()

def deserialize(data):
  prot = pfactory.getProtocol(TTransport.TMemoryBuffer(data))
  ret = Route()
  ret.read(prot)
  return ret

obj = Route(route_setuptime=None, to_address=None, from_address=None, dest_poi_id=None, county=None, seat_count=None, create_time=1542012692, navi_distance=7, business_area=None, user_id=2001, setup_upper=None, to_county=None, county_name=None, total_passenger_count=None, channel=1, starting_poi_id=None, from_name=u'\u5317\u4eac', to_county_name=None, setup_type=None, from_lat=39.97, routeid=4014, to_name=None, from_business_area=None, to_lng=116.41, departure_time=u'17:21', to_lat=40.02, auto_striv_settings=None, navi_dis_type=0, route_name=None, setup_lower=None, from_lng=116.36, coord_type=None, from_city_id=1, to_city_id=1, route_eta_time=-1)
data = serialize(obj)
print deserialize(data)

posted @ 2018-11-14 14:30  Hedy_lqq  阅读(261)  评论(0编辑  收藏  举报