Client

class naz.client.Client(smsc_host, smsc_port, system_id, password, broker, system_type='', addr_ton=0, addr_npi=0, address_range='', interface_version=52, client_id=None, enquire_link_interval=55.0, logger=None, rate_limiter=None, hook=None, sequence_generator=None, throttle_handler=None, correlation_handler=None, drain_duration=8.0, socket_timeout=30.0, custom_codecs=None)[source]

Bases: object

The SMPP client that will interact with SMSC/server.

Example declaration:

import os
import asyncio
import naz

broker = naz.broker.SimpleBroker(maxsize=1000)
client = naz.Client(
        smsc_host="127.0.0.1",
        smsc_port=2775,
        system_id="smppclient1",
        password=os.getenv("password", "password"),
        broker=broker,
    )

# 1. connect to the SMSC host
# 2. bind to the SMSC host
# 3. send any queued messages to SMSC
# 4. read any data from SMSC
# 5. continually check the state of the SMSC
tasks = asyncio.gather(
    client.connect(),
    client.tranceiver_bind(),
    client.dequeue_messages(),
    client.receive_data(),
    client.enquire_link(),
)
loop = asyncio.get_event_loop()
loop.run_until_complete(tasks)
__init__(smsc_host, smsc_port, system_id, password, broker, system_type='', addr_ton=0, addr_npi=0, address_range='', interface_version=52, client_id=None, enquire_link_interval=55.0, logger=None, rate_limiter=None, hook=None, sequence_generator=None, throttle_handler=None, correlation_handler=None, drain_duration=8.0, socket_timeout=30.0, custom_codecs=None)[source]
Parameters
  • smsc_host (str) – the IP address(or domain name) of the SMSC gateway/server

  • smsc_port (int) – the port at which SMSC is listening on

  • system_id (str) – Identifies the ESME system requesting to bind as a transceiver with the SMSC.

  • password (str) – The password to be used by the SMSC to authenticate the ESME requesting to bind.

  • broker (BaseBroker) – python class instance implementing some queueing mechanism. messages to be sent to SMSC are queued using the said mechanism before been sent

  • client_id (Union[None, str]) – a unique string identifying a naz client class instance

  • system_type (str) – Identifies the type of ESME system requesting to bind with the SMSC.

  • addr_ton (int) – Type of Number of the ESME address.

  • addr_npi (int) – Numbering Plan Indicator (NPI) for ESME address(es) served via this SMPP transceiver session

  • address_range (str) – A single ESME address or a range of ESME addresses served via this SMPP transceiver session.

  • interface_version (int) – Indicates the version of the SMPP protocol supported by the ESME.

  • enquire_link_interval (float) – time in seconds to wait before sending an enquire_link request to SMSC to check on its status

  • logger (Union[None, Logger]) – python logger instance to be used for logging

  • rate_limiter (Union[None, BaseRateLimiter]) – python class instance implementing rate limitation

  • hook (Union[None, BaseHook]) – python class instance implemeting functionality/hooks to be called by naz just before sending request to SMSC and just after getting response from SMSC

  • sequence_generator (Union[None, BaseSequenceGenerator]) – python class instance used to generate sequence_numbers

  • throttle_handler (Union[None, BaseThrottleHandler]) – python class instance implementing functionality of what todo when naz starts getting throttled responses from SMSC

  • correlation_handler (Union[None, BaseCorrelater]) – A python class instance that naz uses to store relations between SMPP sequence numbers and user applications’ log_id’s and/or hook_metadata.

  • drain_duration (float) – duration in seconds that naz will wait for after receiving a termination signal.

  • socket_timeout (float) – duration that naz will wait, for socket/connection related activities with SMSC, before timing out

  • custom_codecs (Union[None, Dict[str, CodecInfo]]) – a dictionary of encodings and their corresponding codecs.CodecInfo that you would like to register.

Raises

NazClientError – raised if there’s an error instantiating a naz Client.

