示例#1
0
def main():
    # 创建socket,连接localhost的9090端口
    transport = TSocket.TSocket('localhost', 9090)

    # 使用带buffer 传输层协议
    transport = TTransport.TBufferedTransport(transport)

    # 使用TBinaryProtocol protocal
    protocol = TBinaryProtocol.TBinaryProtocol(transport)

    # 创建client
    client = Calculator.Client(protocol)

    # 连接服务端
    transport.open()
    # 调用服务ping方法
    client.ping()
    print('ping()')
    # 调用服务端add方法
    sum_ = client.add(1, 1)
    print('1+1=%d' % sum_)


    # 关闭连接
    transport.close()
示例#2
0
def communicate():
    # create client
    transport = TTornado.TTornadoStreamTransport('localhost', 9090)
    # open the transport, bail on error
    try:
        yield transport.open()
        print('Transport is opened')
    except TTransport.TTransportException as ex:
        logging.error(ex)
        raise gen.Return()

    pfactory = TBinaryProtocol.TBinaryProtocolFactory()
    client = Calculator.Client(transport, pfactory)

    # ping
    yield client.ping()
    print("ping()")

    # add
    sum_ = yield client.add(1, 1)
    print("1 + 1 = {0}".format(sum_))

    # make a oneway call without a callback (schedule the write and continue
    # without blocking)
    client.zip()
    print("zip() without callback")

    # make a oneway call with a callback (we'll wait for the stream write to
    # complete before continuing)
    client.zip()
    print("zip() with callback")

    # calculate 1/0
    work = Work()
    work.op = Operation.DIVIDE
    work.num1 = 1
    work.num2 = 0

    try:
        quotient = yield client.calculate(1, work)
        print("Whoa? You know how to divide by zero ? -> {0}".format(quotient))
    except InvalidOperation as io:
        print("InvalidOperation: {0}".format(io))

    # calculate 15-10
    work.op = Operation.SUBTRACT
    work.num1 = 15
    work.num2 = 10

    diff = yield client.calculate(1, work)
    print("15 - 10 = {0}".format(diff))

    # getStruct
    log = yield client.getStruct(1)
    print("Check log: {0}".format(log.value))

    # close the transport
    client._transport.close()
    raise gen.Return()
示例#3
0
def main():
    # Make socket
    transport = TSocket.TSocket('localhost', 9090)

    # Buffering is critical. Raw sockets are very slow
    transport = TTransport.TBufferedTransport(transport)

    # Wrap in a protocol
    protocol = TBinaryProtocol.TBinaryProtocol(transport)

    # Create a client to use the protocol encoder
    client = Calculator.Client(protocol)

    # Connect!
    transport.open()

    client.ping()
    print('ping()')

    sum_ = client.add(1, 1)
    print('1+1=%d' % sum_)

    work = Work()

    work.op = Operation.DIVIDE
    work.num1 = 1
    work.num2 = 0

    try:
        quotient = client.calculate(1, work)
        print('Whoa? You know how to divide by zero?')
        print('FYI the answer is %d' % quotient)
    except InvalidOperation as e:
        print('InvalidOperation: %r' % e)

    work.op = Operation.SUBTRACT
    work.num1 = 15
    work.num2 = 10

    diff = client.calculate(1, work)
    print('15-10=%d' % diff)

    log = client.getStruct(1)
    print('Check log: %s' % log.value)

    result = client.request_matching_symbols(request_json="AA")

    print("IB Symbols query result AA = " + str(result))
    # Close!
    transport.close()
def main():
    transport = TSocket.TSocket('localhost', 9090)
    transport = TTransport.TBufferedTransport(transport)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)

    client = Calculator.Client(protocol)

    transport.open()

    client.ping()
    sum_ = client.add(1, 1)
    print("1+1=%d" % sum_)
    
    client.sub(2, 1)

    transport.close()
