当前位置:首页 > python > 正文内容

python 使用grpc 的方法

root4年前 (2022-07-08)python1644

需要的依赖包

grpcio
googleapis-common-protos

首先需要根据proto 文件生成代码

proto 示例代码

syntax = "proto3";

service MsgService {
 rpc GetMsg (MsgRequest) returns (MsgResponse){}
}

message MsgRequest {
  string name = 1;
}

message MsgResponse {
  string msg = 1;
}

根据proto 生成py代码

python -m grpc_tools.protoc -I . --python_out=. --grpc_python_out=. msg.proto


server 端代码示例

import grpc
from grpc_demo import msg_pb2
from grpc_demo import msg_pb2_grpc

from concurrent import futures
import time

_ONE_DAY_IN_SECONDS = 60 * 60 * 24


class MsgServicer(msg_pb2_grpc.MsgServiceServicer):

    def GetMsg(self, request, context):
        print("Received name: %s" % request.name)
        return msg_pb2.MsgResponse(msg='Hello, %s!' % request.name)


def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    msg_pb2_grpc.add_MsgServiceServicer_to_server(MsgServicer(), 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__':
    serve()


客户端代码示例

import grpc
from grpc_demo import msg_pb2, msg_pb2_grpc

_HOST = 'localhost'
_PORT = '8080'


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 = msg_pb2_grpc.MsgServiceStub(channel)
        response = stub.GetMsg(msg_pb2.MsgRequest(name='world'))
    print("Client received: " + response.msg)


if __name__ == '__main__':
    run()


proto 中 repeated 对象的属性是列表 

py接收到数据的时候需要遍历获取内容

 请求的参数中某个某个字段如下

google.protobuf.Timestamp after = 3;

时间类型的数据转换

from google.protobuf.timestamp_pb2 import Timestamp
after = "000-00-00T00:00:00"
befer = "000-00-00T00:00:00"
timestamp_message_after = Timestamp()
timestamp_message_befer = Timestamp()
timestamp_message_after.FromJsonString(after)
timestamp_message_befer.FromJsonString(befer)



扫描二维码推送至手机访问。

版权声明:本文由一叶知秋发布,如需转载请注明出处。

本文链接:https://zhiqiu.top/?id=233

分享给朋友:

相关文章

python简单的加密解密

rsa 是非对称加密公钥加密,私钥解密pip install rsaimport rsa from binascii import b2a_hex, a2b_hex class&nb...

python 的configparser 读取配置文件遇到%特殊符号

test.ini 配置文件中有mysql的密码,且密码含有“%”这个特殊符号因为%在py是转义符的含义需要对该字符转义即修改  %  为 %%用%对%进行转义...

selenium控制webdriver  设置请求头。只能设置简单的。自定义和固定的格式无法修改成功

selenium控制webdriver 设置请求头。只能设置简单的。自定义和固定的格式无法修改成功

time selenium webdriver options webdriver.() options.() options.( options.() browser webdriver....

cmd启动python交互模式 出现UnicodeDecodeError: 'gbk' codec can't decode byte 0x9a in position 533

这是因为在python交互模式的中输出了中文,且是个输出被记录在.python_history中删除历史记录文件C:\Users\Administrator\.python_history...

python跟pip不是同一个版本的坑

python执行默认是Python2.7但是pip默认确实pip3的用pip install 安装包只会安装到python3环境里面指回pip vim /usr/local/bin/pip把 第一行的#!/usr/bin...

python 之optparse模块OptionParser

该模块让python脚本命令能够符合标准的Unix命令例程式每个命令行参数就是由参数名字符串和参数属性组成的。如 -f 或者 file 分别是长短参数名当你将所有的命令行参数都定义好了的时候,我们需要调用parse_args()方法赖际熙a...