python uses grpc and packages it into python module

xmlrpc is still a feasible solutionI. environmentpython3.6 II. Installation module pip3 install grpcio pip3 install protobuf pip3 install grpcio-tool...

xmlrpc is still a feasible solution
I. environment
python3.6
II. Installation module

pip3 install grpcio pip3 install protobuf pip3 install grpcio-tools

3. Prepare grpc configuration file grpcdatabase.proto
Directory structure:

The contents are as follows:

syntax = "proto3"; package grpcServer; service Greeter { rpc GetSummary (Request) returns (Return) {} //Define the function to be called (GetSummary)+(Request) accept parameter + (Return) return parameter rpc GetSynonym (Request) returns (Return) {} } message Request { //Parameter data type string content = 1;//text int32 code=2; //Return status 0success; 1failed } message Return { //Return data type string message = 1;//text int32 code=2; //Return status 0success; 1failed } //Execute command + installation steps //python3 -m grpc_tools.protoc -I. --python_out=grpc_base_models/ --grpc_python_out=grpc_base_models/ grpcdatabase.proto

Compile: generate grpcatabase? Pb2.py? Grpcdatabase? PB2? Grpc.py file

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


Write server code:

# -*- coding: utf-8 -*- # @author: chenhuachao # @time: 2019/3/7 # Servers.py import sys sys.path.append('grpc_base_models') import grpc import time from concurrent import futures import grpcdatabase_pb2 import grpcdatabase_pb2_grpc # from grpc_base_models import grpcdatabase_pb2 # from grpc_base_models import grpcdatabase_pb2_grpc _SLEEP_TIME = 60 _HOST = "0.0.0.0" _PORT = "19999" class RpcServer(grpcdatabase_pb2_grpc.GreeterServicer): def GetContent(self, request, context): ''' //Get article summary :param request: :param context: :return: ''' try: _content = request.content code = 0 except Exception as e: _content = str(e) code=1 return grpcdatabase_pb2.Return(message=_content,code=code) def server(): if sys.argv.__len__()>=2: _PORT = sys.argv[1] else: _PORT = "19999" grpcServer = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) grpcdatabase_pb2_grpc.add_GreeterServicer_to_server(RpcServer(), grpcServer) grpcServer.add_insecure_port(":".format(_HOST, _PORT)) grpcServer.start() try: while True: time.sleep(_SLEEP_TIME) except KeyboardInterrupt: grpcServer.stop(0) if __name__ == '__main__': server()

Write client code:

# -*- coding: utf-8 -*- # @author: chenhuachao # @time: 2019/3/7 # Client.py import sys import grpc sys.path.append('grpc_base_models') import grpcdatabase_pb2_grpc import grpcdatabase_pb2 # from grpc_base_models import grpcdatabase_pb2_grpc # from grpc_base_models import grpcdatabase_pb2 # _HOST = '192.168.3.191' _HOST = '127.0.0.1' _PORT = '19999' def RpcClient(funcname,content): ''' rpc Client program :param funcname: available funcname For the following two >>> GetContent Get summary, parameters: content='text' *** Both of the above functions return message Attribute and code(1:failed 0:success)attribute >>> Return value: response.message response.code :return: ''' with grpc.insecure_channel(":".format(_HOST, _PORT)) as channel: client = grpcdatabase_pb2_grpc.GreeterStub(channel=channel) if hasattr(client,funcname): response = getattr(client,funcname)(grpcdatabase_pb2.Request(content=content)) else: raise Exception(u"Function name error") print("message=" , response.message) print( "code=",response.code) if __name__ == '__main__': text = u''' //Test text ''' RpcClient('GetContent',text)

Run: Server.py and Client.py respectively to view the results

Each time the above server-side code is used, it depends on the two files grpcdatabase [PB2 *. Py. Therefore, we can package them as python modules. More convenient to use Pack

Add the setup.py file in the root directory: structure chart

The contents of the setup.py file are as follows

# -*- coding: utf-8 -*- # @author: chenhuachao # @time: 2019/3/8 # setup.py from setuptools import setup,find_packages setup( name = "grpc_base_models", version = "0.0.1", keywords = ("pip", "pygrpc", "company", "chenhuachao"), description = "python Versions grpc Common module,For personal projects,For reference only", long_description="grpc server for python", license="MIT Licence", url="https://github.com/leizhu900516", author="chenhuachao", author_email="[email protected]", packages = find_packages(), install_requires = [ 'grpcio==1.19.0', 'grpcio-tools==1.19.0', 'protobuf==3.7.0', ] )

Pack:
python3 setup.py sdist is as follows:

Install: PIP install dist / grpc? Base? Models-0.0.1.tar.gz
Can be used in python scripts
By reference:

from grpc_base_models import grpcdatabase_pb2_grpc from grpc_base_models import grpcdatabase_pb2

**Attach code address grpc instance of python

3 December 2019, 13:56 | Views: 7972

Add new comment

For adding a comment, please log in
or create account

0 comments