def send_mail(password, source_name, destination_name, message): request = mailbox_pb2.SendMailRequest() request.password = password request.mail.timestamp = math.floor(time.time()) request.mail.source_name = source_name request.mail.destination_name = destination_name request.mail.message = message with grpc.insecure_channel(MAILMAN_ADDRESS) as channel: stub = mailbox_pb2_grpc.MailManStub(channel) response = stub.SendMail(request) error = response.error time_until_pickup = response.time_until_pickup mails = response.mails if error: print(error) else: print( f'mail is waiting in {source_name} to be picked up by the mailman') print(f'time until pickup: {time_until_pickup} seconds') if len(mails) > 0: print( f'{len(mails)} units of mail have been removed from {source_name}' ) for mail in mails: print_mail(mail)
def remove_mailbox(name, password): request = mailbox_pb2.RemoveMailboxRequest(name=name, password=password) with grpc.insecure_channel(MAILMAN_ADDRESS) as channel: stub = mailbox_pb2_grpc.MailManStub(channel) response = stub.RemoveMailbox(request) error = response.error if error: print(error) else: print(f'{name} has been removed')
def register_mailbox(name): request = mailbox_pb2.RegisterMailboxRequest(name=name) with grpc.insecure_channel(MAILMAN_ADDRESS) as channel: stub = mailbox_pb2_grpc.MailManStub(channel) response = stub.RegisterMailbox(request) password = response.password error = response.error if password: print(f'password for {name} is {password}') else: print(error)
def list_mailboxes(query): request = mailbox_pb2.ListMailboxesRequest(query=query) with grpc.insecure_channel(MAILMAN_ADDRESS) as channel: stub = mailbox_pb2_grpc.MailManStub(channel) response = stub.ListMailboxes(request) names = response.names if len(names) > 0: for name in names: print(name)
def get_mail(name, password): request = mailbox_pb2.GetMailRequest(name=name, password=password) with grpc.insecure_channel(MAILMAN_ADDRESS) as channel: stub = mailbox_pb2_grpc.MailManStub(channel) response = stub.GetMail(request) error = response.error mails = response.mails if error: print(error) elif len(mails) == 0: print("no mail") else: print(f'{len(mails)} mails have been removed from {name}') for mail in mails: print_mail(mail)