I have been able to send the SMS to a phone number using the following code
import smpplib
import settings
import sys
client = smpplib.client.Client(settings.SMS_SYSTEM_HOSTNAME, settings.SMS_SYSTEM_PORT)
# 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()
client.bind_transceiver(system_id=settings.SMS_SYSTEM_ID, password=settings.SMS_SYSTEM_PASSWORD)
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',
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='90474xxxxx',
short_message=b'Test message')
print(pdu.sequence)
client.listen()
But the main purpose of the project is sending to a lot of numbers (at least a million) at once. How can I go about that?
The obvious solution might be to put
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',
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='904xxxxxxxx',
short_message=b'Test message')
in a loop but I doubt the efficiency of that. Is there a better, more efficient way to send bulk SMSs using Python and SMPP?
You can use a message-broker and a distributed task-queue (for example celery and rabbitmq ).
With these, you can have asynchronous tasks. Where each task is an independent piece of code, and where one task would send an SMS. So instead of sending all your SMS in a loop, you will instead create tasks in the message-broker's queue. And the distributed task-queue service will execute them.
PS : if you use a loop, I advise you to add a try/except
. So if a send_message
failed, you won't be forced to start again.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.