-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrpc_python.py
More file actions
52 lines (41 loc) · 1.54 KB
/
Copy pathgrpc_python.py
File metadata and controls
52 lines (41 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
"""Call the generated UTCP gRPC stub against the example server."""
import json
import os
from pathlib import Path
import sys
ROOT = Path(__file__).resolve().parents[1]
VENV_ROOT = ROOT / ".venv-grpc"
VENV_PYTHON = VENV_ROOT / "bin" / "python"
if VENV_PYTHON.exists() and Path(sys.prefix).resolve() != VENV_ROOT.resolve():
os.execv(str(VENV_PYTHON), [str(VENV_PYTHON), __file__, *sys.argv[1:]])
try:
import grpc
except ImportError as error:
raise SystemExit("Missing grpcio. Run: make grpc-python-setup") from error
GENERATED_DIR = Path(__file__).resolve().parent / "generated"
sys.path.insert(0, str(GENERATED_DIR))
try:
import utcp_pb2
import utcp_pb2_grpc
except (ImportError, RuntimeError) as error:
raise SystemExit(
"Missing or incompatible generated gRPC stubs. Run: make grpc-python-setup"
) from error
def main():
host = os.environ.get("UTCP_GRPC_HOST", "127.0.0.1")
port = int(os.environ.get("UTCP_GRPC_PORT", "50051"))
with grpc.insecure_channel(f"{host}:{port}") as channel:
stub = utcp_pb2_grpc.UTCPServiceStub(channel)
manual = stub.GetManual(utcp_pb2.Empty(), timeout=5)
print("tools:", ", ".join(tool.name for tool in manual.tools))
response = stub.CallTool(
utcp_pb2.ToolCallRequest(
tool="echo",
args_json=json.dumps({"message": "Hello from Python gRPC client"}),
),
timeout=5,
)
print(json.loads(response.result_json))
if __name__ == "__main__":
main()