Update: add proto3 document link, change chinese translation and comment of 'unary'

pull/19938/head
kerbalwzy 5 years ago
parent deee1f0826
commit 25a1caa9b0
  1. 7
      examples/python/data_transmission/client.py
  2. 20
      examples/python/data_transmission/demo.proto
  3. 7
      examples/python/data_transmission/server.py

@ -4,12 +4,13 @@ import grpc
import demo_pb2_grpc import demo_pb2_grpc
import demo_pb2 import demo_pb2
SERVER_ADDRESS = "localhost:23334" SERVER_ADDRESS = "localhost:23333"
CLIENT_ID = 1 CLIENT_ID = 1
# 简单模式 # 一元模式(在一次调用中, 客户端只能向服务器传输一次请求数据, 服务器也只能返回一次响应)
# unary-unary # unary-unary(In a single call, the client can only send request once, and the server can
# only respond once.)
def simple_method(stub): def simple_method(stub):
print("--------------Call SimpleMethod Begin--------------") print("--------------Call SimpleMethod Begin--------------")
request = demo_pb2.Request(client_id=CLIENT_ID, request_data="called by Python client") request = demo_pb2.Request(client_id=CLIENT_ID, request_data="called by Python client")

@ -1,9 +1,11 @@
// //
// Syntax version declaration. Must be placed on the first line of non-commentary. // Syntax version declaration. Must be placed on the first line of non-commentary.
syntax = "proto3"; syntax = "proto3";
// The document of proto3: https://developers.google.com/protocol-buffers/docs/proto3
// , Python中使用时可以省略不写(PS:Go中使用,) // , Python中使用时可以省略不写
// Package name definition, which can be omitted in Python. (PS: I'll use it again in Go, so stay here) // Package name definition, which can be omitted in Python.
package demo; package demo;
/* /*
@ -12,8 +14,8 @@ package demo;
Python中定义一个类或者Golang中定义一个结构体 Python中定义一个类或者Golang中定义一个结构体
*/ */
/* /*
`message` is used to define the structure of the data to be transmitted, After the equal sign is the field number. `message` is used to define the structure of the data to be transmitted, after the equal sign
Each field in the message definition has a unique number. is the field number. Each field in the message definition has a unique number.
The overall format is similar to defining a class in Python or a structure in Golang. The overall format is similar to defining a class in Python or a structure in Golang.
*/ */
message Request { message Request {
@ -26,11 +28,13 @@ message Response {
string response_data = 2; string response_data = 2;
} }
// service是用来给GRPC服务定义方法的, , Golang中定义一个接口 // `service` gRPC服务定义方法的, , Golang中定义一个接口
// `service` is used to define methods for GRPC services in a fixed format, similar to defining an interface in Golang // `service` is used to define methods for gRPC services in a fixed format, similar to defining
//an interface in Golang
service GRPCDemo { service GRPCDemo {
// // (, , )
// unary-unary // unary-unary(In a single call, the client can only send request once, and the server can
// only respond once.)
rpc SimpleMethod (Request) returns (Response); rpc SimpleMethod (Request) returns (Response);
// , , // , ,

@ -6,14 +6,15 @@ from concurrent import futures
import demo_pb2_grpc import demo_pb2_grpc
import demo_pb2 import demo_pb2
SERVER_ADDRESS = 'localhost:23334' SERVER_ADDRESS = 'localhost:23333'
SERVER_ID = 1 SERVER_ID = 1
class DemoServer(demo_pb2_grpc.GRPCDemoServicer): class DemoServer(demo_pb2_grpc.GRPCDemoServicer):
# 简单模式 # 一元模式(在一次调用中, 客户端只能向服务器传输一次请求数据, 服务器也只能返回一次响应)
# unary-unary # unary-unary(In a single call, the client can only send request once, and the server can
# only respond once.)
def SimpleMethod(self, request, context): def SimpleMethod(self, request, context):
print("SimpleMethod called by client(%d) the message: %s" % (request.client_id, request.request_data)) print("SimpleMethod called by client(%d) the message: %s" % (request.client_id, request.request_data))
response = demo_pb2.Response(server_id=SERVER_ID, response_data="Python server SimpleMethod Ok!!!!") response = demo_pb2.Response(server_id=SERVER_ID, response_data="Python server SimpleMethod Ok!!!!")

Loading…
Cancel
Save