def send(self, recipient, title, message): """ Send notification to recipient :param str recipient: phone number :param str title: title for message (including in SMS) :param str message: short text message """ # Replace bad symbols in phone for char in '+()- ': recipient = recipient.replace(char, '') # Convert to unicode if type(title) != str: title = title.decode('utf-8') if type(message) != str: message = message.decode('utf-8') client = None if title: text = '%s:\n%s' % (title, message) else: text = message parts, encoding_flag, msg_type_flag = smpplib.gsm.make_parts(text) try: client = smpplib.client.Client(self.host, self.port) client.connect() if self.auth_pair: client.bind_transceiver( system_id=self.login, password=self.passwd) for part in parts: client.send_message( source_addr_ton=smpplib.consts.SMPP_TON_ALNUM, source_addr_npi=smpplib.consts.SMPP_NPI_UNK, source_addr=self.source, dest_addr_ton=smpplib.consts.SMPP_TON_INTL, dest_addr_npi=smpplib.consts.SMPP_NPI_ISDN, destination_addr=recipient, short_message=part, data_coding=encoding_flag, esm_class=msg_type_flag ) except Exception as e: raise SendSMSException(e) finally: if client: try: client.unbind() except: pass client.disconnect() return False
def send_sms(*args): try: # if you want to know what's happening logging.basicConfig(filename='sms_smpp.log', filemode='w', level='DEBUG') # Two parts, UCS2, SMS with UDH parts, encoding_flag, msg_type_flag = smpplib.gsm.make_parts(MESSAGE.get()) client = smpplib.client.Client(SMSC_HOST.get(), int(SMSC_PORT.get())) # Print when obtain message_id client.set_message_sent_handler( lambda pdu: sys.stdout.write('sent {} {}\n'.format(pdu.sequence, pdu.message_id)) ) client.set_message_received_handler( lambda pdu: sys.stdout.write('delivered {}\n'.format(pdu.receipted_message_id)) ) client.connect() if USER_TYPE.get() == "bind_transceiver": client.bind_transceiver(system_id=SYSTEM_ID.get(), password=SYSTEM_PASS.get()) if USER_TYPE.get() == "bind_transmitter": client.bind_transmitter(system_id=SYSTEM_ID.get(), password=SYSTEM_PASS.get()) for part in parts: pdu = client.send_message( source_addr_ton=smpplib.consts.SMPP_TON_INTL, #source_addr_npi=smpplib.consts.SMPP_NPI_ISDN, # Make sure it is a byte string, not unicode: source_addr=SENDER_ID.get(), dest_addr_ton=smpplib.consts.SMPP_TON_INTL, #dest_addr_npi=smpplib.consts.SMPP_NPI_ISDN, # Make sure thease two params are byte strings, not unicode: destination_addr=DESTINATION_NO.get(), short_message=part, data_coding=encoding_flag, esm_class=msg_type_flag, registered_delivery=True, ) print(pdu.sequence) client.unbind() print('Unbind Done') client.disconnect() print('Disconnected') print('Sms sent on: ' + str(datetime.datetime.now())) except ValueError: pass
def stop(self): print("stop fun") sql = "UPDATE smpptrafficmonitor SET ackRecv = %s,status =%s where timeStamp= %s" val = (int(data["ackRecv"]), "1", str(data["timeStamp"])) workerClass().mycursor.execute(sql, val) workerClass().database.commit() try: client.unbind() client.disconnect() except Exception as e: print(e) print("Unbind exception occured") print("Send unbind")
def smpp_bind(client): while True: try: smpplib.client.logger.setLevel('INFO') client.set_message_received_handler(rx_deliver_sm) client.set_message_sent_handler(post_tx_message) #client.set_test_handler(my_test) #client.disconnect() client.connect() # Bind to OSMPP, out configured default-route in nitb. client.bind_transceiver(system_id="OSMPP", password="******") #client.test_handler(client, foo="bar") log.info('Listening....') client.listen([11]) except smpplib.exceptions.ConnectionError as e: print ("Connection Error (%s)" % e) client.disconnect() time.sleep(1)
def smpp_bind(client): while True: try: smpplib.client.logger.setLevel('INFO') client.set_message_received_handler(rx_deliver_sm) client.set_message_sent_handler(post_tx_message) #client.set_test_handler(my_test) #client.disconnect() client.connect() # Bind to OSMPP, out configured default-route in nitb. client.bind_transceiver(system_id="OSMPP", password="******") #client.test_handler(client, foo="bar") log.info('Listening....') client.listen([11]) except smpplib.exceptions.ConnectionError as e: print("Connection Error (%s)" % e) client.disconnect() time.sleep(1)
def sendMessage(myServer, myPort, system_id, password, phoneNumber, text): # if you want to know what's happening # logging.basicConfig(level='DEBUG') # Two parts, UCS2, SMS with UDH parts, encoding_flag, msg_type_flag = smpplib.gsm.make_parts(text) try: client = smpplib.client.Client(myServer, myPort) # Print when obtain message_id client.set_message_sent_handler(lambda pdu: logging.info( 'sent {} {}\n'.format(pdu.sequence, pdu.message_id))) client.set_message_received_handler(lambda pdu: logging.info( 'delivered {}\n'.format(pdu.receipted_message_id))) client.connect() client.bind_transceiver(system_id=system_id, password=password) for part in parts: pdu = client.send_message( source_addr_ton=smpplib.consts.SMPP_TON_INTL, #source_addr_npi=smpplib.consts.SMPP_NPI_ISDN, # Make sure it is a byte string, not unicode: source_addr='', dest_addr_ton=smpplib.consts.SMPP_TON_INTL, #dest_addr_npi=smpplib.consts.SMPP_NPI_ISDN, # Make sure thease two params are byte strings, not unicode: destination_addr=phoneNumber, short_message=part, data_coding=encoding_flag, esm_class=msg_type_flag, registered_delivery=True, ) time.sleep(1) client.disconnect() # client.listen() # t = threading.Thread(target=listen, args=(client,)) # t.start() except Exception as e: logging.info(str(e))
client = None try: client = smpplib.client.Client(settings.SMS_SYSTEM_HOSTNAME, settings.SMS_SYSTEM_PORT) client.connect() try: client.bind_transmitter(system_id=settings.SMS_SYSTEM_ID, password=settings.SMS_SYSTEM_PASSWORD) client.send_message(source_addr_ton=smpplib.consts.SMPP_TON_INTL, source_addr='9535134654', dest_addr_ton=smpplib.consts.SMPP_TON_INTL, destination_addr='9535134654', short_message='hola sms'.encode()) finally: #print "==client.state====", client.state if client.state in [smpplib.consts.SMPP_CLIENT_STATE_BOUND_TX]: #if bound to transmitter try: client.unbind() except smpplib.exceptions.UnknownCommandError as ex: #https://github.com/podshumok/python-smpplib/issues/2 try: client.unbind() except smpplib.exceptions.PDUError as ex: pass finally: if client: #print "==client.state====", client.state client.disconnect() #print "==client.state====", client.state
def disconnect_client(): client.unbind() client.disconnect()