【发布时间】:2021-03-16 18:19:14
【问题描述】:
我可以使用带有以下语法的 grpcurl 向我们启用 gRPC、启用反射的服务器之一发送请求:
grpcurl --plaintext -d '{"test_input": "Test 1 2 3", "config": { "max_results": 3 }}' localhost:6565 myorg.myproject.v1alpha6.MyService.MyStub
这不需要来自 grpcurl 的原型知识。它非常完美,因为我在一个测试脚本中使用它,我希望尽可能简单。
我已经能够利用它编写一个非常简单的 python 脚本:
import json
import subprocess
input = { "test_input": "Test 1 2 3", "config": { "max_results": 3 } }
result = subprocess.check_output(['grpcurl', '--plaintext', '-d', json.dumps(input), 'localhost:6565', 'MyStub'])
dict_result = json.loads(result)
但我对不得不调用外部进程不太满意。
我在互联网上找到的所有文档都需要下载 protos 及其依赖项并使用 protoc 编译它们。我可以做到这一点,这不是问题的对象。这里的目标是使用 Python 与使用 JSON 的服务器进行交换,而无需下载 proto。就像使用 grpcurl 完成的一样。
这可以仅使用 python 完成吗?
【问题讨论】:
标签: python json protocol-buffers grpc grpcurl