pull/21458/head
Richard Belleville 5 years ago
parent fd0d0f77df
commit 999bbf1401
  1. 8
      examples/python/data_transmission/client.py
  2. 4
      examples/python/data_transmission/server.py
  3. 8
      examples/python/helloworld/greeter_client_with_options.py
  4. 4
      examples/python/interceptors/default_value/greeter_client.py
  5. 4
      examples/python/interceptors/headers/greeter_client.py
  6. 7
      examples/python/interceptors/headers/greeter_server.py
  7. 4
      examples/python/metadata/metadata_client.py
  8. 4
      examples/python/metadata/metadata_server.py
  9. 27
      examples/python/multiplex/multiplex_client.py
  10. 11
      examples/python/multiplex/multiplex_server.py
  11. 3
      examples/python/multiplex/route_guide_resources.py
  12. 22
      examples/python/route_guide/route_guide_client.py
  13. 3
      examples/python/route_guide/route_guide_resources.py
  14. 7
      examples/python/route_guide/route_guide_server.py
  15. 1
      src/python/grpcio/grpc/_runtime_protos.py
  16. 6
      src/python/grpcio_tests/tests/unit/BUILD.bazel
  17. 18
      tools/distrib/python/grpcio_tools/BUILD.bazel
  18. 4
      tools/distrib/python/grpcio_tools/grpc_tools/protoc.py
  19. 20
      tools/distrib/python/grpcio_tools/grpc_tools/test/BUILD.bazel

