1#!/usr/bin/env python3 2# Copyright (c) 2023, Meta 3# 4# SPDX-License-Identifier: Apache-2.0 5 6"""Thrift Hello Client Sample 7 8Connect to a hello service and demonstrate the 9ping(), echo(), and counter() Thrift RPC methods. 10 11Usage: 12 ./hello_client.py [ip] 13""" 14 15import argparse 16import sys 17sys.path.append('gen-py') 18 19from thrift.protocol import TBinaryProtocol 20from thrift.transport import TTransport 21from thrift.transport import TSocket 22from hello import Hello 23 24 25def parse_args(): 26 parser = argparse.ArgumentParser(allow_abbrev=False) 27 parser.add_argument('--ip', default='192.0.2.1', 28 help='IP address of hello server') 29 30 return parser.parse_args() 31 32 33def main(): 34 args = parse_args() 35 36 transport = TSocket.TSocket(args.ip, 4242) 37 transport = TTransport.TBufferedTransport(transport) 38 protocol = TBinaryProtocol.TBinaryProtocol(transport) 39 client = Hello.Client(protocol) 40 41 transport.open() 42 43 client.ping() 44 client.echo('Hello, world!') 45 46 # necessary to mitigate unused variable warning with for i in range(5) 47 i = 0 48 while i < 5: 49 client.counter() 50 i = i + 1 51 52 transport.close() 53 54 55if __name__ == '__main__': 56 main() 57