def test_16_parse_int(self): r = parse_int("xxx", 12) self.assertEqual(r, 12) r = parse_int("ABC", 11) self.assertEqual(r, 2748) r = parse_int("ABCX", 11) self.assertEqual(r, 11) r = parse_int(123) self.assertEqual(r, 123) r = parse_int(0x12) self.assertEqual(r, 18) r = parse_int("0x12") self.assertEqual(r, 18) r = parse_int("123") self.assertEqual(r, 123)
def submit_message(self, phone, message): """ send a message to a phone via an smpp protocol to smsc :param phone: the phone number :param message: the message to submit to the phone :return: """ if not import_successful: # pragma: no cover log.error("smpplib can not be found!") raise SMSError(404, "smpplib can not be found!") log.debug("submitting message {0!s} to {1!s}".format(message, phone)) if not self.smsgateway: # this should not happen. We now always use sms gateway definitions. log.warning("Missing smsgateway definition!") raise SMSError(-1, "Missing smsgateway definition!") smsc_host = self.smsgateway.option_dict.get("SMSC_HOST") smsc_port = self.smsgateway.option_dict.get("SMSC_PORT") sys_id = self.smsgateway.option_dict.get("SYSTEM_ID") passwd = self.smsgateway.option_dict.get("PASSWORD") s_addr_ton = parse_int(self.smsgateway.option_dict.get("S_ADDR_TON")) s_addr_npi = parse_int(self.smsgateway.option_dict.get("S_ADDR_NPI")) s_addr = self.smsgateway.option_dict.get("S_ADDR") d_addr_ton = parse_int(self.smsgateway.option_dict.get("D_ADDR_TON")) d_addr_npi = parse_int(self.smsgateway.option_dict.get("D_ADDR_NPI")) if not smsc_host: log.warning("Can not submit message. SMSC_HOST is missing.") raise SMSError(-1, "No SMSC_HOST specified in the provider config.") if not smsc_port: log.warning("Can not submit message. SMSC_PORT is missing.") raise SMSError(-1, "No SMSC_PORT specified in the provider config.") # Initialize the SMPP Client client = None error_message = None try: client = smpplib.client.Client(smsc_host, smsc_port) client.connect() r = client.bind_transmitter(system_id=sys_id, password=passwd) log.debug("bind_transmitter returns {0!r}".format(r)) r = client.send_message(source_addr_ton=s_addr_ton, source_addr_npi=s_addr_npi, source_addr=s_addr, dest_addr_ton=d_addr_ton, dest_addr_npi=d_addr_npi, destination_addr=phone, short_message=message) log.debug("send_message returns {0!r}".format(r)) except Exception as err: error_message = "{0!r}".format(err) log.warning("Failed to send message: {0!r}".format(error_message)) log.debug("{0!s}".format(traceback.format_exc())) finally: if client: client.disconnect() if error_message: raise SMSError( error_message, "SMS could not be " "sent: {0!r}".format(error_message)) return True
def submit_message(self, phone, message): """ send a message to a phone via an smpp protocol to smsc :param phone: the phone number :param message: the message to submit to the phone :return: """ if not import_successful: # pragma: no cover log.error("smpplib can not be found!") raise SMSError(404, "smpplib can not be found!") log.debug("submitting message {0!s} to {1!s}".format(message, phone)) if not self.smsgateway: # this should not happen. We now always use sms gateway definitions. log.warning("Missing smsgateway definition!") raise SMSError(-1, "Missing smsgateway definition!") smsc_host = self.smsgateway.option_dict.get("SMSC_HOST") smsc_port = self.smsgateway.option_dict.get("SMSC_PORT") sys_id = self.smsgateway.option_dict.get("SYSTEM_ID") passwd = self.smsgateway.option_dict.get("PASSWORD") s_addr_ton = parse_int(self.smsgateway.option_dict.get("S_ADDR_TON")) s_addr_npi = parse_int(self.smsgateway.option_dict.get("S_ADDR_NPI")) s_addr = self.smsgateway.option_dict.get("S_ADDR") d_addr_ton = parse_int(self.smsgateway.option_dict.get("D_ADDR_TON")) d_addr_npi = parse_int(self.smsgateway.option_dict.get("D_ADDR_NPI")) if not smsc_host: log.warning("Can not submit message. SMSC_HOST is missing.") raise SMSError(-1, "No SMSC_HOST specified in the provider config.") if not smsc_port: log.warning("Can not submit message. SMSC_PORT is missing.") raise SMSError(-1, "No SMSC_PORT specified in the provider config.") # Initialize the SMPP Client client = None error_message = None try: client = smpplib.client.Client(smsc_host, smsc_port) client.connect() r = client.bind_transmitter(system_id=sys_id, password=passwd) log.debug("bind_transmitter returns {0!r}".format(r)) r = client.send_message(source_addr_ton=s_addr_ton, source_addr_npi=s_addr_npi, source_addr=s_addr, dest_addr_ton=d_addr_ton, dest_addr_npi=d_addr_npi, destination_addr=phone, short_message=message) log.debug("send_message returns {0!r}".format(r)) except Exception as err: error_message = "{0!r}".format(err) log.warning("Failed to send message: {0!r}".format(error_message)) log.debug("{0!s}".format(traceback.format_exc())) finally: if client: client.disconnect() if error_message: raise SMSError(error_message, "SMS could not be " "sent: {0!r}".format(error_message)) return True