Ubuntu18.04安装gRPC

  • protobuf-compiler-grpc安装

    sudo apt-get install protobuf-compiler-grpc
  • protobuf-compiler安装

    sudo apt-get install protobuf-compiler
  • gRPC 的安装

    pip install grpcio
  • 安装 ProtoBuf 相关的 python 依赖库

    pip install protobuf
  • 安装 python grpc 的 protobuf 编译工具

    pip install grpcio-tools

编写示例工程

  • 工程结构

  • 编写 proto 文件

    在工程下新建stream目录,新建stream.proto文件,文件可名称任意
    syntax = "proto3";
    package stream;
    service StreamService {
    rpc SimpleFun(RequestData) returns (ResponseData){}
    }
    message RequestData {
    string text = 1;
    } message ResponseData {
    string text = 1;
    }
  • 编译 protobuf

    切换至stream目录,执行以下命令:

    python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. ./stream.proto

stream目录中执行编译,会生成:stream_pb2.pystream_pb2_grpc.py,需修正stream_pb2_grpc.py的引用stream__pb2的路径

  • 实现 server 端,simple_server.py
    #! /usr/bin/env python
    # -*- coding: utf-8 -*-
    import grpc
    import time
    from concurrent import futures
    from stream import stream_pb2, stream_pb2_grpc _ONE_DAY_IN_SECONDS = 60 * 60 * 24
    _HOST = 'localhost'
    _PORT = '8883' class servicer(stream_pb2_grpc.StreamServiceServicer): def SimpleFun(self, request, context):
    str = request.text
    print("received: " + str)
    return stream_pb2.ResponseData(text=('hello,gRPC')) def serve():
    grpcServer = grpc.server(futures.ThreadPoolExecutor(max_workers=4))
    stream_pb2_grpc.add_StreamServiceServicer_to_server(servicer(), grpcServer)
    grpcServer.add_insecure_port(_HOST + ':' + _PORT)
    grpcServer.start()
    try:
    while True:
    time.sleep(_ONE_DAY_IN_SECONDS)
    except KeyboardInterrupt:
    grpcServer.stop(0) if __name__ == '__main__':
    serve()
  • 实现 client端,simple_client.py
    #! /usr/bin/env python
    # -*- coding: utf-8 -*-
    import grpc
    from stream import stream_pb2, stream_pb2_grpc _HOST = 'localhost'
    _PORT = '8883' def run():
    conn = grpc.insecure_channel(_HOST + ':' + _PORT)
    client = stream_pb2_grpc.StreamServiceStub(channel=conn)
    response = client.SimpleFun(stream_pb2.RequestData(text='hello,world!'))
    print("received: " + response.text) if __name__ == '__main__':
    run()
  • 执行结果

    先启动simple_server.py再启动simple_client.py

  • 完整代码

    https://github.com/gm19900510/stream_grpc

python gRPC简单示例的更多相关文章

  1. Python Thrift 简单示例

    本文基于Thrift-0.10,使用Python实现服务器端,使用Java实现客户端,演示了Thrift RPC调用示例.Java客户端提供两个字符串参数,Python服务器端计算这两个字符串的相似度 ...

  2. python psutil简单示例

    python psutil简单示例 利用psutil编写简单的检测小脚本 0.安装psutil模块                                                    ...

  3. Websocket - Websocket原理(握手、解密、加密)、基于Python实现简单示例

    一.Websocket原理(握手.解密.加密) WebSocket协议是基于TCP的一种新的协议.WebSocket最初在HTML5规范中被引用为TCP连接,作为基于TCP的套接字API的占位符.它实 ...

  4. C#调用Python脚本的简单示例

    C#调用Python脚本的简单示例 分类:Python (2311)  (0)  举报  收藏 IronPython是一种在 .NET及 Mono上的 Python实现,由微软的 Jim Huguni ...

  5. Python自定义线程类简单示例

    Python自定义线程类简单示例 这篇文章主要介绍了Python自定义线程类,结合简单实例形式分析Python线程的定义与调用相关操作技巧,需要的朋友可以参考下.具体如下: 一. 代码     # - ...

  6. python golang中grpc 使用示例代码详解

    python 1.使用前准备,安装这三个库 pip install grpcio pip install protobuf pip install grpcio_tools 2.建立一个proto文件 ...

  7. robot framework环境搭建和简单示例

    环境搭建 因为我的本机已经安装了python.selenium.pip等,所以还需安装以下程序 1.安装wxPythonhttp://downloads.sourceforge.net/wxpytho ...

  8. AJAX的简单示例:注册校验

    众所周知,我们每次需要注册一个网站的用户名时,都会校验该邮箱.用户名是不是正确的格式.是不是有被使用过,密码是否符合规则,二次确认是否符合. 如果这些校验都采用form表单提交的话,会给用户带来极不好 ...

  9. web 框架的本质及自定义web框架 模板渲染jinja2 mvc 和 mtv框架 Django框架的下载安装 基于Django实现的一个简单示例

    Django基础一之web框架的本质 本节目录 一 web框架的本质及自定义web框架 二 模板渲染JinJa2 三 MVC和MTV框架 四 Django的下载安装 五 基于Django实现的一个简单 ...

随机推荐

  1. TCP定时器 之 TIME_WAIT定时器

    概述 在FIN_WAIT_2收到对端发来的FIN,并回复ACK之后,会进入TIME_WAIT状态,此时添加定时器,定时器超时会将tw控制块从ehash和bhash中删除,并且释放tw控制块: 启动定时 ...

  2. 全排列函数next_permutation(a,a+n)

    #include<iostream> #include<algorithm> using namespace std; int main(){ ]; int n; cin> ...

  3. 自定义Chrome的console(样式、打印图片、开关)

    1.常用console类型 console.log() 常规打印 console.warn() 打印警告信息 console.error() 打印错误信息 console.time() 和 conso ...

  4. Ubuntu系统下各种报错杂烩(持续更新)

    在Github时报Permanently added the RSA host key for IP address '192.30.252.128' to the list of known hos ...

  5. KNN原理小结

    K近邻法(K-nearest neighbors,KNN)既可以分类,也可以回归. KNN做回归和分类的区别在于最后预测时的决策方式.KNN做分类时,一般用多数表决法:KNN做回归时,一般用平均法. ...

  6. GO开发:链表

    链表 type Student struct { Name string Next* Student } 每个节点包含下一个节点的地址,这样把所有的节点串起来了,通常把链表中的第一个节点叫做链表头 p ...

  7. 第八章 SpringCloud之Feign、Hystrix结合使用

    #这个章节主要是针对Hystrix的使用,因为Feign的章节在上一节已经实现了,整个代码也是在上一个章节的基础上修改的 ##################Hystrix一个简单Demo实现#### ...

  8. ListView控件,表格模式下,如何调整行高

    参考说明: https://www.codeproject.com/Articles/1401/Changing-Row-Height-in-an-owner-drawn-Control 如果所有项的 ...

  9. Redis 入门 3.2.4 命令拾遗

    Redis 入门 3.2 字符串类型 3.2.4 命令拾遗 1. 增加指定的整数 INCRBY key increment   INCRBY命令与INCR命令基本一样,只不过前者可以通过increme ...

  10. Go语言入门 不错

    Go入门 Go介绍 部落图鉴之Go:爹好还这么努力? 环境配置 安装 下载源码编译安装 下载相应平台的安装包安装 下载地址:https://golang.org/dl/ Linux上的apt-get, ...