人类用grpc。grpc反射支持客户端
grpc-requests的Python项目详细描述
grpc请求
人类用GRPC
fromgrpc_requestsimportClientclient=Client.get_by_endpoint("localhost:50051")assertclient.service_names==["helloworld.Greeter"]request_data={"name":'sinsky'}result=client.request("helloworld.Greeter","SayHello",request_data)print(result)# {"message":"Hellow sinsky"}
特色
- 使用反射或存根连接服务器
- 不需要存根类请求grpc(如果需要)
- 支持所有一元流方法
- 支持tls和压缩连接
安装
^{pr2}$像RPC一样使用它!在
如果你的服务器支持反射!在
fromgrpc_requestsimportClientclient=Client.get_by_endpoint("localhost:50051")# if you want connect tls# client = Client.get_by_endpoint("localhost:443",ssl=True)# or if you want Compression connect# client = Client.get_by_endpoint("localhost:443",compression=grpc.Compression.Gzip)assertclient.service_names==["helloworld.Greeter",'grpc.health.v1.Health']health=client.service('grpc.health.v1.Health')asserthealth.method_names==('Check','Watch')result=health.Check()assertresult=={'status':'SERVING'}greeter=client.service("helloworld.Greeter")request_data={"name":'sinsky'}result=greeter.SayHello(request_data)results=greeter.SayHelloGroup(request_data)requests_data=[{"name":'sinsky'}]result=greeter.HelloEveryone(requests_data)results=greeter.SayHelloOneByOne(requests_data)
你也可以用你的存根
fromgrpc_requestsimportStubClientfrom.hellow_pb2importDescriptorservice_descriptor=DESCRIPTOR.services_by_name['Greeter']# or you can just use _GREETERclient=StubClient.get_by_endpoint("localhost:50051",service_descriptors=[service_descriptor,])# if you want connect tls# client = Client.get_by_endpoint("localhost:443",ssl=True)# or if you want Compression connect# client = Client.get_by_endpoint("localhost:443",compression=grpc.Compression.Gzip)assertclient.service_names==["helloworld.Greeter"]greeter=client.service("helloworld.Greeter")request_data={"name":'sinsky'}result=greeter.SayHello(request_data)results=greeter.SayHelloGroup(request_data)requests_data=[{"name":'sinsky'}]result=greeter.HelloEveryone(requests_data)results=greeter.SayHelloOneByOne(requests_data)
示例
请求一元一元
service="helloworld.Greeter"unary_unary_method='SayHello'request_data={"name":'sinsky'}# You Don't Need Stub!result=client.request(service,unary_unary_method,request_data)assertdict==type(result)# result is dict Type!!! not Stub Object!assert{"message":"Hellow sinsky"}==result# orrequest_data={"name":'sinsky'}# You Don't Need Stub!# any one know this method is unary-unaryresult=client.unary_unary(service,unary_unary_method,request_data)assertdict==type(result)# result is dict Type!!! not Stub Object!assert{"message":"Hellow sinsky"}==result
请求一元流
unary_stream_method='SayHelloGroup'unary_stream_results=client.request(service,unary_unary_method,request_data)assertall([dict==type(result)forresultinunary_stream_results])assert[{"message":"Hellow sinsky"}]==list(unary_stream_results)# orunary_stream_results=client.unary_stream(service,unary_unary_method,request_data)assertall([dict==type(result)forresultinunary_stream_results])assert[{"message":"Hellow sinsky"}]==list(unary_stream_results)
请求流一元
requests_data=[request_data]# iteratorstream_unary_method='HelloEveryone'result_stream_unary=client.request(service,stream_unary_method,requests_data)assertdict==type(result)# result is dict Type!!! not Stub Object!# orresult_stream_unary=client.stream_unary(service,stream_unary_method,requests_data)assertdict==type(result)# result is dict Type!!! not Stub Object!
请求流
requests_data=[request_data]# iteratorstream_stream_method='SayHelloOneByOne'result=client.request(service,stream_stream_method,requests_data)assertall([dict==type(result)forresultinunary_stream_results])# orresult=client.stream_stream(service,stream_stream_method,requests_data)assertall([dict==type(result)forresultinunary_stream_results])
反射客户端,但您可以通过存根发送mesg
fromgrpc_requestsimportClientfromhelloworld_pb2importHelloRequestport='50051'host="localhost"endpoint=f"{host}:{port}"client=Client.get_by_endpoint(endpoint)print(client.service_names)# ["helloworld.Greeter"]service="helloworld.Greeter"method='SayHello'result=client.unary_unary(service,method,HelloRequest(name='sinsky'))print(type(result))# result is dict Type!!! not Stub Object!print(result)# {"message":"Hellow sinsky"}# or get raw response dataresult=client.unary_unary(service,method,HelloRequest(name='sinsky'),raw_output=True)print(type(result))# HelloReply stub class
路线图
- [x] 不支持反射服务器(存根客户端)
- []支持异步API!在
关系项目
- homi:类似烧瓶的微型grpc框架。使用方便!在
更改日志
- 在
0.0.8
- :火花:特征
- #12添加StubClient
- :bug:bug修复!
- #11绕过Kargs到基本客户端
- :火花:特征
- 在
0.0.7
- 特色
- 支撑压缩
- 特色
- 在
0.0.6
- 特色
- 支持tls连接
- 特色
- 在
0.0.5
- 改变
- 响应字段获取原始原型字段(在returend LowerMacalCase之前)
- 改变
- 在
0.0.3
- 特色
- 动态请求方法
- 服务客户端
- 特色
- 在
0.0.2款
- 支持所有方法类型
- 添加请求测试用例
- 在
0.0.1款
- 使用反射同步原型
- 从(到)dict的自动转换请求(响应)
- 支持一元一元
- 项目
标签: