0

I'm receiving an encoded string from jasmis-sms, the original string is áéíóú ñaña!, jasmin does some encoding and gives me:

����� �a�a! (in my terminal)

which chardet detects as charset: windows-1252 But when trying to decode that with

Message.decode('windows-1252')

But that returns

<type 'exceptions.UnicodeEncodeError'>: 'ascii' codec can't encode character u'\xe1' in position 11: ordinal not in range(128)

String is coming from jasmin-sms on an interception python script

The script is launched by jasmin-sms on receiving an SMS via smpp, it injects a global variable routable, which contains all the SMS's data, the complete code is:

python2

import urllib
import urllib2
import re
import chardet

file=open('/opt/jasmin-scritps/interception/mo-interceptor.log','a')
file.write('===============================================================================\n')
file.write('Start logging...\n')

SMS_to = routable.pdu.params['destination_addr']
SMS_from = routable.pdu.params['source_addr']

SMS_message = routable.pdu.params['short_message']

file.write('To: [%s]\n' % SMS_to )
file.write('From: [%s]\n' % SMS_from )
file.write('ShortMessage: [%s]\n' % SMS_message.encode("hex") )
file.write('data-coding: [%s]\n' % routable.pdu.params['data_coding'] )
file.write('charset: %s\n' % chardet.detect( SMS_message )['encoding'] )
file.write('decoded: [%s]\n' % SMS_message )
file.write('SmppSatus: [%s]\n' % smpp_status )

file.write('Content: [%s]\n' % routable.pdu.params['short_message'] )

And I'm not sure how to solve this problem.

And help is really appreciated!

David Villasmil
  • 395
  • 2
  • 19
  • Can you please post the complete line, that is failing and also show line that opens the jasmin file and also specify the python version you're using. – gelonida Oct 06 '19 at 21:25
  • OK you know probably that python2 will be end of life (availability of fixes / updates / security fixes) by January 2020. Handling of unicode differs between python2 and python3. If you can I suggest to use python3 if you can't I suggest to look whether you can pip install future, add the line `from io import open` as first line, change `file=open('/opt/jasmin-scritps/interception/mo-interceptor.log','a')` to `file=open('/opt/jasmin-scritps/interception/mo-interceptor.log','a', encoding='utf-8') – gelonida Oct 06 '19 at 21:47
  • need a second comment. (not enough characters left) then replace `file.write('decoded: [%s]\n' % SMS_message )` with `file.write('decoded: [%s]\n' % SMS_message.decode("cp1522'))` if you can't use the future library and you want to write code, that might not work with python3, then just try *as only change !!!) `file.write('decoded: [%s]\n' % SMS_message.decode('cp1252').encode('utf-8') )` – gelonida Oct 06 '19 at 21:49
  • Thanks @gelonida Thanks! that worked PERFECTLY! – David Villasmil Oct 06 '19 at 22:22
  • Just out of curiosity: what of my three suggestions did you follow? python3? python2 with future? or python2 only? – gelonida Oct 06 '19 at 22:24
  • A question to the SO experts. If a comment resolves an issue. Should it be copied to be a solution? Does this question have an added value to be searchable with an answer? Or would it be better to link this question to a similar encoding / decoding question? Consider also, that this is python2 and such questions might perhaps be of less and less interest, though some people might still be stuck with python2 for a few years despite it's end of life – gelonida Oct 06 '19 at 22:29
  • I think you should copy the comment as a answer and possibly ellaborate a bit. – lofihelsinki Oct 07 '19 at 12:50

0 Answers0