Add petstore_server.py
This commit is contained in:
parent
d9a7e6a6f9
commit
473bb8f226
|
@ -0,0 +1,51 @@
|
|||
import gen.python.pet.v1.pet_pb2 as pb2
|
||||
from concurrent import futures
|
||||
import logging
|
||||
import grpc
|
||||
import time
|
||||
|
||||
max_id = 0
|
||||
pet_list = []
|
||||
|
||||
class Petstore(pb2._PETSTORESERVICE): # 这个地方不一定能跑通... 和讲的有出入
|
||||
def GetPet(self, request, context):
|
||||
global pet_list
|
||||
for pet in pet_list:
|
||||
if pet.pet_id == request.pet_id:
|
||||
return pb2.GetPetResponse(pet)
|
||||
return pb2.GetPetResponse()
|
||||
|
||||
def PutPet(self, request, context):
|
||||
global max_id, pet_list
|
||||
for pet in pet_list:
|
||||
if pet.pet_type == request.pet_type and pet.name == request.name:
|
||||
return pb2.PutPetResponse(pet=pet)
|
||||
new_one = pb2.Pet(pet_type=request.pet_type, pet_id=str(max_id).zfill(5), name=request.name)
|
||||
pet_list.append()
|
||||
max_id += 1
|
||||
return pb2.PutPetResponse(pet=new_one)
|
||||
|
||||
def DeletePet(self, request, context):
|
||||
global pet_list
|
||||
for pet in pet_list:
|
||||
if pet.pet_id == request.pet_id:
|
||||
pet_list.remove(pet)
|
||||
return pb2.DeletePetResponse()
|
||||
return pb2.DeletePetResponse()
|
||||
|
||||
|
||||
def serve():
|
||||
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
|
||||
pb2.add_PetStoreServiceServicer_to_server(Petstore(), server) # 这个地方不一定能跑通... 和讲的有出入
|
||||
server.add_insecure_port('[::]:23333')
|
||||
server.start()
|
||||
try:
|
||||
while True:
|
||||
time.sleep(60 * 60 * 24)
|
||||
except :
|
||||
server.stop(0)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
logging.basicConfig()
|
||||
serve()
|
Loading…
Reference in New Issue