程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python from door to master (IX): grpc implementation

編輯:Python

To be honest , I really don't approve of using python Write business server code . This chapter uses python A set of language implementation grpc frame , The example is not too complicated , When it is realized, it is ​ ​ be based on grpc Build a quasi production distributed application from scratch ( series )​​ Here's an example . The author is realizing demo I stepped on a lot of holes , Because there is little information on the Internet .

Program download address :​ ​demo-python Source code ​​ Extraction code : 26kv

One 、 Install necessary Libraries

Some versions of the packages in the following table must be the same , Otherwise, it will not be applied protobuf The problem of .

pip3
install
grpcio
==
1.43.0

pip3 install grpcio - tools == 1.43.0

pip3 install protobuf == 3.20.1
  • 1.
  • 2.
  • 3.
  • 4.

Two 、 Run the sample program

Take a look at... In the source code readme.md file . Enter the project root path and execute ,--python_out Be sure to specify the project root path ( This is a specification for lawsuit implementation , Cannot change directory ).

python3
-
m
grpc_tools.
protoc
-
-
python_out
=..
/
-
-
grpc_python_out
=..
/
-
I
=. .
/
*.
proto

  • 1.

Then run one by one :

Server side :ApplicationServerStart.py

client :SystemlogTest.py
  • 1.
  • 2.

3、 ... and 、 Command line test methods

python You cannot test with the graph line tool , The command line test method is as follows

brew
install
grpcurl

// see GRPC Service list of all services
grpcurl -- plaintext 127.0 .0 .1: 9898 list
Output is as follows :
/*grpc.health.v1.Health
grpc.reflection.v1alpha.ServerReflection
net.devh.boot.grpc.example.MyService
*/

// View the service interface provided by an interface
grpcurl -- plaintext 127.0 .0 .1: 9898 list net. devh. boot. grpc. example. MyService
Output is as follows :
/*net.devh.boot.grpc.example.MyService.SayHello*/

// Simulated access
grpcurl -- plaintext - d '{"name": "test"}' 127.0 .0 .1: 9898 net. devh. boot. grpc. example. MyService / sayHello
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved