python rpc client demo

邰博远
2023-12-01
#!/usr/bin/env python
#coding:utf-8
#coding=utf-8
#!/usr/bin/python
# -*- coding: utf-8 -*-
#!/usr/bin/python
# vim: set fileencoding=utf-8
#py文件编码 https://www.python.org/dev/peps/pep-0263/
import sys, glob,base64,hashlib,demjson,io

from rpc import RPCService
from rpc.ttypes import *

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.protocol import TCompactProtocol

try:
  transport = TSocket.TSocket('localhost', 8888)
  transport = TTransport.TFramedTransport(transport)
  protocol = TCompactProtocol.TCompactProtocol(transport)
  client = RPCService.Client(protocol)
  transport.open()

  print 'add()';
  ret=client.test('xxx');
  print(ret)

  request=RPCRequest();
  request.encode=False
  request.key="1024"
  sign="c1009fb1404efa06aab2c571a074e0a69f53eb23"
  data='{"action":"testfuck","ip":"127.0.0.101","module":"test_ok","name":"来自美国的邮件","timestamp":"15 Dec 2015 06:25:48 GMT","token":"token","userid":"1024","version":"2.0"}';

  dic=demjson.decode(data)
  print(dic)
  print(len(dic))
  print(dic.keys())
  print(dic.items())

  for key,value in dic.items():
      print('%s=%s' % (key,value))

  builder = io.StringIO();
  for value in sorted(dic.values()):
     builder.write("%s,"%value);

  str=builder.getvalue();
  print(str)
  print(sign)
  #demjson.decode(data);
  sha1 = hashlib.sha1();
  sha1.update(str.encode('utf-8'));
  newsign=sha1.hexdigest();
  print(newsign)

  request.sign=newsign;
  request.data=data;
  resonpse=client.post(request)

  print(resonpse)
  print(resonpse.data)
  response_data=resonpse.data
  print(demjson.decode(response_data))

  transport.close()

except Thrift.TException, tx:
  print '%s' % (tx.message)

 类似资料:

相关阅读

相关文章

相关问答