Add python client
diff --git a/examples/python/client/BUILD.bazel b/examples/python/client/BUILD.bazel new file mode 100644 index 0000000..dc5c34c --- /dev/null +++ b/examples/python/client/BUILD.bazel
@@ -0,0 +1,12 @@ +load("@aspect_rules_py//py:defs.bzl", "py_binary") + +py_binary( + name = "client", + srcs = ["__main__.py"], + main = "__main__.py", + deps = [ + "//proto:greeter_py_grpc", + "@pypi//grpcio", + "@pypi//protobuf", + ], +)
diff --git a/examples/python/client/__main__.py b/examples/python/client/__main__.py new file mode 100644 index 0000000..bd2b644 --- /dev/null +++ b/examples/python/client/__main__.py
@@ -0,0 +1,36 @@ +import logging + +import grpc +from proto import greeter_pb2 +from proto.greeter_pb2_grpc import GreeterStub +from google.protobuf.any_pb2 import Any + + +def run(): + # Create a channel to connect to the server + with grpc.insecure_channel('[::1]:5042') as channel: + # Create a stub (client) + stub = GreeterStub(channel) + + # Create a detail message using Any + detail = Any() + detail.type_url = "type.googleapis.com/mypackage.MyMessage" + detail.value = b"details" + + # Create the request + request = greeter_pb2.HelloRequest( + name="Python Client", + details=[detail] + ) + + # Make the call + try: + response = stub.SayHello(request) + print(f"Response: {response.message}") + except grpc.RpcError as e: + print(f"RPC failed: {e}") + + +if __name__ == "__main__": + logging.basicConfig() + run() \ No newline at end of file