smpplib
raw JSON → 2.2.4 verified Sat May 09 auth: no python
SMPP library for Python implementing the Short Message Peer-to-Peer (SMPP) protocol for sending/receiving SMS via SMSCs. Current version: 2.2.4. Release cadence: irregular, with updates for bug fixes and features.
pip install smpplib Common errors
error AttributeError: 'Client' object has no attribute 'send_pdu' ↓
cause Using an outdated or wrong import; the correct method is 'send_pdu' on the client instance, but the error may occur if the client is not properly connected or if an older version lacks it.
fix
Ensure you are using smpplib >=2.0 and that you have called 'client.connect()' before sending.
error TypeError: __init__() got an unexpected keyword argument 'source_addr_ton' ↓
cause You are passing constructor arguments to SubmitSM incorrectly. SubmitSM is a class, not a function; you should instantiate it with keyword arguments.
fix
Use 'pdu = smpplib.pdu.SubmitSM(source_addr_ton=...)'
error ConnectionRefusedError: [Errno 111] Connection refused ↓
cause The SMSC host or port is incorrect, or the SMSC is not reachable.
fix
Double-check the hostname and port. Ensure firewall allows outbound connection to port 2775.
Warnings
breaking In version 2.0, the library dropped support for Python 2 and older 3.x versions. The context manager interface was added, and __del__ was moved to __exit__. ↓
fix Use context managers (with statement) for connection handling.
gotcha If you send a message with UCS-2 (data_coding=8), you must ensure the message content length is calculated correctly including NULL terminator, or the message may be split incorrectly. ↓
fix Explicitly handle UCS-2 encoding; consider setting data_coding to 0 (default) or 8 for Unicode, and test with your SMSC.
gotcha The socket receive error may be silently ignored in versions <=2.2.2. This can cause connection hangs. ↓
fix Upgrade to >=2.2.3 or patch the client to log/raise socket errors.
deprecated The library does not support Python 3.11+ fully? No deprecation warnings for Python version, but some features may be untested. ↓
fix Test your application; consider submitting issues if problems arise.
Imports
- Client
from smpplib import client - consts
from smpplib import consts - pdu
from smpplib import pdu
Quickstart
import smpplib.client
import smpplib.consts
import logging
logging.basicConfig(level=logging.DEBUG)
client = smpplib.client.Client('smpp.example.com', 2775)
client.connect()
client.bind_transceiver(system_id='user', password='pass')
# Send a short message
import os
try:
smpplib.pdu.SubmitSM(
source_addr_ton=consts.SMPP_TON_INTL,
source_addr_npi=consts.SMPP_NPI_ISDN,
source_addr='1234',
dest_addr_ton=consts.SMPP_TON_INTL,
dest_addr_npi=consts.SMPP_NPI_ISDN,
destination_addr='447700900000',
short_message='Hello, World!',
data_coding=consts.SMPP_DATA_CODING_DEFAULT,
)
except Exception as e:
print(f"Error: {e}")
client.unbind()
client.disconnect()