0

I'm a newbie in SMPP but I need to simulate traffic over the SMPP protocol. I have found the tutorial how to send SMS using smpp lib from Python How to Send SMS using SMPP Protocol

I'm trying to write a receiver,but I am unable to get it to work. Please help. My code is:

 import smpplib


class ClientCl():
    client=None
    def receive_SMS(self):
        client=smpplib.client.Client('localhost',1000)

        try:
            client.connect()          
            client.bind_receiver("sysID","login","password")
            sms=client.get_message()
            print(sms)

        except : 
            print("boom! nothing works")           
            pass


sms_getter=ClientCl.receive_SMS

1 Answer 1

2

From what I can understand the smpplib you are using is the one available at github. Looking at your code and the client code, I can't find the function client.get_message. Perhaps you have an older version of the library? Or I have the wrong library. In any case, it is likely that the get_message function does not block and wait for the message to arrive.

Looking at the client code it seems that you have two options:

  • Poll the library until you get a valid message
  • Setup the library to listen to the SMPP port and call a function once a message arrives.

If you look at the README.md file it shows how you can setup the library to implement the second option (which is the better option).

...
client = smpplib.client.Client('example.com', SOMEPORTNUMBER)

# 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='login', password='secret')

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='SENDERPHONENUM',

    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,
)
print(pdu.sequence)
client.listen()
...

When receiving a message or delivery receipt the function defined in client.set_message_received_handler() will be called. In the example, it is a lambda function. There is also an example on how to set up for listening in a thread.

If you prefer the simpler polling option you should use the poll function. For the simplest implementation all you need to do is:

while True:
  client.Poll()

As before, the function set in client.set_message_received_handler() will be called once a message arrives.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.