Git Product home page Git Product logo

smspdudecoder's Introduction

SMS-PDU Decoder

PyPI version

This library will help you to decode raw SMS data you can get from a GSM modem (generally by using AT commands).

It has some encoding primitives as well, but full encoding support is not available, since this is not the main purpose of this library.

It is recommended to read the GSM 03.40 specification to better understand the components this library works wtih.

How to install

This library has been successfully tested and works with Python versions ranging from 3.7 up to 3.10.

pip install smspdudecoder

How to use

Please take a look at the source code, which comes with documentation and examples.

For instance, if you need to work with text, you can use GSM and UCS2 encodings just like that:

>>> from smspdudecoder.codecs import GSM, UCS2
>>> GSM.decode('C8F71D14969741F977FD07')
'How are you?'
>>> UCS2.decode('004C006F00720065006D00200049007000730075006D')
'Lorem Ipsum'

If you need to decode a full incoming SMS PDU, you can use the SMSDeliver class:

from io import StringIO
from smspdudecoder.fields import SMSDeliver

deliver_pdu = StringIO('07916407058099F9040B916407950303F100008921222140140004D4E2940A')
sms_data = SMSDeliver.decode(deliver_pdu)

If you execute this example, the sms_data variable will contain a dictionary with the following data:

{
  'smsc': {
    'length': 7,
    'toa': {
      'ton': 'international', 'npi': 'isdn'
    },
    'number': '46705008999'
  },
  'header': {
    'rp': False,
    'udhi': False,
    'sri': False,
    'lp': False,
    'mms': True,
    'mti': 'deliver'
  },
  'sender': {
    'length': 11,
    'toa': { 'ton': 'international', 'npi': 'isdn' },
    'number': '46705930301'
  },
  'pid': 0,
  'dcs': { 'encoding': 'gsm' },
  'scts': datetime.datetime(2098, 12, 22, 12, 4, 41, tzinfo = datetime.timezone.utc),
  'user_data': { 'header': None, 'data': 'TEST' }
}

If you don't need all the technical details, you can use the easy module to get a simple representation of the SMS:

from smspdudecoder.easy import read_incoming_sms

sms = read_incoming_sms('07916407058099F9040B916407950303F100008921222140140004D4E2940A')

Which will produce the following result:

{
  'sender': '+46705930301',
  'content': 'TEST',
  'date': datetime.datetime(2098, 12, 22, 12, 4, 41, tzinfo=datetime.timezone.utc),
  'partial': False
}

How to test and contribute

First, clone this repository:

git clone [email protected]:qotto/smspdudecoder.git
# or use HTTPS if you are unauthenticated:
# git clone https://github.com/qotto/smspdudecoder.git
cd smspdudecoder

Using Docker

The easiest way to test this library against all supported Python versions is to use Docker.

make docker-test

Behinds the scenes, this will build a Docker image with all supported Python versions and run the tests with tox.

Using your existing Python installation

If you want to simply run the test suite, make sure that you have dependencies installed with:

pip install -r requirements.txt

And use the following command:

make test

smspdudecoder's People

Contributors

alexpirine avatar asmox avatar comic31 avatar marsoft avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

smspdudecoder's Issues

GSM.decode strange result

Hi,
I've tried with python3.10 but you can see that Out[4 ] is pretty strange as result, any idea on how to fix this?

In [1]: from smspdudecoder.codecs import GSM, UCS2


In [2]: GSM.decode(GSM.encode('1234567', with_padding=True), strip_padding=True)
Out[2]: '1234567'


In [4]: GSM.decode('07919333851805420406D0D4641300002220720171424087C7B29B9E6697416376BAECA69759206AB2099A068D45D0B52804D1E561D00D744EBFE5EE34081
   ...: E9ECFCBF23F280C8287CFE176D94D7F83C66F37A8EE068DC3EEB7BB0C22A741F3373B', strip_padding=True)
Out[4]: 'ì"NÆSΔF$BòΣ@MΞYÇ@@ò£".¡8B@æ<,v&Of.ùΞfN.v&/f$"MlèΞ\r4,è:-Λè"Ψåù:¥:NüΠwNù$åßÉ/Πàì\nù$å>Ån.v&àùΞüvù*wùΞÅvüv.ù"NùΞàf'

I've tried out also python-messaging that output an expected readable result of Out[4]:

In [1]: from messaging.sms import  SmsDeliver

In [2]: pdutext='07919333851805420406D0D4641300002220720171424087C7B29B9E6697416376BAECA69759206AB2099A068D45D0B52804D1E561D00D744EBFE5EE34081E9ECFCBF23F280C8287CFE176D
   ...: 94D7F83C66F37A8EE068DC3EEB7BB0C22A741F3373B'

In [3]: x=SmsDeliver(pdutext)

In [4]: x.text
Out[4]: 'Gentile cliente, TIM SAFE WEB tra 7 giorni passerà a pagamento con un canone di sol'

Source text doesn't equal text after encoding and decoding

Hi,

I've found a situation when after encoding and decoding text is not equal to source text. Example

text = 'ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà'
print(codec.decode(codec.encode(text)) == text)
False
print(codec.decode(codec.encode(text)))
ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà@

we can see that after encoding/decoding an extra symbol "@" is added.

tox error

Hi,
Trying to run tests on a freshly cloned smspdu on python-3.6, I'm getting this error:

py36 runtests: PYTHONHASHSEED='807310402'
py36 runtests: commands[0] | pip install -r requirements.txt
Requirement already satisfied: bitstring==3.1.5 in ./.tox/py36/lib/python3.6/site-packages (from -r requirements.txt (line 1)) (3.1.5)
Requirement already satisfied: pytz==2017.3 in ./.tox/py36/lib/python3.6/site-packages (from -r requirements.txt (line 2)) (2017.3)
Requirement already satisfied: six==1.11.0 in ./.tox/py36/lib/python3.6/site-packages (from -r requirements.txt (line 3)) (1.11.0)
py36 runtests: commands[1] | mypy smspdu
smspdu/elements.py:205: error: Argument 1 to "get" of "Mapping" has incompatible type "Optional[str]"; expected "str"
smspdu/elements.py:206: error: Argument 1 to "get" of "Mapping" has incompatible type "Optional[str]"; expected "str"
smspdu/fields.py:195: error: Value of type "Optional[Dict[Any, Any]]" is not indexable
smspdu/fields.py:199: error: Value of type "Optional[Dict[Any, Any]]" is not indexable
smspdu/fields.py:201: error: Value of type "Optional[Dict[Any, Any]]" is not indexable
smspdu/fields.py:208: error: Value of type "Optional[Dict[Any, Any]]" is not indexable
ERROR: InvocationError: 'xxxxxx/smspdu/.tox/py36/bin/mypy smspdu'
_____________________________________ summary _____________________________________
ERROR: py36: commands failed

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.