avi*_*avi 5 python python-3.x grpc
我想知道如何使用Python gRPC发送自定义标头(或元数据).我看了看文件,找不到任何东西.
avi*_*avi 11
我想出了阅读代码.您可以向metadata函数调用发送一个参数,其中metadata是一个2元组的元组:
metadata = (('md-key', 'some value'),
('some-md-key', 'another value'))
response = stub.YourFunctionCall(request=request, metadata=metadata)
Run Code Online (Sandbox Code Playgroud)
小智 6
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'),
))
Run Code Online (Sandbox Code Playgroud)
或者如果你想定义一个拦截器
metadata = []
if client_call_details.metadata is not None:
metadata = list(client_call_details.metadata)
metadata.append((
header,
value,
))
client_call_details = _ClientCallDetails(
client_call_details.method, client_call_details.timeout, metadata,
client_call_details.credentials)
Run Code Online (Sandbox Code Playgroud)
重要的是元数据的键不能有大写字符(它困扰了我很长时间)。
| 归档时间: |
|
| 查看次数: |
3638 次 |
| 最近记录: |