Python gRPC
gRPC (Google Remote Procedure Call) is an open-source high-performance RPC (Remote Procedure Call) framework that can be used to build distributed systems. It enables client and server applications to communicate transparently, and simplifies the building of connected systems.
In Python, you can use the grpcio
and grpcio-tools
packages for using gRPC. Here’s a basic example of a gRPC server and client in Python:
-
First, you need to install the
grpcio
andgrpcio-tools
packages, you can use pip for that:bashpip install grpcio grpcio-tools
-
Define the service:
You need to define the service using Protocol Buffers (often abbreviated as protobuf). Here’s an example of a simple service defined in a .proto file:
protobufsyntax = "proto3";
service MyService {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}message HelloRequest {
string name = 1;
}message HelloReply {
string message = 1;
}
Save the above service definition in a file, say
myservice.proto
. -
Generate Python code:
Use the following command to generate Python code from the .proto file:
bashpython -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. myservice.proto
This will generate
myservice_pb2.py
andmyservice_pb2_grpc.py
files. -
Write server code:
Here’s an example of server code:
pythonimport grpc
from concurrent import futures
import myservice_pb2
import myservice_pb2_grpcclass MyServicer(myservice_pb2_grpc.MyServiceServicer):
def SayHello(self, request, context):
return myservice_pb2.HelloReply(message=f"Hello {request.name}")server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
myservice_pb2_grpc.add_MyServiceServicer_to_server(MyServicer(), server)
server.add_insecure_port('[::]:50051')
server.start()
server.wait_for_termination()
-
Write client code:
And here’s a corresponding client:
pythonimport grpc
import myservice_pb2
import myservice_pb2_grpcchannel = grpc.insecure_channel('localhost:50051')
stub = myservice_pb2_grpc.MyServiceStub(channel)
response = stub.SayHello(myservice_pb2.HelloRequest(name='you'))
print("Greeter client received: " + response.message)
You run the server code first, then run the client code. The client will send a “Hello” message to the server, and the server will respond with a greeting message.
Please be aware that the above code uses insecure communication for the sake of simplicity. In a production environment, you should use secure communication
Python Training Demo Day 1
Conclusion:
Unogeeks is the No.1 IT Training Institute for Python Training. Anyone Disagree? Please drop in a comment
You can check out our other latest blogs on Python here – Python Blogs
You can check out our Best In Class Python Training Details here – Python Training
Follow & Connect with us:
———————————-
For Training inquiries:
Call/Whatsapp: +91 73960 33555
Mail us at: info@unogeeks.com
Our Website ➜ https://unogeeks.com
Follow us:
Instagram: https://www.instagram.com/unogeeks
Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute
Twitter: https://twitter.com/unogeeks