From e43d1abe239cd7e0db0c7d284152e3f0f1f16107 Mon Sep 17 00:00:00 2001 From: JAVIER DEL EGIDO SIERRA Date: Wed, 7 Feb 2024 16:34:55 +0100 Subject: [PATCH 1/3] Adding ascii_pdu output to send_message and read_once --- smpplib/client.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/smpplib/client.py b/smpplib/client.py index ebad492..72fcd0a 100644 --- a/smpplib/client.py +++ b/smpplib/client.py @@ -162,7 +162,7 @@ def _bind(self, command_name, **kwargs): self.send_pdu(p) try: - resp = self.read_pdu() + resp, ascii_pdu = self.read_pdu() except socket.timeout: raise exceptions.ConnectionError() if resp.is_error(): @@ -252,8 +252,10 @@ def read_pdu(self): raise exceptions.PDUError('Broken PDU') raw_pdu = raw_len + self._recv_exact(length - 4) + ascii_pdu = binascii.b2a_hex(raw_pdu) #Modified self.logger.debug('<<%s (%d bytes)', binascii.b2a_hex(raw_pdu), len(raw_pdu)) + #print("ascii_pdu_library", ascii_pdu) pdu = smpp.parse_pdu( raw_pdu, @@ -264,12 +266,12 @@ def read_pdu(self): self.logger.debug('Read %s PDU', pdu.command) if pdu.is_error(): - return pdu + return pdu#, ascii_pdu #Modified elif pdu.command in consts.STATE_SETTERS: self.state = consts.STATE_SETTERS[pdu.command] - return pdu + return pdu, ascii_pdu #Modified def accept(self, obj): """Accept an object""" @@ -345,7 +347,7 @@ def read_once(self, ignore_error_codes=None, auto_send_enquire_link=True): try: try: - pdu = self.read_pdu() + pdu, ascii_pdu = self.read_pdu() except socket.timeout: if not auto_send_enquire_link: raise @@ -379,6 +381,8 @@ def read_once(self, ignore_error_codes=None, auto_send_enquire_link=True): self.logger.warning('(%d) %s. Ignored.', e.args[1], e.args[0]) else: raise + + return ascii_pdu def poll(self, ignore_error_codes=None, auto_send_enquire_link=True): """Act on available PDUs and return""" @@ -406,7 +410,13 @@ def send_message(self, **kwargs): ssm = smpp.make_pdu('submit_sm', client=self, **kwargs) self.send_pdu(ssm) - return ssm + + try: + resp, ascii_pdu = self.read_pdu() + except socket.timeout: + raise exceptions.ConnectionError() + + return ssm, ascii_pdu def query_message(self, **kwargs): """Query message state From 6db44803a70ae2541400a91c64f4440136c176c6 Mon Sep 17 00:00:00 2001 From: JAVIER DEL EGIDO SIERRA Date: Thu, 8 Feb 2024 14:42:44 +0100 Subject: [PATCH 2/3] Modified creating specific functions for rawpdu --- smpplib/client.py | 70 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 60 insertions(+), 10 deletions(-) diff --git a/smpplib/client.py b/smpplib/client.py index 72fcd0a..c2f77ad 100644 --- a/smpplib/client.py +++ b/smpplib/client.py @@ -162,7 +162,7 @@ def _bind(self, command_name, **kwargs): self.send_pdu(p) try: - resp, ascii_pdu = self.read_pdu() + resp = self.read_pdu() except socket.timeout: raise exceptions.ConnectionError() if resp.is_error(): @@ -237,7 +237,7 @@ def _recv_exact(self, exact_size): received += len(part) parts.append(part) return b"".join(parts) - + def read_pdu(self): """Read PDU from the SMSC""" @@ -252,10 +252,8 @@ def read_pdu(self): raise exceptions.PDUError('Broken PDU') raw_pdu = raw_len + self._recv_exact(length - 4) - ascii_pdu = binascii.b2a_hex(raw_pdu) #Modified self.logger.debug('<<%s (%d bytes)', binascii.b2a_hex(raw_pdu), len(raw_pdu)) - #print("ascii_pdu_library", ascii_pdu) pdu = smpp.parse_pdu( raw_pdu, @@ -266,12 +264,28 @@ def read_pdu(self): self.logger.debug('Read %s PDU', pdu.command) if pdu.is_error(): - return pdu#, ascii_pdu #Modified + return pdu elif pdu.command in consts.STATE_SETTERS: self.state = consts.STATE_SETTERS[pdu.command] - return pdu, ascii_pdu #Modified + return pdu + + def read_rawpdu(self): + """Read PDU from the SMSC""" + + raw_len = self._recv_exact(4) + + try: + length = struct.unpack('>L', raw_len)[0] + except struct.error: + self.logger.warning('Receive broken pdu... %s', repr(raw_len)) + raise exceptions.PDUError('Broken PDU') + + raw_pdu = raw_len + self._recv_exact(length - 4) + raw_pdu = binascii.b2a_hex(raw_pdu) + + return raw_pdu def accept(self, obj): """Accept an object""" @@ -334,7 +348,29 @@ def error_pdu_handler(self, pdu): consts.DESCRIPTIONS.get(pdu.status, 'Unknown status')), int(pdu.status), ) + + def read_once_rawpdu(self, ignore_error_codes=None, auto_send_enquire_link=True): + + if ignore_error_codes is not None: + warnings.warn( + "ignore_error_codes is deprecated, use set_error_pdu_handler to " + "configure a custom error PDU handler instead.", + DeprecationWarning, + ) + + try: + ascii_pdu = self.read_rawpdu() + except socket.timeout: + if not auto_send_enquire_link: + raise + self.logger.debug('Socket timeout, listening again') + pdu = smpp.make_pdu('enquire_link', client=self) + self.send_pdu(pdu) + return + + return ascii_pdu + def read_once(self, ignore_error_codes=None, auto_send_enquire_link=True): """Read a PDU and act""" @@ -347,7 +383,7 @@ def read_once(self, ignore_error_codes=None, auto_send_enquire_link=True): try: try: - pdu, ascii_pdu = self.read_pdu() + pdu = self.read_pdu() except socket.timeout: if not auto_send_enquire_link: raise @@ -381,8 +417,6 @@ def read_once(self, ignore_error_codes=None, auto_send_enquire_link=True): self.logger.warning('(%d) %s. Ignored.', e.args[1], e.args[0]) else: raise - - return ascii_pdu def poll(self, ignore_error_codes=None, auto_send_enquire_link=True): """Act on available PDUs and return""" @@ -410,9 +444,25 @@ def send_message(self, **kwargs): ssm = smpp.make_pdu('submit_sm', client=self, **kwargs) self.send_pdu(ssm) + + return ssm + + def send_message_rawpdu(self, **kwargs): + """Send message + + Required Arguments: + source_addr_ton -- Source address TON + source_addr -- Source address (string) + dest_addr_ton -- Destination address TON + destination_addr -- Destination address (string) + short_message -- Message text (string) + """ + ssm = smpp.make_pdu('submit_sm', client=self, **kwargs) + self.send_pdu(ssm) + try: - resp, ascii_pdu = self.read_pdu() + ascii_pdu = self.read_rawpdu() except socket.timeout: raise exceptions.ConnectionError() From 557a73f8b39ca0a5d112bd7695356d2747e03da0 Mon Sep 17 00:00:00 2001 From: JAVIER DEL EGIDO SIERRA Date: Fri, 9 Feb 2024 12:38:43 +0100 Subject: [PATCH 3/3] Update --- smpplib/client.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/smpplib/client.py b/smpplib/client.py index c2f77ad..21cfe1b 100644 --- a/smpplib/client.py +++ b/smpplib/client.py @@ -192,11 +192,13 @@ def unbind(self): p = smpp.make_pdu('unbind', client=self) self.send_pdu(p) + ''' try: return self.read_pdu() except socket.timeout: raise exceptions.ConnectionError() - + ''' + def send_pdu(self, p): """Send PDU to the SMSC"""