在Python服务器中读取gRPC元数据

时间:2018-03-29 10:18:02

标签: python grpc

我有这样的客户:

The 'Access-Control-Allow-Origin' header has a value 'null' that is not equal to the supplied origin. Origin 'null' is therefore not allowed access.

服务器:

def run():
    channel = grpc.insecure_channel('localhost:50051')
    stub = helloworld_pb2_grpc.GreeterStub(channel)
    metadata = [('key', "value")]
    response = stub.SayHello(
        helloworld_pb2.HelloRequest(name='you'),
        metadata=metadata
    )
    print("Greeter client received: " + response.message)

那么,在服务器中,如何读取客户端发送的元数据?

2 个答案:

答案 0 :(得分:2)

https://grpc.io/grpc/python/grpc.html#grpc.ServicerContext.invocation_metadata

我们可以使用context.invocation_metadata()来访问客户端的元数据

答案 1 :(得分:1)

只需在-的正确答案之上添加一些内容即可。

您可以像字典一样检索数据

metadict = dict(context.invocation_metadata())
userid = metadict['userid']

此外,我强烈建议您使用ServerInterceptor检出gprc示例存储库以了解更高级的用例-here-