人类用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 Bluestack未连接到eclipse   java如何从ConstraintViolationException Hibernamte获取数据库字段名   HttpResponse HttpResponse=httpClient引发java运行时错误。执行(httpPost);   Jama中矩阵的java点积和叉积   java有什么方法可以唯一地识别可扩展设备吗?   java我需要用*来写我的名字,但我不断遇到一个错误,我对编码很陌生   java变量是在内部类中访问的。需要被宣布为最终决定。但我不想宣布最终结果   java如何缩短base64图像字符串,Android?   JavaSpringMVC:计划方法不自动触发   图形学习Java 2D API的好资源是什么?   如何在java中对方法进行排队   java JavaFX多行   java Selenium无法在[链接]上找到基于CSS元素的密码字段元素http://www.cartasi.it/gtwpages/index.jsp   Java中的equals()和hashCode()契约   软删除情况下的java Hibernate二级缓存   java为什么这段代码要两次调用这些方法?