grpc-python03-常用protobuf数据类型和pb文件

 

 

 

 

# 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 {}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值