async def run(loop): nc = NATS() await nc.connect(servers=["nats://localhost:4222"], io_loop=loop) sc = STAN() await sc.connect("test-cluster", "dataeng-publisher", nats=nc) # sebelumnya kita publish angka saja # i = 0 # while True: # print(f"publish {i}") # await sc.publish("hitung", str(i).encode()) # i += 1 # time.sleep(1) # lalu kita ingin publish message dalam bentuk protocol buffers write_card_holder = WriteCardHolder() while True: card_holder_book = holder_pb2.CardHolder() msg = write_card_holder.PromptForAddress(card_holder_book) print("Publish", msg) await sc.publish("hitung", str(msg).encode()) time.sleep(1) def signal_handler(): if nc.is_closed: return print('Disconnecting...') loop.create_task(sc.close()) loop.create_task(nc.close()) for sig in ('SIGINT', 'SIGTERM'): loop.add_signal_handler(getattr(signal, sig), signal_handler)
def __init__(self): self.card_holder = holder_pb2.CardHolder()
# This function fills in a Person message based on user input. def PromptForAddress(self,card_holder): fake = Faker() card_holder.name = fake.name() card_holder.job = fake.job() card_holder.phone_number = fake.phone_number() card_holder.address = fake.address() card_holder.card_number = fake.credit_card_number() card_holder.card_provider = fake.credit_card_provider() return card_holder class ReadCardHolder: def __init__(self): self.card_holder = Holder.CardHolder() def ListPeople(self): print("Name:", card_holder.name) print("Job:", card_holder.job) print("Phone Number:", card_holder.phone_number) print("Address:", card_holder.address) print("Card Number:", card_holder.card_number) print("Card Provider:", card_holder.card_provider) if __name__ == "__main__": write_card_holder = WriteCardHolder() card_holder = Holder.CardHolder() a = write_card_holder.PromptForAddress(card_holder) serialize_a = a.SerializeToString() read_card_holder = ReadCardHolder() read_card_holder.card_holder.ParseFromString(serialize_a) read_card_holder.ListPeople()
def __init__(self): self.card_holder = Holder.CardHolder()
import holder_pb2 from faker import Faker class WriteCardHolder: def generate(self, card_holder): fake = Faker() card_holder.name = fake.name() card_holder.job = fake.job() card_holder.phone_number = fake.phone_number() card_holder.address = fake.address() card_holder.card_number = fake.credit_card_number() card_holder.card_provider = fake.credit_card_provider() return card_holder if __name__ == "__main__": write_card_holder = WriteCardHolder() card_holder_book = holder_pb2.CardHolder() f = open("records.bin", "ab") for x in range(10): a = write_card_holder.generate(card_holder_book) f.write(a.SerializeToString()) f.write(b';') f.close()