人类用grpc。grpc反射支持客户端

grpc-requests的Python项目详细描述


grpc请求

PyPI - Python VersionPyPIPyPI download monthcodecovViews

人类用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的自动转换请求(响应)
    • 支持一元一元

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java窗口。位置和窗口。公开问题   java如何从存储在ArrayList<Node>中的动态生成的文本字段中获取文本?   java如何立即关闭InputStream?   如何重新启动Java程序以激活环境变量   java搜索字符串是否相差一个字符   java CFB模式输出与CTR输出相同;我做错什么了吗?   java如何在javaFX中将实例化对象添加到Stage   java如何在jtextarea上打印来自不同类的文本消息   java以编程方式确定IOException的原因?   限制Java NIO通道(文件或socket)中的可用内容   javajaxb与JDOM:是否可以使用JAXB更新xml文件   批处理文件到java测试   JavaFX:stage的作用是什么。可设置大小(false)是否会导致额外的页边距?   java有没有办法告诉IntelliJ按需堆叠参数?   java Seam会话范围的组件在下一个请求中消失   java Google Web Toolkit对开发复杂的java脚本有用吗?   安卓 studio java ArrayList正在检索最高值   java为什么递归地用随机数填充LinkedList时会出现StackOverflowException?