@ -36,8 +36,8 @@ CLIENT_ID = 1
# only respond once.) # only respond once.)
def simple_method(stub): def simple_method(stub):
print("--------------Call SimpleMethod Begin--------------") print("--------------Call SimpleMethod Begin--------------")
request = protos.Request( request = protos.Request(client_id=CLIENT_ID,
client_id=CLIENT_ID, request_data="called by Python client") request_data="called by Python client")
response = stub.SimpleMethod(request) response = stub.SimpleMethod(request)
print("resp from server(%d), the message=%s" % print("resp from server(%d), the message=%s" %
(response.server_id, response.response_data)) (response.server_id, response.response_data))
@ -70,8 +70,8 @@ def client_streaming_method(stub):
# but the server can return the response many times.) # but the server can return the response many times.)
def server_streaming_method(stub): def server_streaming_method(stub):
print("--------------Call ServerStreamingMethod Begin--------------") print("--------------Call ServerStreamingMethod Begin--------------")
request = protos.Request( request = protos.Request(client_id=CLIENT_ID,
client_id=CLIENT_ID, request_data="called by Python client") request_data="called by Python client")
response_iterator = stub.ServerStreamingMethod(request) response_iterator = stub.ServerStreamingMethod(request)
for response in response_iterator: for response in response_iterator:
print("recv from server(%d), message=%s" % print("recv from server(%d), message=%s" %

@ -44,8 +44,8 @@ class DemoServer(services.GRPCDemoServicer):
def ClientStreamingMethod(self, request_iterator, context): def ClientStreamingMethod(self, request_iterator, context):
print("ClientStreamingMethod called by client...") print("ClientStreamingMethod called by client...")
for request in request_iterator: for request in request_iterator:
print("recv from client(%d), message= %s" % (request.client_id, print("recv from client(%d), message= %s" %
request.request_data)) (request.client_id, request.request_data))
response = protos.Response( response = protos.Response(
server_id=SERVER_ID, server_id=SERVER_ID,
response_data="Python server ClientStreamingMethod ok") response_data="Python server ClientStreamingMethod ok")

@ -27,11 +27,11 @@ def run():
# of the code. # of the code.
# #
# For more channel options, please see https://grpc.io/grpc/core/group__grpc__arg__keys.html # For more channel options, please see https://grpc.io/grpc/core/group__grpc__arg__keys.html
with grpc.insecure_channel( with grpc.insecure_channel(target='localhost:50051',
target='localhost:50051',
options=[('grpc.lb_policy_name', 'pick_first'), options=[('grpc.lb_policy_name', 'pick_first'),
('grpc.enable_retries', 0), ('grpc.keepalive_timeout_ms', ('grpc.enable_retries', 0),
10000)]) as channel: ('grpc.keepalive_timeout_ms', 10000)
]) as channel:
stub = services.GreeterStub(channel) stub = services.GreeterStub(channel)
# Timeout in seconds. # Timeout in seconds.
# Please refer gRPC Python documents for more detail. https://grpc.io/grpc/python/grpc.html # Please refer gRPC Python documents for more detail. https://grpc.io/grpc/python/grpc.html

@ -18,8 +18,8 @@ import logging
import grpc import grpc
protos, services = grpc.protos_and_services( protos, services = grpc.protos_and_services("protos/helloworld.proto",
"protos/helloworld.proto", include_paths=["../../.."]) include_paths=["../../.."])
import default_value_client_interceptor import default_value_client_interceptor

@ -18,8 +18,8 @@ import logging
import grpc import grpc
protos, services = grpc.protos_and_services( protos, services = grpc.protos_and_services("protos/helloworld.protos",
"protos/helloworld.protos", include_paths=["../../.."]) include_paths=["../../.."])
import header_manipulator_client_interceptor import header_manipulator_client_interceptor

@ -18,8 +18,8 @@ import logging
import grpc import grpc
protos, services = grpc.protos_and_services( protos, services = grpc.protos_and_services("protos/helloworld.protos",
"protos/helloworld.protos", include_paths=["../../.."]) include_paths=["../../.."])
from request_header_validator_interceptor import RequestHeaderValidatorInterceptor from request_header_validator_interceptor import RequestHeaderValidatorInterceptor
@ -33,8 +33,7 @@ def serve():
header_validator = RequestHeaderValidatorInterceptor( header_validator = RequestHeaderValidatorInterceptor(
'one-time-password', '42', grpc.StatusCode.UNAUTHENTICATED, 'one-time-password', '42', grpc.StatusCode.UNAUTHENTICATED,
'Access denied!') 'Access denied!')
server = grpc.server( server = grpc.server(futures.ThreadPoolExecutor(max_workers=10),
futures.ThreadPoolExecutor(max_workers=10),
interceptors=(header_validator,)) interceptors=(header_validator,))
services.add_GreeterServicer_to_server(Greeter(), server) services.add_GreeterServicer_to_server(Greeter(), server)
server.add_insecure_port('[::]:50051') server.add_insecure_port('[::]:50051')

@ -18,8 +18,8 @@ import logging
import grpc import grpc
protos, services = grpc.protos_and_services( protos, services = grpc.protos_and_services("protos/helloworld.proto",
"protos/helloworld.proto", include_paths=["../.."]) include_paths=["../.."])
def run(): def run():

@ -19,8 +19,8 @@ import logging
import grpc import grpc
protos, services = grpc.protos_and_services( protos, services = grpc.protos_and_services("protos/helloworld.proto",
"protos/helloworld.proto", include_paths=["../.."]) include_paths=["../.."])
class Greeter(services.GreeterServicer): class Greeter(services.GreeterServicer):

@ -21,17 +21,17 @@ import logging
import grpc import grpc
hw_protos, hw_services = grpc.protos_and_services( hw_protos, hw_services = grpc.protos_and_services("protos/helloworld.proto",
"protos/helloworld.proto", include_paths=["../.."]) include_paths=["../.."])
rg_protos, rg_services = grpc.protos_and_services( rg_protos, rg_services = grpc.protos_and_services("protos/route_guide.proto",
"protos/route_guide.proto", include_paths=["../.."]) include_paths=["../.."])
import route_guide_resources import route_guide_resources
def make_route_note(message, latitude, longitude): def make_route_note(message, latitude, longitude):
return rg_protos.RouteNote( return rg_protos.RouteNote(message=message,
message=message, location=rg_protos.Point(latitude=latitude,
location=rg_protos.Point(latitude=latitude, longitude=longitude)) longitude=longitude))
def guide_get_one_feature(route_guide_stub, point): def guide_get_one_feature(route_guide_stub, point):
@ -47,17 +47,18 @@ def guide_get_one_feature(route_guide_stub, point):
def guide_get_feature(route_guide_stub): def guide_get_feature(route_guide_stub):
guide_get_one_feature(route_guide_stub, guide_get_one_feature(
rg_protos.Point( route_guide_stub,
latitude=409146138, longitude=-746188906)) rg_protos.Point(latitude=409146138, longitude=-746188906))
guide_get_one_feature(route_guide_stub, guide_get_one_feature(route_guide_stub,
rg_protos.Point(latitude=0, longitude=0)) rg_protos.Point(latitude=0, longitude=0))
def guide_list_features(route_guide_stub): def guide_list_features(route_guide_stub):
rectangle = rg_protos.Rectangle( rectangle = rg_protos.Rectangle(lo=rg_protos.Point(latitude=400000000,
lo=rg_protos.Point(latitude=400000000, longitude=-750000000), longitude=-750000000),
hi=rg_protos.Point(latitude=420000000, longitude=-730000000)) hi=rg_protos.Point(latitude=420000000,
longitude=-730000000))
print("Looking for features between 40, -75 and 42, -73") print("Looking for features between 40, -75 and 42, -73")
features = route_guide_stub.ListFeatures(rectangle) features = route_guide_stub.ListFeatures(rectangle)

@ -20,10 +20,10 @@ import logging
import grpc import grpc
hw_protos, hw_services = grpc.protos_and_services( hw_protos, hw_services = grpc.protos_and_services("protos/helloworld.proto",
"protos/helloworld.proto", include_paths=["../.."]) include_paths=["../.."])
rg_protos, rg_services = grpc.protos_and_services( rg_protos, rg_services = grpc.protos_and_services("protos/route_guide.proto",
"protos/route_guide.proto", include_paths=["../.."]) include_paths=["../.."])
import route_guide_resources import route_guide_resources
@ -103,8 +103,7 @@ class _RouteGuideServicer(rg_services.RouteGuideServicer):
prev_point = point prev_point = point
elapsed_time = time.time() - start_time elapsed_time = time.time() - start_time
return rg_protos.RouteSummary( return rg_protos.RouteSummary(point_count=point_count,
point_count=point_count,
feature_count=feature_count, feature_count=feature_count,
distance=int(distance), distance=int(distance),
elapsed_time=int(elapsed_time)) elapsed_time=int(elapsed_time))

@ -32,8 +32,7 @@ def read_route_guide_database():
for item in json.load(route_guide_db_file): for item in json.load(route_guide_db_file):
feature = protos.Feature( feature = protos.Feature(
name=item["name"], name=item["name"],
location=protos.Point( location=protos.Point(latitude=item["location"]["latitude"],
latitude=item["location"]["latitude"],
longitude=item["location"]["longitude"])) longitude=item["location"]["longitude"]))
feature_list.append(feature) feature_list.append(feature)
return feature_list return feature_list

@ -20,15 +20,15 @@ import logging
import grpc import grpc
protos, services = grpc.protos_and_services( protos, services = grpc.protos_and_services("protos/route_guide.proto",
"protos/route_guide.proto", include_paths=["../.."]) include_paths=["../.."])
import route_guide_resources import route_guide_resources
def make_route_note(message, latitude, longitude): def make_route_note(message, latitude, longitude):
return protos.RouteNote( return protos.RouteNote(message=message,
message=message, location=protos.Point(latitude=latitude,
location=protos.Point(latitude=latitude, longitude=longitude)) longitude=longitude))
def guide_get_one_feature(stub, point): def guide_get_one_feature(stub, point):
@ -44,16 +44,16 @@ def guide_get_one_feature(stub, point):
def guide_get_feature(stub): def guide_get_feature(stub):
guide_get_one_feature(stub, guide_get_one_feature(
protos.Point( stub, protos.Point(latitude=409146138, longitude=-746188906))
latitude=409146138, longitude=-746188906))
guide_get_one_feature(stub, protos.Point(latitude=0, longitude=0)) guide_get_one_feature(stub, protos.Point(latitude=0, longitude=0))
def guide_list_features(stub): def guide_list_features(stub):
rectangle = protos.Rectangle( rectangle = protos.Rectangle(lo=protos.Point(latitude=400000000,
lo=protos.Point(latitude=400000000, longitude=-750000000), longitude=-750000000),
hi=protos.Point(latitude=420000000, longitude=-730000000)) hi=protos.Point(latitude=420000000,
longitude=-730000000))
print("Looking for features between 40, -75 and 42, -73") print("Looking for features between 40, -75 and 42, -73")
features = stub.ListFeatures(rectangle) features = stub.ListFeatures(rectangle)

@ -31,8 +31,7 @@ def read_route_guide_database():
for item in json.load(route_guide_db_file): for item in json.load(route_guide_db_file):
feature = protos.Feature( feature = protos.Feature(
name=item["name"], name=item["name"],
location=protos.Point( location=protos.Point(latitude=item["location"]["latitude"],
latitude=item["location"]["latitude"],
longitude=item["location"]["longitude"])) longitude=item["location"]["longitude"]))
feature_list.append(feature) feature_list.append(feature)
return feature_list return feature_list

@ -20,8 +20,8 @@ import logging
import grpc import grpc
protos, services = grpc.protos_and_services( protos, services = grpc.protos_and_services("protos/route_guide.proto",
"protos/route_guide.proto", include_paths=["../.."]) include_paths=["../.."])
import route_guide_resources import route_guide_resources
@ -96,8 +96,7 @@ class RouteGuideServicer(services.RouteGuideServicer):
prev_point = point prev_point = point
elapsed_time = time.time() - start_time elapsed_time = time.time() - start_time
return protos.RouteSummary( return protos.RouteSummary(point_count=point_count,
point_count=point_count,
feature_count=feature_count, feature_count=feature_count,
distance=int(distance), distance=int(distance),
elapsed_time=int(elapsed_time)) elapsed_time=int(elapsed_time))

@ -14,6 +14,7 @@
import sys import sys
def _uninstalled_protos(*args, **kwargs): def _uninstalled_protos(*args, **kwargs):
raise NotImplementedError( raise NotImplementedError(
"Install the grpcio-tools package to use the protos function.") "Install the grpcio-tools package to use the protos function.")

@ -116,15 +116,15 @@ py2and3_test(
name = "_dynamic_stubs_test", name = "_dynamic_stubs_test",
size = "small", size = "small",
srcs = ["_dynamic_stubs_test.py"], srcs = ["_dynamic_stubs_test.py"],
main = "_dynamic_stubs_test.py",
imports = ["../../"],
data = [ data = [
"data/foo/bar.proto", "data/foo/bar.proto",
], ],
imports = ["../../"],
main = "_dynamic_stubs_test.py",
deps = [ deps = [
"@six",
"//src/python/grpcio/grpc:grpcio", "//src/python/grpcio/grpc:grpcio",
"//src/python/grpcio_tests/tests/testing", "//src/python/grpcio_tests/tests/testing",
"//tools/distrib/python/grpcio_tools:grpc_tools", "//tools/distrib/python/grpcio_tools:grpc_tools",
"@six",
], ],
) )

@ -12,7 +12,10 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
package(default_visibility = ["//src/python:__subpackages__", "//tools/distrib/python/grpcio_tools:__subpackages__"]) package(default_visibility = [
"//src/python:__subpackages__",
"//tools/distrib/python/grpcio_tools:__subpackages__",
])
load("//bazel:cython_library.bzl", "pyx_library") load("//bazel:cython_library.bzl", "pyx_library")
@ -20,11 +23,11 @@ cc_library(
name = "protoc_lib", name = "protoc_lib",
srcs = ["grpc_tools/main.cc"], srcs = ["grpc_tools/main.cc"],
hdrs = ["grpc_tools/main.h"], hdrs = ["grpc_tools/main.h"],
includes = ["."],
deps = [ deps = [
"@com_google_protobuf//:protoc_lib",
"//src/compiler:grpc_plugin_support", "//src/compiler:grpc_plugin_support",
"@com_google_protobuf//:protoc_lib",
], ],
includes = ["."],
) )
pyx_library( pyx_library(
@ -35,11 +38,14 @@ pyx_library(
py_library( py_library(
name = "grpc_tools", name = "grpc_tools",
srcs = ["grpc_tools/__init__.py", "grpc_tools/protoc.py"], srcs = [
"grpc_tools/__init__.py",
"grpc_tools/protoc.py",
],
srcs_version = "PY2AND3",
deps = [ deps = [
":cyprotoc", ":cyprotoc",
"@com_google_protobuf//:protobuf_python",
"//src/python/grpcio/grpc:grpcio", "//src/python/grpcio/grpc:grpcio",
"@com_google_protobuf//:protobuf_python",
], ],
srcs_version = "PY2AND3",
) )

@ -101,8 +101,8 @@ if sys.version_info[0] > 2:
def _generated_file_to_module_name(self, filepath): def _generated_file_to_module_name(self, filepath):
components = filepath.split(os.path.sep) components = filepath.split(os.path.sep)
return ".".join( return ".".join(components[:-1] +
components[:-1] + [os.path.splitext(components[-1])[0]]) [os.path.splitext(components[-1])[0]])
def exec_module(self, module): def exec_module(self, module):
assert module.__name__ == self._module_name assert module.__name__ == self._module_name

@ -4,38 +4,38 @@ load("//bazel:python_rules.bzl", "py_grpc_library", "py_proto_library")
proto_library( proto_library(
name = "simplest_proto", name = "simplest_proto",
testonly = True,
srcs = ["simplest.proto"], srcs = ["simplest.proto"],
strip_import_prefix = "/tools/distrib/python/grpcio_tools/", strip_import_prefix = "/tools/distrib/python/grpcio_tools/",
testonly = True,
) )
proto_library( proto_library(
name = "complicated_proto", name = "complicated_proto",
testonly = True,
srcs = ["complicated.proto"], srcs = ["complicated.proto"],
deps = [":simplest_proto"],
strip_import_prefix = "/tools/distrib/python/grpcio_tools/", strip_import_prefix = "/tools/distrib/python/grpcio_tools/",
testonly = True, deps = [":simplest_proto"],
) )
py_proto_library( py_proto_library(
name = "complicated_py_pb2", name = "complicated_py_pb2",
deps = ["complicated_proto"],
testonly = True, testonly = True,
deps = ["complicated_proto"],
) )
py_test( py_test(
name = "protoc_test", name = "protoc_test",
srcs = ["protoc_test.py"], srcs = ["protoc_test.py"],
deps = [
"//tools/distrib/python/grpcio_tools:grpc_tools",
":complicated_py_pb2",
],
data = [ data = [
"complicated.proto",
"flawed.proto",
"simple.proto", "simple.proto",
"simpler.proto", "simpler.proto",
"simplest.proto", "simplest.proto",
"complicated.proto",
"flawed.proto",
], ],
python_version = "PY3", python_version = "PY3",
deps = [
":complicated_py_pb2",
"//tools/distrib/python/grpcio_tools:grpc_tools",
],
) )

Loading…
Cancel
Save