import os import socket class Sender(object): def __init__(self, app): self.app = app addr, port = os.getenv('CONSUMER_ADDRESS').split(':') self.address = (addr, int(port)) self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) def __del__(self): self.socket.close() def send(self, message): try: self.socket.sendto(message, self.address) except Exception as e: with self.app.app_context(): self.app.logger.error('Failed to send message: %s', str(e))