-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_grpcio.py
More file actions
65 lines (51 loc) · 2.36 KB
/
example_grpcio.py
File metadata and controls
65 lines (51 loc) · 2.36 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
53
54
55
56
57
58
59
60
61
62
63
64
65
from argparse import ArgumentParser
from management_commands import Command, main
from tests.proto import helloworld_pb2_grpc
from tests.proto import helloworld_pb2
from grpc_boilerplate.grpcio_tools.client import api_stub
import grpc # type: ignore
from concurrent import futures
HOST = "127.0.0.1"
PORT = 50002
TLS_CRT = "certs/server.crt"
TLS_KEY = "certs/server.key"
class GreeterService(helloworld_pb2_grpc.GreeterServicer):
def SayHello(self, request: helloworld_pb2.HelloRequest, context: grpc.RpcContext) -> helloworld_pb2.HelloReply:
return helloworld_pb2.HelloReply(message=f"Hello, {request.name}")
class Server(Command):
def add_arguments(self, parser: ArgumentParser) -> None:
parser.add_argument("--host", default=HOST)
parser.add_argument("--port", type=int, default=PORT)
parser.add_argument("--tls", action="store_true")
parser.add_argument("-tls_crt", default=TLS_CRT)
parser.add_argument("-tls_key", default=TLS_KEY)
def handle(self, **kwargs):
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
helloworld_pb2_grpc.add_GreeterServicer_to_server(GreeterService(), server)
if kwargs["tls"] is True:
with open(kwargs["tls_crt"], "rb") as f:
tls_crt = f.read()
with open(kwargs["tls_key"], "rb") as f:
tls_key = f.read()
creds = grpc.ssl_server_credentials(
[
(tls_key, tls_crt),
]
)
server.add_secure_port(f'{kwargs["host"]}:{kwargs["port"]}', creds)
else:
server.add_insecure_port(f'{kwargs["host"]}:{kwargs["port"]}')
server.start()
print(f'Serving on {kwargs["host"]}:{kwargs["port"]}')
server.wait_for_termination()
class Client(Command):
def add_arguments(self, parser: ArgumentParser) -> None:
parser.add_argument("--connection_string", default=f"h2c://{HOST}:{PORT}")
parser.add_argument("--message", default="World")
def handle(self, **kwargs):
connection_string = kwargs["connection_string"]
with api_stub(connection_string, stub=helloworld_pb2_grpc.GreeterStub) as client:
resp = client.SayHello(helloworld_pb2.HelloRequest(name=kwargs["message"]))
print(resp)
if __name__ == "__main__":
main(commands=[Server(), Client()])