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

python 使用grpc 的方法

root3年前 (2022-07-08)python1028

需要的依赖包

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

分享给朋友:

相关文章

python2的pip 不能使用或者使用总是报错

python2的pip 不能使用或者使用总是报错

python2.7   当然可能还有其他情况有的是pip版本升级过高,有的是pip有点问题无法执行pip的命令升级python2的 pip 一定要小心推荐命令:pip install --upgrad...

python用requests发送模拟请求忽略https的认证,忽略警告

import warnings warnings.filterwarnings('ignore')在文件头添加忽略警告信息的输出r = requests.get('https://kyfw.12306.cn&#...

python 自定义好用logger模块

# -*- coding:utf-8 -*- import sys import logging.handlers DEFAULT_LOG_FMT = '...

flask 服务添加ssl 证书

flask 服务添加ssl 证书

1、利用openssl生成自用的ssl证书利用openssl 生成证书openssl genrsa -des3 -out server.key 2048不要密码:再执行 一下:openssl rsa -in server.key -out...

falsk &django +uwsgi 的配置文件

flask的uwsgi配置文件[uwsgi]pythonpath=/usr/bin/python3  #uwsgi采用的py版本,如果项目采用的py版本跟系统默认一直可以不用写base=/***/***/***  &nb...

Popen 使用注意(python)

Popen 的构造函数可以尝试指定一下 close_fds = True意思是不继承主线程的上下文件因为Popen在启动一个进程时容易出现僵尸进程,当主进程死亡时可能会继承主进程的上下文资源...