pygrpc 0.2.0
Released: Feb-10-2015
pygrpc
is a Python module that provides a high-level client connection object to instantiate Google Remote Procedure Calls (gRPCs).
By default, the Google Protocol Buffer compiler generates client and server code.
The following example is a code snippet provided by Google that creates a helloworld.Greeter
stub object and invokes the SayHello
method. The SayHello
method takes a serializer class as its first argument (helloworld_pb2.HelloRequest
):
"""The Python implementation of the GRPC helloworld.Greeter client."""
from grpc.beta import implementations
import helloworld_pb2
_TIMEOUT_SECONDS = 10
channel = implementations.insecure_channel('localhost', 50051)
stub = helloworld_pb2.beta_create_Greeter_stub(channel)
response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'), _TIMEOUT_SECONDS)
print "Greeter client received: " + response.message
The following example demonstrates the same request using pygrpc
:
from pygrpc import Client
client = Client('localhost', 50051)
# This loads the generated Google Protocol Buffer module.
client.load('helloworld_pb2')
response = client.request('SayHello', name='you')
print "Greeter client received: " + response.message
Both examples would output: Greeter client received: Hello, you!
.
The pygrpc
client only requires the request
method to be called for issuing gRPC requests.
client.request(request, *args, **kwargs)
Return a simple RPC response or a streaming RPC response. This is known as the cardinality of the gRPC request/response.
Type: str
Parameters: request - the name of the RPC request defined in the .proto
file.
Return type: Reply object defined in the .proto
file.
Returns: Either a simple RPC response or a streaming RPC response.