Python Forum
flask app decoding problem - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: flask app decoding problem (/thread-34451.html)



flask app decoding problem - mesbah - Aug-01-2021

My Problem: My client is sending UTF-16BE encoded strings to flask app server and I am getting as request.args.get('text') when I try to decode
getting garbage.But from the APP server URL I see the correct encoded value and I get prefect output when I use texturlencodedvalue=unquote_plus(unquote_to_bytes("%09%9F%09%BF%09%95%09%BE%00+%06G%06F%06'").decode('utf-16-be'))
My Actual Data is: টিকা هنا
But from Flask after decoding getting: ৯뾽৯뾽৯뾽৯뾽 هنا

###########My Flask APP LOG############
My utf-16-be-decoded output is: ৯뾽৯뾽৯뾽৯뾽 هنا
Actual Decoded output is: টিকা هنا
Output:
192.168.1.60 - - [02/Aug/2021 02:04:33] "GET /?username=test&password=test&to=8801833055942&text=%09%9F%09%BF%09%95%09%BE%00+%06G%06F%06'&from=TestAPI&coding=2&charset=UTF-16BE&smsc=djangolocal&dlr-url=68c0b864-5029-490f-b92a-62b27ab5b5dc&dlr-mask=19 HTTP/1.1" 0 -
My Environment Details:
########################
Output:
OS: CentOS 8 Locale : 'UTF-8' Python Version : Python 3.6.8 Flask 2.0.1 Werkzeug 2.0.1
################# My Flask Application###########
from flask import Response
import requests
from requests.structures import CaseInsensitiveDict
import vonage
from vonage import Client
from urllib.parse import unquote,quote, unquote_to_bytes,unquote_plus,parse_qs,parse_qsl
import urllib.parse
import sys


app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False
@app.route('/')
def index():
    username=request.args.get('username')
    password=request.args.get('password')
    to=request.args.get('to')
    sender=request.args.get('from')
    text=unquote_plus(unquote_to_bytes(request.args.get('text')).decode('utf-16-be'))
    coding=request.args.get('coding')
    charset=request.args.get('charset')
    binfo=request.args.get('binfo')
    account=request.args.get('account')
    smsc=request.args.get('smsc')
    dlr_url=request.args.get('dlr-url')
    dlr_mask=request.args.get('dlr-mask')
    print("My utf-16-be-decoded output is: " +text)
    texturlencodedvalue=unquote_plus(unquote_to_bytes("%09%9F%09%BF%09%95%09%BE%00+%06G%06F%06'").decode('utf-16-be'))
    print("Actual Decoded output is: "+texturlencodedvalue)
    try:
        if str(to):
                    client = vonage.Client(key="Test", secret="Test")
                    sms = vonage.Sms(client)
                    if coding==0 or coding==0:
                       responseData = sms.send_message({"from": "Vonage APIs","to": to,"text": text,'type': 'unicode',})
                    else:
                       responseData = sms.send_message({"from": "Vonage APIs","to": to,"text": text,'type': 'unicode',})

                    if responseData["messages"][0]["status"] == "0":
                       print("Message sent successfully.")
                       return Response(status="ok")
                    else:
                        print(f"Message failed with error: {responseData['messages'][0]['error-text']}")
                        return Response(status="failure")
    except Exception as e:
        print(str(e))
        return Response(status="retry later")

if __name__ == '__main__':
    app.run(debug=True, port=5000,host='0.0.0.0')