Spaces:
Running
Running
Francesco Capuano
commited on
Commit
·
9559f3f
1
Parent(s):
529ed6b
add: Testing basic comms
Browse files- Dockerfile +15 -0
- README.md +8 -0
- async_greeter_client.py +33 -0
- async_greeter_client_with_options.py +46 -0
- async_greeter_server.py +45 -0
- async_greeter_server_with_graceful_shutdown.py +65 -0
- async_greeter_server_with_reflection.py +49 -0
- greeter_client.py +38 -0
- greeter_client_reflection.py +48 -0
- greeter_client_with_options.py +50 -0
- greeter_server.py +41 -0
- greeter_server_with_reflection.py +45 -0
- helloworld_pb2.py +41 -0
- helloworld_pb2.pyi +17 -0
- helloworld_pb2_grpc.py +187 -0
Dockerfile
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.10-slim
|
2 |
+
|
3 |
+
WORKDIR /app
|
4 |
+
|
5 |
+
# Copy the entire project
|
6 |
+
COPY . .
|
7 |
+
|
8 |
+
# Install dependencies
|
9 |
+
RUN pip install --no-cache-dir -e .
|
10 |
+
|
11 |
+
# Expose the port that greeter_server listens on
|
12 |
+
EXPOSE 8080
|
13 |
+
|
14 |
+
# Command to run the server
|
15 |
+
CMD ["python", "greeter_server.py"]
|
README.md
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: PolicyServer
|
3 |
+
emoji: 🐳
|
4 |
+
colorFrom: purple
|
5 |
+
colorTo: gray
|
6 |
+
sdk: docker
|
7 |
+
app_port: 8080
|
8 |
+
---
|
async_greeter_client.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2020 gRPC authors.
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
"""The Python AsyncIO implementation of the GRPC helloworld.Greeter client."""
|
15 |
+
|
16 |
+
import asyncio
|
17 |
+
import logging
|
18 |
+
|
19 |
+
import grpc
|
20 |
+
import helloworld_pb2
|
21 |
+
import helloworld_pb2_grpc
|
22 |
+
|
23 |
+
|
24 |
+
async def run() -> None:
|
25 |
+
async with grpc.aio.insecure_channel("localhost:50051") as channel:
|
26 |
+
stub = helloworld_pb2_grpc.GreeterStub(channel)
|
27 |
+
response = await stub.SayHello(helloworld_pb2.HelloRequest(name="you"))
|
28 |
+
print("Greeter client received: " + response.message)
|
29 |
+
|
30 |
+
|
31 |
+
if __name__ == "__main__":
|
32 |
+
logging.basicConfig()
|
33 |
+
asyncio.run(run())
|
async_greeter_client_with_options.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2020 The gRPC Authors
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
"""gRPC Python AsyncIO helloworld.Greeter client with channel options and timeout parameters."""
|
15 |
+
|
16 |
+
import asyncio
|
17 |
+
import logging
|
18 |
+
|
19 |
+
import grpc
|
20 |
+
import helloworld_pb2
|
21 |
+
import helloworld_pb2_grpc
|
22 |
+
|
23 |
+
# For more channel options, please see https://grpc.io/grpc/core/group__grpc__arg__keys.html
|
24 |
+
CHANNEL_OPTIONS = [
|
25 |
+
("grpc.lb_policy_name", "pick_first"),
|
26 |
+
("grpc.enable_retries", 0),
|
27 |
+
("grpc.keepalive_timeout_ms", 10000),
|
28 |
+
]
|
29 |
+
|
30 |
+
|
31 |
+
async def run() -> None:
|
32 |
+
async with grpc.aio.insecure_channel(
|
33 |
+
target="localhost:50051", options=CHANNEL_OPTIONS
|
34 |
+
) as channel:
|
35 |
+
stub = helloworld_pb2_grpc.GreeterStub(channel)
|
36 |
+
# Timeout in seconds.
|
37 |
+
# Please refer gRPC Python documents for more detail. https://grpc.io/grpc/python/grpc.html
|
38 |
+
response = await stub.SayHello(
|
39 |
+
helloworld_pb2.HelloRequest(name="you"), timeout=10
|
40 |
+
)
|
41 |
+
print("Greeter client received: " + response.message)
|
42 |
+
|
43 |
+
|
44 |
+
if __name__ == "__main__":
|
45 |
+
logging.basicConfig()
|
46 |
+
asyncio.run(run())
|
async_greeter_server.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2020 gRPC authors.
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
"""The Python AsyncIO implementation of the GRPC helloworld.Greeter server."""
|
15 |
+
|
16 |
+
import asyncio
|
17 |
+
import logging
|
18 |
+
|
19 |
+
import grpc
|
20 |
+
import helloworld_pb2
|
21 |
+
import helloworld_pb2_grpc
|
22 |
+
|
23 |
+
|
24 |
+
class Greeter(helloworld_pb2_grpc.GreeterServicer):
|
25 |
+
async def SayHello(
|
26 |
+
self,
|
27 |
+
request: helloworld_pb2.HelloRequest,
|
28 |
+
context: grpc.aio.ServicerContext,
|
29 |
+
) -> helloworld_pb2.HelloReply:
|
30 |
+
return helloworld_pb2.HelloReply(message="Hello, %s!" % request.name)
|
31 |
+
|
32 |
+
|
33 |
+
async def serve() -> None:
|
34 |
+
server = grpc.aio.server()
|
35 |
+
helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
|
36 |
+
listen_addr = "[::]:50051"
|
37 |
+
server.add_insecure_port(listen_addr)
|
38 |
+
logging.info("Starting server on %s", listen_addr)
|
39 |
+
await server.start()
|
40 |
+
await server.wait_for_termination()
|
41 |
+
|
42 |
+
|
43 |
+
if __name__ == "__main__":
|
44 |
+
logging.basicConfig(level=logging.INFO)
|
45 |
+
asyncio.run(serve())
|
async_greeter_server_with_graceful_shutdown.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2021 The gRPC Authors
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
"""The graceful shutdown example for the asyncio Greeter server."""
|
15 |
+
|
16 |
+
import asyncio
|
17 |
+
import logging
|
18 |
+
|
19 |
+
import grpc
|
20 |
+
import helloworld_pb2
|
21 |
+
import helloworld_pb2_grpc
|
22 |
+
|
23 |
+
# Coroutines to be invoked when the event loop is shutting down.
|
24 |
+
_cleanup_coroutines = []
|
25 |
+
|
26 |
+
|
27 |
+
class Greeter(helloworld_pb2_grpc.GreeterServicer):
|
28 |
+
async def SayHello(
|
29 |
+
self,
|
30 |
+
request: helloworld_pb2.HelloRequest,
|
31 |
+
context: grpc.aio.ServicerContext,
|
32 |
+
) -> helloworld_pb2.HelloReply:
|
33 |
+
logging.info("Received request, sleeping for 4 seconds...")
|
34 |
+
await asyncio.sleep(4)
|
35 |
+
logging.info("Sleep completed, responding")
|
36 |
+
return helloworld_pb2.HelloReply(message="Hello, %s!" % request.name)
|
37 |
+
|
38 |
+
|
39 |
+
async def serve() -> None:
|
40 |
+
server = grpc.aio.server()
|
41 |
+
helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
|
42 |
+
listen_addr = "[::]:50051"
|
43 |
+
server.add_insecure_port(listen_addr)
|
44 |
+
logging.info("Starting server on %s", listen_addr)
|
45 |
+
await server.start()
|
46 |
+
|
47 |
+
async def server_graceful_shutdown():
|
48 |
+
logging.info("Starting graceful shutdown...")
|
49 |
+
# Shuts down the server with 5 seconds of grace period. During the
|
50 |
+
# grace period, the server won't accept new connections and allow
|
51 |
+
# existing RPCs to continue within the grace period.
|
52 |
+
await server.stop(5)
|
53 |
+
|
54 |
+
_cleanup_coroutines.append(server_graceful_shutdown())
|
55 |
+
await server.wait_for_termination()
|
56 |
+
|
57 |
+
|
58 |
+
if __name__ == "__main__":
|
59 |
+
logging.basicConfig(level=logging.INFO)
|
60 |
+
loop = asyncio.get_event_loop()
|
61 |
+
try:
|
62 |
+
loop.run_until_complete(serve())
|
63 |
+
finally:
|
64 |
+
loop.run_until_complete(*_cleanup_coroutines)
|
65 |
+
loop.close()
|
async_greeter_server_with_reflection.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2020 The gRPC Authors
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
"""The reflection-enabled version of gRPC AsyncIO helloworld.Greeter server."""
|
15 |
+
|
16 |
+
import asyncio
|
17 |
+
import logging
|
18 |
+
|
19 |
+
import grpc
|
20 |
+
from grpc_reflection.v1alpha import reflection
|
21 |
+
import helloworld_pb2
|
22 |
+
import helloworld_pb2_grpc
|
23 |
+
|
24 |
+
|
25 |
+
class Greeter(helloworld_pb2_grpc.GreeterServicer):
|
26 |
+
async def SayHello(
|
27 |
+
self,
|
28 |
+
request: helloworld_pb2.HelloRequest,
|
29 |
+
context: grpc.aio.ServicerContext,
|
30 |
+
) -> helloworld_pb2.HelloReply:
|
31 |
+
return helloworld_pb2.HelloReply(message="Hello, %s!" % request.name)
|
32 |
+
|
33 |
+
|
34 |
+
async def serve() -> None:
|
35 |
+
server = grpc.aio.server()
|
36 |
+
helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
|
37 |
+
SERVICE_NAMES = (
|
38 |
+
helloworld_pb2.DESCRIPTOR.services_by_name["Greeter"].full_name,
|
39 |
+
reflection.SERVICE_NAME,
|
40 |
+
)
|
41 |
+
reflection.enable_server_reflection(SERVICE_NAMES, server)
|
42 |
+
server.add_insecure_port("[::]:50051")
|
43 |
+
await server.start()
|
44 |
+
await server.wait_for_termination()
|
45 |
+
|
46 |
+
|
47 |
+
if __name__ == "__main__":
|
48 |
+
logging.basicConfig()
|
49 |
+
asyncio.run(serve())
|
greeter_client.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2015 gRPC authors.
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
"""The Python implementation of the GRPC helloworld.Greeter client."""
|
15 |
+
|
16 |
+
from __future__ import print_function
|
17 |
+
|
18 |
+
import logging
|
19 |
+
|
20 |
+
import grpc
|
21 |
+
import helloworld_pb2
|
22 |
+
import helloworld_pb2_grpc
|
23 |
+
|
24 |
+
|
25 |
+
def run():
|
26 |
+
# NOTE(gRPC Python Team): .close() is possible on a channel and should be
|
27 |
+
# used in circumstances in which the with statement does not fit the needs
|
28 |
+
# of the code.
|
29 |
+
print("Will try to greet world ...")
|
30 |
+
with grpc.insecure_channel("localhost:50051") as channel:
|
31 |
+
stub = helloworld_pb2_grpc.GreeterStub(channel)
|
32 |
+
response = stub.SayHello(helloworld_pb2.HelloRequest(name="you"))
|
33 |
+
print("Greeter client received: " + response.message)
|
34 |
+
|
35 |
+
|
36 |
+
if __name__ == "__main__":
|
37 |
+
logging.basicConfig()
|
38 |
+
run()
|
greeter_client_reflection.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2023 gRPC authors.
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
"""The Python implementation of the GRPC helloworld.Greeter client with reflection."""
|
15 |
+
|
16 |
+
import logging
|
17 |
+
|
18 |
+
from google.protobuf.descriptor_pool import DescriptorPool
|
19 |
+
import grpc
|
20 |
+
from grpc_reflection.v1alpha.proto_reflection_descriptor_database import (
|
21 |
+
ProtoReflectionDescriptorDatabase,
|
22 |
+
)
|
23 |
+
|
24 |
+
|
25 |
+
def run():
|
26 |
+
print("Will try to greet world ...")
|
27 |
+
with grpc.insecure_channel("localhost:50051") as channel:
|
28 |
+
reflection_db = ProtoReflectionDescriptorDatabase(channel)
|
29 |
+
services = reflection_db.get_services()
|
30 |
+
print(f"found services: {services}")
|
31 |
+
|
32 |
+
desc_pool = DescriptorPool(reflection_db)
|
33 |
+
service_desc = desc_pool.FindServiceByName("helloworld.Greeter")
|
34 |
+
print(f"found Greeter service with name: {service_desc.full_name}")
|
35 |
+
for methods in service_desc.methods:
|
36 |
+
print(f"found method name: {methods.full_name}")
|
37 |
+
input_type = methods.input_type
|
38 |
+
print(f"input type for this method: {input_type.full_name}")
|
39 |
+
|
40 |
+
request_desc = desc_pool.FindMessageTypeByName(
|
41 |
+
"helloworld.HelloRequest"
|
42 |
+
)
|
43 |
+
print(f"found request name: {request_desc.full_name}")
|
44 |
+
|
45 |
+
|
46 |
+
if __name__ == "__main__":
|
47 |
+
logging.basicConfig()
|
48 |
+
run()
|
greeter_client_with_options.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2018 gRPC authors.
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
"""gRPC Python helloworld.Greeter client with channel options and call timeout parameters."""
|
15 |
+
|
16 |
+
from __future__ import print_function
|
17 |
+
|
18 |
+
import logging
|
19 |
+
|
20 |
+
import grpc
|
21 |
+
import helloworld_pb2
|
22 |
+
import helloworld_pb2_grpc
|
23 |
+
|
24 |
+
|
25 |
+
def run():
|
26 |
+
# NOTE(gRPC Python Team): .close() is possible on a channel and should be
|
27 |
+
# used in circumstances in which the with statement does not fit the needs
|
28 |
+
# of the code.
|
29 |
+
#
|
30 |
+
# For more channel options, please see https://grpc.io/grpc/core/group__grpc__arg__keys.html
|
31 |
+
with grpc.insecure_channel(
|
32 |
+
target="localhost:50051",
|
33 |
+
options=[
|
34 |
+
("grpc.lb_policy_name", "pick_first"),
|
35 |
+
("grpc.enable_retries", 0),
|
36 |
+
("grpc.keepalive_timeout_ms", 10000),
|
37 |
+
],
|
38 |
+
) as channel:
|
39 |
+
stub = helloworld_pb2_grpc.GreeterStub(channel)
|
40 |
+
# Timeout in seconds.
|
41 |
+
# Please refer gRPC Python documents for more detail. https://grpc.io/grpc/python/grpc.html
|
42 |
+
response = stub.SayHello(
|
43 |
+
helloworld_pb2.HelloRequest(name="you"), timeout=10
|
44 |
+
)
|
45 |
+
print("Greeter client received: " + response.message)
|
46 |
+
|
47 |
+
|
48 |
+
if __name__ == "__main__":
|
49 |
+
logging.basicConfig()
|
50 |
+
run()
|
greeter_server.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2015 gRPC authors.
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
"""The Python implementation of the GRPC helloworld.Greeter server."""
|
15 |
+
|
16 |
+
from concurrent import futures
|
17 |
+
import logging
|
18 |
+
|
19 |
+
import grpc
|
20 |
+
import helloworld_pb2
|
21 |
+
import helloworld_pb2_grpc
|
22 |
+
|
23 |
+
|
24 |
+
class Greeter(helloworld_pb2_grpc.GreeterServicer):
|
25 |
+
def SayHello(self, request, context):
|
26 |
+
return helloworld_pb2.HelloReply(message="Hello, %s!" % request.name)
|
27 |
+
|
28 |
+
|
29 |
+
def serve():
|
30 |
+
port = "50051"
|
31 |
+
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
|
32 |
+
helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
|
33 |
+
server.add_insecure_port("[::]:" + port)
|
34 |
+
server.start()
|
35 |
+
print("Server started, listening on " + port)
|
36 |
+
server.wait_for_termination()
|
37 |
+
|
38 |
+
|
39 |
+
if __name__ == "__main__":
|
40 |
+
logging.basicConfig()
|
41 |
+
serve()
|
greeter_server_with_reflection.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2018 The gRPC Authors
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
"""The reflection-enabled version of gRPC helloworld.Greeter server."""
|
15 |
+
|
16 |
+
from concurrent import futures
|
17 |
+
import logging
|
18 |
+
|
19 |
+
import grpc
|
20 |
+
from grpc_reflection.v1alpha import reflection
|
21 |
+
import helloworld_pb2
|
22 |
+
import helloworld_pb2_grpc
|
23 |
+
|
24 |
+
|
25 |
+
class Greeter(helloworld_pb2_grpc.GreeterServicer):
|
26 |
+
def SayHello(self, request, context):
|
27 |
+
return helloworld_pb2.HelloReply(message="Hello, %s!" % request.name)
|
28 |
+
|
29 |
+
|
30 |
+
def serve():
|
31 |
+
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
|
32 |
+
helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
|
33 |
+
SERVICE_NAMES = (
|
34 |
+
helloworld_pb2.DESCRIPTOR.services_by_name["Greeter"].full_name,
|
35 |
+
reflection.SERVICE_NAME,
|
36 |
+
)
|
37 |
+
reflection.enable_server_reflection(SERVICE_NAMES, server)
|
38 |
+
server.add_insecure_port("[::]:50051")
|
39 |
+
server.start()
|
40 |
+
server.wait_for_termination()
|
41 |
+
|
42 |
+
|
43 |
+
if __name__ == "__main__":
|
44 |
+
logging.basicConfig()
|
45 |
+
serve()
|
helloworld_pb2.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
3 |
+
# NO CHECKED-IN PROTOBUF GENCODE
|
4 |
+
# source: helloworld.proto
|
5 |
+
# Protobuf Python Version: 5.27.2
|
6 |
+
"""Generated protocol buffer code."""
|
7 |
+
from google.protobuf import descriptor as _descriptor
|
8 |
+
from google.protobuf import descriptor_pool as _descriptor_pool
|
9 |
+
from google.protobuf import runtime_version as _runtime_version
|
10 |
+
from google.protobuf import symbol_database as _symbol_database
|
11 |
+
from google.protobuf.internal import builder as _builder
|
12 |
+
_runtime_version.ValidateProtobufRuntimeVersion(
|
13 |
+
_runtime_version.Domain.PUBLIC,
|
14 |
+
5,
|
15 |
+
27,
|
16 |
+
2,
|
17 |
+
'',
|
18 |
+
'helloworld.proto'
|
19 |
+
)
|
20 |
+
# @@protoc_insertion_point(imports)
|
21 |
+
|
22 |
+
_sym_db = _symbol_database.Default()
|
23 |
+
|
24 |
+
|
25 |
+
|
26 |
+
|
27 |
+
DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x10helloworld.proto\x12\nhelloworld\"\x1c\n\x0cHelloRequest\x12\x0c\n\x04name\x18\x01 \x01(\t\"\x1d\n\nHelloReply\x12\x0f\n\x07message\x18\x01 \x01(\t2\xe4\x01\n\x07Greeter\x12>\n\x08SayHello\x12\x18.helloworld.HelloRequest\x1a\x16.helloworld.HelloReply\"\x00\x12K\n\x13SayHelloStreamReply\x12\x18.helloworld.HelloRequest\x1a\x16.helloworld.HelloReply\"\x00\x30\x01\x12L\n\x12SayHelloBidiStream\x12\x18.helloworld.HelloRequest\x1a\x16.helloworld.HelloReply\"\x00(\x01\x30\x01\x42\x36\n\x1bio.grpc.examples.helloworldB\x0fHelloWorldProtoP\x01\xa2\x02\x03HLWb\x06proto3')
|
28 |
+
|
29 |
+
_globals = globals()
|
30 |
+
_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals)
|
31 |
+
_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'helloworld_pb2', _globals)
|
32 |
+
if not _descriptor._USE_C_DESCRIPTORS:
|
33 |
+
_globals['DESCRIPTOR']._loaded_options = None
|
34 |
+
_globals['DESCRIPTOR']._serialized_options = b'\n\033io.grpc.examples.helloworldB\017HelloWorldProtoP\001\242\002\003HLW'
|
35 |
+
_globals['_HELLOREQUEST']._serialized_start=32
|
36 |
+
_globals['_HELLOREQUEST']._serialized_end=60
|
37 |
+
_globals['_HELLOREPLY']._serialized_start=62
|
38 |
+
_globals['_HELLOREPLY']._serialized_end=91
|
39 |
+
_globals['_GREETER']._serialized_start=94
|
40 |
+
_globals['_GREETER']._serialized_end=322
|
41 |
+
# @@protoc_insertion_point(module_scope)
|
helloworld_pb2.pyi
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from google.protobuf import descriptor as _descriptor
|
2 |
+
from google.protobuf import message as _message
|
3 |
+
from typing import ClassVar as _ClassVar, Optional as _Optional
|
4 |
+
|
5 |
+
DESCRIPTOR: _descriptor.FileDescriptor
|
6 |
+
|
7 |
+
class HelloRequest(_message.Message):
|
8 |
+
__slots__ = ("name",)
|
9 |
+
NAME_FIELD_NUMBER: _ClassVar[int]
|
10 |
+
name: str
|
11 |
+
def __init__(self, name: _Optional[str] = ...) -> None: ...
|
12 |
+
|
13 |
+
class HelloReply(_message.Message):
|
14 |
+
__slots__ = ("message",)
|
15 |
+
MESSAGE_FIELD_NUMBER: _ClassVar[int]
|
16 |
+
message: str
|
17 |
+
def __init__(self, message: _Optional[str] = ...) -> None: ...
|
helloworld_pb2_grpc.py
ADDED
@@ -0,0 +1,187 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
|
2 |
+
"""Client and server classes corresponding to protobuf-defined services."""
|
3 |
+
import grpc
|
4 |
+
import warnings
|
5 |
+
|
6 |
+
import helloworld_pb2 as helloworld__pb2
|
7 |
+
|
8 |
+
GRPC_GENERATED_VERSION = '1.66.0'
|
9 |
+
GRPC_VERSION = grpc.__version__
|
10 |
+
_version_not_supported = False
|
11 |
+
|
12 |
+
try:
|
13 |
+
from grpc._utilities import first_version_is_lower
|
14 |
+
_version_not_supported = first_version_is_lower(GRPC_VERSION, GRPC_GENERATED_VERSION)
|
15 |
+
except ImportError:
|
16 |
+
_version_not_supported = True
|
17 |
+
|
18 |
+
if _version_not_supported:
|
19 |
+
raise RuntimeError(
|
20 |
+
f'The grpc package installed is at version {GRPC_VERSION},'
|
21 |
+
+ f' but the generated code in helloworld_pb2_grpc.py depends on'
|
22 |
+
+ f' grpcio>={GRPC_GENERATED_VERSION}.'
|
23 |
+
+ f' Please upgrade your grpc module to grpcio>={GRPC_GENERATED_VERSION}'
|
24 |
+
+ f' or downgrade your generated code using grpcio-tools<={GRPC_VERSION}.'
|
25 |
+
)
|
26 |
+
|
27 |
+
|
28 |
+
class GreeterStub(object):
|
29 |
+
"""The greeting service definition.
|
30 |
+
"""
|
31 |
+
|
32 |
+
def __init__(self, channel):
|
33 |
+
"""Constructor.
|
34 |
+
|
35 |
+
Args:
|
36 |
+
channel: A grpc.Channel.
|
37 |
+
"""
|
38 |
+
self.SayHello = channel.unary_unary(
|
39 |
+
'/helloworld.Greeter/SayHello',
|
40 |
+
request_serializer=helloworld__pb2.HelloRequest.SerializeToString,
|
41 |
+
response_deserializer=helloworld__pb2.HelloReply.FromString,
|
42 |
+
_registered_method=True)
|
43 |
+
self.SayHelloStreamReply = channel.unary_stream(
|
44 |
+
'/helloworld.Greeter/SayHelloStreamReply',
|
45 |
+
request_serializer=helloworld__pb2.HelloRequest.SerializeToString,
|
46 |
+
response_deserializer=helloworld__pb2.HelloReply.FromString,
|
47 |
+
_registered_method=True)
|
48 |
+
self.SayHelloBidiStream = channel.stream_stream(
|
49 |
+
'/helloworld.Greeter/SayHelloBidiStream',
|
50 |
+
request_serializer=helloworld__pb2.HelloRequest.SerializeToString,
|
51 |
+
response_deserializer=helloworld__pb2.HelloReply.FromString,
|
52 |
+
_registered_method=True)
|
53 |
+
|
54 |
+
|
55 |
+
class GreeterServicer(object):
|
56 |
+
"""The greeting service definition.
|
57 |
+
"""
|
58 |
+
|
59 |
+
def SayHello(self, request, context):
|
60 |
+
"""Sends a greeting
|
61 |
+
"""
|
62 |
+
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
|
63 |
+
context.set_details('Method not implemented!')
|
64 |
+
raise NotImplementedError('Method not implemented!')
|
65 |
+
|
66 |
+
def SayHelloStreamReply(self, request, context):
|
67 |
+
"""Missing associated documentation comment in .proto file."""
|
68 |
+
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
|
69 |
+
context.set_details('Method not implemented!')
|
70 |
+
raise NotImplementedError('Method not implemented!')
|
71 |
+
|
72 |
+
def SayHelloBidiStream(self, request_iterator, context):
|
73 |
+
"""Missing associated documentation comment in .proto file."""
|
74 |
+
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
|
75 |
+
context.set_details('Method not implemented!')
|
76 |
+
raise NotImplementedError('Method not implemented!')
|
77 |
+
|
78 |
+
|
79 |
+
def add_GreeterServicer_to_server(servicer, server):
|
80 |
+
rpc_method_handlers = {
|
81 |
+
'SayHello': grpc.unary_unary_rpc_method_handler(
|
82 |
+
servicer.SayHello,
|
83 |
+
request_deserializer=helloworld__pb2.HelloRequest.FromString,
|
84 |
+
response_serializer=helloworld__pb2.HelloReply.SerializeToString,
|
85 |
+
),
|
86 |
+
'SayHelloStreamReply': grpc.unary_stream_rpc_method_handler(
|
87 |
+
servicer.SayHelloStreamReply,
|
88 |
+
request_deserializer=helloworld__pb2.HelloRequest.FromString,
|
89 |
+
response_serializer=helloworld__pb2.HelloReply.SerializeToString,
|
90 |
+
),
|
91 |
+
'SayHelloBidiStream': grpc.stream_stream_rpc_method_handler(
|
92 |
+
servicer.SayHelloBidiStream,
|
93 |
+
request_deserializer=helloworld__pb2.HelloRequest.FromString,
|
94 |
+
response_serializer=helloworld__pb2.HelloReply.SerializeToString,
|
95 |
+
),
|
96 |
+
}
|
97 |
+
generic_handler = grpc.method_handlers_generic_handler(
|
98 |
+
'helloworld.Greeter', rpc_method_handlers)
|
99 |
+
server.add_generic_rpc_handlers((generic_handler,))
|
100 |
+
server.add_registered_method_handlers('helloworld.Greeter', rpc_method_handlers)
|
101 |
+
|
102 |
+
|
103 |
+
# This class is part of an EXPERIMENTAL API.
|
104 |
+
class Greeter(object):
|
105 |
+
"""The greeting service definition.
|
106 |
+
"""
|
107 |
+
|
108 |
+
@staticmethod
|
109 |
+
def SayHello(request,
|
110 |
+
target,
|
111 |
+
options=(),
|
112 |
+
channel_credentials=None,
|
113 |
+
call_credentials=None,
|
114 |
+
insecure=False,
|
115 |
+
compression=None,
|
116 |
+
wait_for_ready=None,
|
117 |
+
timeout=None,
|
118 |
+
metadata=None):
|
119 |
+
return grpc.experimental.unary_unary(
|
120 |
+
request,
|
121 |
+
target,
|
122 |
+
'/helloworld.Greeter/SayHello',
|
123 |
+
helloworld__pb2.HelloRequest.SerializeToString,
|
124 |
+
helloworld__pb2.HelloReply.FromString,
|
125 |
+
options,
|
126 |
+
channel_credentials,
|
127 |
+
insecure,
|
128 |
+
call_credentials,
|
129 |
+
compression,
|
130 |
+
wait_for_ready,
|
131 |
+
timeout,
|
132 |
+
metadata,
|
133 |
+
_registered_method=True)
|
134 |
+
|
135 |
+
@staticmethod
|
136 |
+
def SayHelloStreamReply(request,
|
137 |
+
target,
|
138 |
+
options=(),
|
139 |
+
channel_credentials=None,
|
140 |
+
call_credentials=None,
|
141 |
+
insecure=False,
|
142 |
+
compression=None,
|
143 |
+
wait_for_ready=None,
|
144 |
+
timeout=None,
|
145 |
+
metadata=None):
|
146 |
+
return grpc.experimental.unary_stream(
|
147 |
+
request,
|
148 |
+
target,
|
149 |
+
'/helloworld.Greeter/SayHelloStreamReply',
|
150 |
+
helloworld__pb2.HelloRequest.SerializeToString,
|
151 |
+
helloworld__pb2.HelloReply.FromString,
|
152 |
+
options,
|
153 |
+
channel_credentials,
|
154 |
+
insecure,
|
155 |
+
call_credentials,
|
156 |
+
compression,
|
157 |
+
wait_for_ready,
|
158 |
+
timeout,
|
159 |
+
metadata,
|
160 |
+
_registered_method=True)
|
161 |
+
|
162 |
+
@staticmethod
|
163 |
+
def SayHelloBidiStream(request_iterator,
|
164 |
+
target,
|
165 |
+
options=(),
|
166 |
+
channel_credentials=None,
|
167 |
+
call_credentials=None,
|
168 |
+
insecure=False,
|
169 |
+
compression=None,
|
170 |
+
wait_for_ready=None,
|
171 |
+
timeout=None,
|
172 |
+
metadata=None):
|
173 |
+
return grpc.experimental.stream_stream(
|
174 |
+
request_iterator,
|
175 |
+
target,
|
176 |
+
'/helloworld.Greeter/SayHelloBidiStream',
|
177 |
+
helloworld__pb2.HelloRequest.SerializeToString,
|
178 |
+
helloworld__pb2.HelloReply.FromString,
|
179 |
+
options,
|
180 |
+
channel_credentials,
|
181 |
+
insecure,
|
182 |
+
call_credentials,
|
183 |
+
compression,
|
184 |
+
wait_for_ready,
|
185 |
+
timeout,
|
186 |
+
metadata,
|
187 |
+
_registered_method=True)
|