第一步:安装依赖包
googleapis-common-protos==1.54.0
grpcio==1.42.0
grpcio-tools==1.42.0
protobuf==3.19.1
six==1.16.0
pip3.9 install grpcio pip3.9 install grpcio-tools pip install googleapis-common-protos
第二步:写client
# coding=utf-8
ixxxort json
ixxxort grpc
from x'x'x ixxxort xxx_pb2_grpc, xxx_pb2
def run():
# 连接rpc服务器,insecure(不安全的)
"""
'xxx-xxx-grpc.xxx.test:xxx' 为服务端起的端口
"""
channel = grpc.insecure_channel('xxx-xxx-grpc.xxx.test:xxx')
# 调用rpc服务
"""
SecurityStub就是生成的xxx_pb2_grpc.py中的类class SecurityStub(object),
而Security就是grpc协议文档xxx.proto中的service Security,然后拼接成SecurityStub
stub.xxx就是xxx.proto中的rpc xxx,
xxx_pb2.RequestSecurity就是 rpc xxx(RequestSecurity)中的请求"RequestSecurity",
参数就是app_id=12345678就是 uint32 app_id = 1 定义的参数名app_id,
response获取响应的结果 xxx.proto的message ResponseSecurity
"""
stub = xxx_pb2_grpc.SecurityStub(channel)
request_data = xxx_pb2.RequestSecurity(xxx =12345678, xxx ="小米")
print(type(request_data))
print(request_data)
response = stub.xxx(request_data)
print(type(response))
print(response)
print(response.trace_id)
res = response.res
print(type(res))
print(res)
print(res[3].words)
print(res[2].words)
for k in response.res:
print(k)
print(type(k))
print(response.res[k])
if __name__ == '__main__':
run()
第三步:右键run client
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)