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: টিকা هنا My Environment Details:######################## ################# 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') |