grpc 安装与简单测试-python语言

1、gRPC 的安装:
    $ pip install grpcio
    
2、安装 ProtoBuf 相关的 python 依赖库:
    $ pip install protobuf

3、安装 python grpc 的 protobuf 编译工具:
    $ pip install grpcio-tools


4、创建定义的文件 helloworld.proto 内容如下:
    syntax = "proto3";
    // The greeting service definition.
    service Greeter {
      // Sends a greeting
      rpc say_hello (HelloRequest) returns (HelloReply) {}
    }
    
    // The request message containing the user's name.
    message HelloRequest {
      string name = 1;
    }
    
    // The response message containing the greetings
    message HelloReply {
      string message = 1;
    }
    

5、编译服务器和客户端文件
    python -m grpc_tools.protoc -I ./  --python_out=. --grpc_python_out=.  ./helloworld.proto
    
    当前目录下生成
    helloworld_pb2_grpc.py
    helloworld_pb2.py
    
    
6、创建服务端文件 greeter_server.py
    # Copyright 2015 gRPC authors.
    #
    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at
    #
    #     http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
    """The Python implementation of the GRPC helloworld.Greeter server."""
    
    from concurrent import futures
    import time
    import logging
    
    import grpc
    
    import helloworld_pb2
    import helloworld_pb2_grpc
    
    _ONE_DAY_IN_SECONDS = 60 * 60 * 24
    
    
    class Greeter(helloworld_pb2_grpc.GreeterServicer):
    
        def say_hello(self, request, context):
            return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)
    
    
    def serve():
        server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
        helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
        server.add_insecure_port('[::]:50051')
        server.start()
        try:
            while True:
                time.sleep(_ONE_DAY_IN_SECONDS)
        except KeyboardInterrupt:
            server.stop(0)
    
    
    if __name__ == '__main__':
        logging.basicConfig()
        serve()
    
7、创建客户端文件  greeter_client.py 
    # Copyright 2015 gRPC authors.
    #
    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at
    #
    #     http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
    """The Python implementation of the GRPC helloworld.Greeter client."""

    from __future__ import print_function
    import logging

    import grpc

    import helloworld_pb2
    import helloworld_pb2_grpc

    def run():
        # NOTE(gRPC Python Team): .close() is possible on a channel and should be
        # used in circumstances in which the with statement does not fit the needs
        # of the code.
        with grpc.insecure_channel('localhost:50051') as channel:
            stub = helloworld_pb2_grpc.GreeterStub(channel)
            response = stub.say_hello(helloworld_pb2.HelloRequest(name='pwp'))
        print("Greeter client received: " + response.message)


    if __name__ == '__main__':
        logging.basicConfig()
        run()

8、运行服务端 
    python greeter_server.py
    
    
9、运行客户端调用
    python greeter_client.py 
    打印:
    Greeter client received: Hello, pwp!
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值