github.com/hashicorp/go-plugin@v1.6.0/examples/grpc/plugin-python/plugin.py (about) 1 # Copyright (c) HashiCorp, Inc. 2 # SPDX-License-Identifier: MPL-2.0 3 4 from concurrent import futures 5 import sys 6 import time 7 8 import grpc 9 10 from proto import kv_pb2 11 from proto import kv_pb2_grpc 12 13 from grpc_health.v1.health import HealthServicer 14 from grpc_health.v1 import health_pb2, health_pb2_grpc 15 16 class KVServicer(kv_pb2_grpc.KVServicer): 17 """Implementation of KV service.""" 18 19 def Get(self, request, context): 20 filename = "kv_"+request.key 21 with open(filename, 'r+b') as f: 22 result = kv_pb2.GetResponse() 23 result.value = f.read() 24 return result 25 26 def Put(self, request, context): 27 filename = "kv_"+request.key 28 value = "{0}\n\nWritten from plugin-python".format(request.value) 29 with open(filename, 'w') as f: 30 f.write(value) 31 32 return kv_pb2.Empty() 33 34 def serve(): 35 # We need to build a health service to work with go-plugin 36 health = HealthServicer() 37 health.set("plugin", health_pb2.HealthCheckResponse.ServingStatus.Value('SERVING')) 38 39 # Start the server. 40 server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) 41 kv_pb2_grpc.add_KVServicer_to_server(KVServicer(), server) 42 health_pb2_grpc.add_HealthServicer_to_server(health, server) 43 server.add_insecure_port('127.0.0.1:1234') 44 server.start() 45 46 # Output information 47 print("1|1|tcp|127.0.0.1:1234|grpc") 48 sys.stdout.flush() 49 50 try: 51 while True: 52 time.sleep(60 * 60 * 24) 53 except KeyboardInterrupt: 54 server.stop(0) 55 56 if __name__ == '__main__': 57 serve()