diff options
author | Lidi Zheng <scallopsky@gmail.com> | 2018-12-14 10:24:54 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-14 10:24:54 -0800 |
commit | 20df6e75d77dcbb68072ed53664f987f3a447bc4 (patch) | |
tree | da12f7e76f6e12ebb813bffe3312ce831705a6be /examples/python/metadata/metadata_client.py | |
parent | eb65dbb0304241a57d51b3b9f88a50de1f68a521 (diff) | |
parent | 7fd68349e3ac2dbf642cb1c7907901ad51b5d6c6 (diff) |
Merge pull request #17485 from lidizheng/metadata-example
Add gRPC Python Example: Metadata
Diffstat (limited to 'examples/python/metadata/metadata_client.py')
-rw-r--r-- | examples/python/metadata/metadata_client.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/examples/python/metadata/metadata_client.py b/examples/python/metadata/metadata_client.py new file mode 100644 index 0000000000..f2a8e37cc2 --- /dev/null +++ b/examples/python/metadata/metadata_client.py @@ -0,0 +1,48 @@ +# Copyright 2018 The gRPC Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""Example gRPC client that gets/sets metadata (HTTP2 headers)""" + +from __future__ import print_function +import logging + +import grpc + +import helloworld_pb2 +import helloworld_pb2_grpc + + +def run(): + # NOTE(gRPC Python Team): .close() is possible on a channel and should be + # used in circumstances in which the with statement does not fit the needs + # of the code. + with grpc.insecure_channel('localhost:50051') as channel: + stub = helloworld_pb2_grpc.GreeterStub(channel) + response, call = stub.SayHello.with_call( + helloworld_pb2.HelloRequest(name='you'), + metadata=( + ('initial-metadata-1', 'The value should be str'), + ('binary-metadata-bin', + b'With -bin surffix, the value can be bytes'), + ('accesstoken', 'gRPC Python is great'), + )) + + print("Greeter client received: " + response.message) + for key, value in call.trailing_metadata(): + print('Greeter client received trailing metadata: key=%s value=%s' % + (key, value)) + + +if __name__ == '__main__': + logging.basicConfig() + run() |