# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
"""Client and server classes corresponding to protobuf-defined services."""
import grpc
import hello_word_pb2 as hello__word__pb2
class SayHelloStub(object):
"""Missing associated documentation comment in .proto file."""
def __init__(self, channel):
"""Constructor.
Args:
channel: A grpc.Channel.
"""
# unary_unary 普通传输类型
self.HelloDonghao = channel.unary_unary(
# 相当于restful 转成的url
'/test.SayHello/HelloDonghao',
# 定义序列化与反序列化
request_serializer=hello__word__pb2.HelloDonghaoReq.SerializeToString,
response_deserializer=hello__word__pb2.HelloDonghaoResPonse.FromString,
)
# stream_stream 流传输
self.HelloTest = channel.stream_stream(
'/test.SayHello/HelloTest',
request_serializer=hello__word__pb2.HelloTestReq.SerializeToString,
response_deserializer=hello__word__pb2.HelloTestResponse.FromString,
)
# 定义的服务
class SayHelloServicer(object):
"""Missing associated documentation comment in .proto file."""
def HelloDonghao(self, request, context):
"""Missing associated documentation comment in .proto file."""
# 定义状态码
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
# 定义错误信息
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def HelloTest(self, request_iterator, context):
"""Missing associated documentation comment in .proto file."""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
# 注册类到grpc server
def add_SayHelloServicer_to_server(servicer, server):
# prc方法处理器
rpc_method_handlers = {
# 普通传输
'HelloDonghao': grpc.unary_unary_rpc_method_handler(
servicer.HelloDonghao,
request_deserializer=hello__word__pb2.HelloDonghaoReq.FromString,
response_serializer=hello__word__pb2.HelloDonghaoResPonse.SerializeToString,
),
'HelloTest': grpc.stream_stream_rpc_method_handler(
servicer.HelloTest,
request_deserializer=hello__word__pb2.HelloTestReq.FromString,
response_serializer=hello__word__pb2.HelloTestResponse.SerializeToString,
),
}
generic_handler = grpc.method_handlers_generic_handler(
'test.SayHello', rpc_method_handlers)
# 针对中间件
server.add_generic_rpc_handlers((generic_handler,))
# This class is part of an EXPERIMENTAL API.
class SayHello(object):
"""Missing associated documentation comment in .proto file."""
# 定义的一些方法
@staticmethod
def HelloDonghao(request,
target,
options=(),
channel_credentials=None,
call_credentials=None,
insecure=False,
compression=None,
wait_for_ready=None,
timeout=None,
metadata=None):
return grpc.experimental.unary_unary(request, target, '/test.SayHello/HelloDonghao',
hello__word__pb2.HelloDonghaoReq.SerializeToString,
hello__word__pb2.HelloDonghaoResPonse.FromString,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)
@staticmethod
def HelloTest(request_iterator,
target,
options=(),
channel_credentials=None,
call_credentials=None,
insecure=False,
compression=None,
wait_for_ready=None,
timeout=None,
metadata=None):
return grpc.experimental.stream_stream(request_iterator, target, '/test.SayHello/HelloTest',
hello__word__pb2.HelloTestReq.SerializeToString,
hello__word__pb2.HelloTestResponse.FromString,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)
protobuf:
syntax = "proto3";
package test;
// 包的导入import "xxxxx"
service SayHello {
// 定义为流传输
rpc HelloTest(stream HelloTestReq) returns (stream HelloTestResponse) {};
}
message HelloTestReq {
string name = 1;
int64 age = 2;
repeated string data = 3;
// 相当于局部变量,只供HelloTestReq消息体使用
message HelloTestReqNumberValue {
string name = 1;
int32 age = 2;
bool is_active = 3;
}
// map<string, string> number = 4; 单种数据类型
// value多种数据类型
// 传递: number = {"a": pb2.xxxxNumber: {'name': 'xxx', 'age': xx, is_active: 'xxx'}}
map<string, HelloTestReqNumberValue> number = 4;
}
message HelloTestResponse {}