Return type

None

async command_handlers(pdu, body_data, smpp_command, command_status_value, sequence_number, log_id, hook_metadata)[source]

This routes the various different SMPP PDU to their respective handlers.

Parameters
  • pdu (bytes) – the full PDU as received from SMSC

  • body_data (bytes) – PDU body as received from SMSC

  • smpp_command (str) – type of PDU been received. eg bind_transceiver_resp

  • command_status_value (int) – the response code from SMSC for a specific PDU

  • sequence_number (int) – SMPP sequence_number

  • log_id (str) – a unique identify of this request

  • hook_metadata (str) – additional metadata that you would like to be passed on to hooks

Return type

None

async connect(log_id='')[source]

make a network connection to SMSC server.

Return type

None

async deliver_sm_resp(sequence_number)[source]

send a DELIVER_SM_RESP pdu to SMSC.

Parameters

sequence_number (int) – SMPP sequence_number

Return type

None

async dequeue_messages(TESTING=False)[source]

In a loop; dequeues items from the broker and sends them to SMSC.

Parameters

TESTING (bool) – indicates whether this method is been called while running tests.

Return type

Union[Message, Dict[Any, Any]]

send an ENQUIRE_LINK pdu to SMSC.

Parameters

TESTING (bool) – indicates whether this method is been called while running tests.

Return type

Union[None, bytes]

send an ENQUIRE_LINK_RESP pdu to SMSC.

Parameters

sequence_number (int) – SMPP sequence_number

Return type

None

async re_establish_conn_bind(smpp_command, log_id, TESTING=False)[source]

Called if connection is lost. It reconnects & rebinds to SMSC.

Parameters

TESTING (bool) – indicates whether this method is been called while running tests.

Return type

None

async receive_data(TESTING=False)[source]

In a loop; read bytes from the network connected to SMSC and hand them over to the _parse_response_pdu method for parsing.

Parameters

TESTING (bool) – indicates whether this method is been called while running tests.

Return type

Union[None, bytes]

async send_data(smpp_command, msg, log_id, hook_metadata='')[source]

Sends PDU’s to SMSC over a network connection. This method does not block; it buffers the data and arranges for it to be sent out asynchronously. It also accts as a flow control method that interacts with the IO write buffer.

Parameters
  • smpp_command (str) – type of PDU been sent. eg bind_transceiver

  • msg (bytes) – PDU to be sent to SMSC over the network connection.

  • log_id (str) – a unique identify of this request

  • hook_metadata (str) – additional metadata that you would like to be passed on to hooks

Return type

None

async send_message(proto_msg)[source]

Sends a message/SUBMIT_SM to SMSC. That message will get enqueued to broker and later on sent to SMSC.

Parameters

proto_msg (SubmitSM) – the message to send to SMSC. Has to be a class instance of naz.protocol.SubmitSM

Usage:

import naz

broker = naz.broker.SimpleBroker(maxsize=1000)
client = naz.Client(
        smsc_host="127.0.0.1",
        smsc_port=2775,
        system_id="smppclient1",
        password=os.getenv("password", "password"),
        broker=broker,
    )
msg = naz.protocol.SubmitSM(
    short_message="hello world",
    source_addr="255700111222",
    destination_addr="255799000888",
    log_id="some-id",
)
await client.send_message(msg)
Return type

None

async shutdown()[source]

Cleanly shutdown this client.

Return type

None

async tranceiver_bind(log_id='')[source]

send a BIND_TRANSCEIVER pdu to SMSC.

Return type

None

async unbind()[source]

send an UNBIND pdu to SMSC.

Return type

None

async unbind_resp(sequence_number)[source]

send an UNBIND_RESP pdu to SMSC.

Parameters

sequence_number (int) – SMPP sequence_number

Return type

None

exception naz.client.NazClientError[source]

Bases: Exception

Error raised when there’s an error instantiating a naz Client.