Python怎么搭建gRPC服务
发表于:2025-11-11 作者:千家信息网编辑
千家信息网最后更新 2025年11月11日,这篇文章主要介绍Python怎么搭建gRPC服务,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!1、安装python所需的库。pip install grpciopip inst
千家信息网最后更新 2025年11月11日Python怎么搭建gRPC服务
这篇文章主要介绍Python怎么搭建gRPC服务,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
1、安装python所需的库。
pip install grpciopip install grpcio-tools pip install protobuf
2、定义gRPC接口。
syntax = "proto3";option cc_generic_services = true;//定义服务接口service GrpcService { rpc hello (HelloRequest) returns (HelloResponse) {} //一个服务中可以定义多个接口,也就是多个函数功能}//请求的参数message HelloRequest { string data = 1; //数字1,2是参数的位置顺序,并不是对参数赋值 Skill skill = 2; //支持自定义的数据格式,非常灵活};//返回的对象message HelloResponse { string result = 1; map map_result = 2; //支持map数据格式,类似dict};message Skill { string name = 1;}; 3、用protoc和相应的插件编译生成相应语言的代码。
python -m grpc_tools.protoc -I ./ --python_out=./ --grpc_python_out=. ./hello.proto
使用编译工具将proto文件转换成py文件,直接在当前文件目录下运行上述代码。
4、编写grpc服务器代码。
#! /usr/bin/env python# coding=utf8import timefrom concurrent import futuresimport grpcfrom gRPC_example import hello_pb2_grpc, hello_pb2_ONE_DAY_IN_SECONDS = 60 * 60 * 24class TestService(hello_pb2_grpc.GrpcServiceServicer): ''' 继承GrpcServiceServicer,实现hello方法 ''' def __init__(self): pass def hello(self, request, context): ''' 具体实现hello的方法,并按照pb的返回对象构造HelloResponse返回 :param request: :param context: :return: ''' result = request.data + request.skill.name + " this is gprc test service" list_result = {"12": 1232} return hello_pb2.HelloResponse(result=str(result), map_result=list_result)def run(): ''' 模拟服务启动 :return: ''' server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) hello_pb2_grpc.add_GrpcServiceServicer_to_server(TestService(),server) server.add_insecure_port('[::]:50052') server.start() print("start service...") try: while True: time.sleep(_ONE_DAY_IN_SECONDS) except KeyboardInterrupt: server.stop(0)if __name__ == '__main__': run()5、编写gRPC客户端代码。
#! /usr/bin/env python# coding=utf8import grpcfrom gRPC_example import #! /usr/bin/env python# coding=utf8import grpcfrom gRPC_example import hello_pb2_grpc, hello_pb2def run(): ''' 模拟请求服务方法信息 :return: ''' conn=grpc.insecure_channel('localhost:50052') client = hello_pb2_grpc.GrpcServiceStub(channel=conn) skill = hello_pb2.Skill(name="engineer") request = hello_pb2.HelloRequest(data="xiao gang", skill=skill) respnse = client.hello(request) print("received:",respnse.result)if __name__ == '__main__': run()def run(): ''' 模拟请求服务方法信息 :return: ''' conn=grpc.insecure_channel('localhost:50052') client = hello_pb2_grpc.GrpcServiceStub(channel=conn) skill = hello_pb2.Skill(name="engineer") request = hello_pb2.HelloRequest(data="xiao gang", skill=skill) response = client.hello(request) print("received:",response.result)if __name__ == '__main__': run()6、调用测试。
首先启动运行服务器的代码,然后启动运行客户端的代码。
以上是"Python怎么搭建gRPC服务"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
服务
代码
方法
参数
接口
文件
运行
信息
内容
多个
客户
对象
数据
服务器
格式
篇文章
支持
编译
也就是
价值
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
韶关网络技术
武汉超静音服务器
北京网络安全博览会
公司内的文件服务器怎么搭建
网络技术专员怎么样
一年级网络安全法小报
网络安全法外网
天津美橙互联云服务器
香港网站服务器的特点
曙光服务器关闭高性能
数据库经常采用的结构
c 软件开发项目全程解剖
用台湾服务器可以ob韩服吗
软件开发工作任务少
安徽软件开发哪家好
阿里云香港服务器ip
昂楷数据库运维
网络安全实战比武
如何用数据库筛选mirna
数据库修改数据部分内容吗
服务器系统自检通过黑屏
服务器进不了安全系统
刀片服务器维保方案
怎么打开NTP服务器端口
数据库怎么存储日期
上海颜悦网络技术有限公司
cmd中进入数据库
电信和网络安全解决方案
关于网络安全的英语倡议书
软件开发修改文档模板