示例#5
0
def main():
    # Make socket
    transport = TLambdaClientTransport("tutorial-server")

    # Wrap in a protocol
    protocol = TBinaryProtocol.TBinaryProtocol(transport)

    # Create a client to use the protocol encoder
    client = Calculator.Client(protocol)

    # Connect!
    transport.open()

    print('going to ping')
    client.ping()
    print('ping()')

    sum_ = client.add(1, 1)
    print('1+1=%d' % sum_)

    work = Work()

    work.op = Operation.DIVIDE
    work.num1 = 1
    work.num2 = 0

    try:
        quotient = client.calculate(1, work)
        print('Whoa? You know how to divide by zero?')
        print('FYI the answer is %d' % quotient)
    except InvalidOperation as e:
        print('InvalidOperation: %r' % e)

    work.op = Operation.SUBTRACT
    work.num1 = 15
    work.num2 = 10

    diff = client.calculate(1, work)
    print('15-10=%d' % diff)

    log = client.getStruct(1)
    print('Check log: %s' % log.value)

    # Close!
    transport.close()
示例#6
0
def rpc(tk, procedure, params, logger):
    host, port = tk.get_provider_address().split(':')
    port = int(port)
    logger.info("provider address, host - {}, port - {}".format(host, port))

    socket = TSocket.TSocket(host, port)
    transport = TTransport.TBufferedTransport(socket)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = Calculator.Client(protocol)
    transport.open()

    if procedure == 'ping':
        client.ping()
    else:
        logger.error('unrecognized procedure {}'.format(procedure))
        sys.exit(1)

    transport.close()
示例#7
0
import json
from tutorial import Calculator, CcktvRoom
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.protocol.TMultiplexedProtocol import TMultiplexedProtocol

transport = TSocket.TSocket('127.0.0.1', 8000)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)

# 如果服务端使用TMultiplexedProcessor接收处理,客户端必须用TMultiplexedProtocol并且serviceName必须和服务端的一致
calculator_protocol = TMultiplexedProtocol(protocol, "calculator")
ccktv_room_protocol = TMultiplexedProtocol(protocol, "ccktv_room")
calc_client = Calculator.Client(calculator_protocol)
room_client = CcktvRoom.Client(ccktv_room_protocol)

transport.open()

print(
    calc_client.invoke(1, '1111-2222-3333-4444',
                       json.dumps({"name": "zhoujielun"})))
print(calc_client.sayMsg("avatar"))
res = room_client.getBannerList({'1': '11'}, 10)
print(res, type(res))

transport.close()

# transport = TSocket.TSocket('127.0.0.1', 8000)
# transport = TTransport.TBufferedTransport(transport)
# protocol = TBinaryProtocol.TBinaryProtocol(transport)
示例#8
0
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

try:

    # Make socket
    transport = TSocket.TSocket('localhost', 9090)

    # Buffering is critical. Raw sockets are very slow
    transport = TTransport.TBufferedTransport(transport)

    # Wrap in a protocol
    protocol = TBinaryProtocol.TBinaryProtocol(transport)

    # Create a client to use the protocol encoder
    client = Calculator.Client(protocol)

    # Connect!
    transport.open()
    print('Connected')

    client.ping()
    print('ping()')

    sum = client.add(1, 1)
    print('1+1=%d' % (sum))

    work = Work()

    work.op = Operation.DIVIDE
    work.num1 = 1
示例#9
0
from tutorial import Calculator
from tutorial.ttypes import *
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from prettytable import PrettyTable


class thrift_utils:
    transport = TSocket.TSocket('192.168.56.1', 9090)
    transport = TTransport.TBufferedTransport(transport)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    transport.open()


client = Calculator.Client(thrift_utils.protocol)


def print_rpt_grp(row, grp_name):
    rpt_vars = get_repeat_vars_map(row)
    maps_of_maps = rpt_vars.get(grp_name)
    headers = get_unique_headers(rpt_vars, grp_name)
    print_maps_of_maps(maps_of_maps, headers)


def convert_key_to_int(headers_map):
    new_map = {}
    for k, v in headers_map.iteritems():
        new_map[int(k)] = v
    return new_map