-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
49 lines (39 loc) · 1.4 KB
/
Copy pathclient.py
File metadata and controls
49 lines (39 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import sys
import grpc
from google.protobuf import wrappers_pb2
import rpc_meta_pb2
import rpc_meta_pb2_grpc
def run():
channel = grpc.insecure_channel('localhost:8080')
stub = rpc_meta_pb2_grpc.GrpcServiceStub(channel)
param = wrappers_pb2.StringValue(value='World')
rpc_request = rpc_meta_pb2.RpcRequest(
interface_name='com.xiaoyu.rpc.api.HelloService',
method_name='sayHello',
param_types=['java.lang.String'],
parameters=[param.SerializeToString()],
request_id='python-ci-request',
)
try:
response = stub.handle(rpc_request, timeout=5)
if response.message != 'Success':
raise RuntimeError(f'RPC failed: {response.message}')
result = wrappers_pb2.StringValue()
result.ParseFromString(response.data)
if 'World' not in result.value:
raise RuntimeError(f'Unexpected RPC result: {result.value!r}')
print('RpcResponse received:')
print(f'Data: {result.value}')
print(f'Message: {response.message}')
print(f'RequestID: {response.request_id}')
finally:
channel.close()
if __name__ == '__main__':
try:
run()
except grpc.RpcError as exc:
print(f'gRPC Error: {exc.code()} - {exc.details()}', file=sys.stderr)
sys.exit(1)
except Exception as exc:
print(f'Client Error: {exc}', file=sys.stderr)
sys.exit(1)