Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
flask app decoding problem
#1
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')
Larz60+ write Aug-02-2021, 09:10 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
fixed for you this time. Please use bbcode tags on future posts.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Decoding a serial stream AKGentile1963 7 4,121 Mar-20-2021, 08:07 PM
Last Post: deanhystad
  Problem with Flask Bcrypt import module marcello86 2 3,239 Aug-31-2020, 08:10 PM
Last Post: marcello86
  xml decoding failure(bs4) roughstroke 1 1,361 May-09-2020, 04:37 PM
Last Post: snippsat
  python3 decoding problem but python2 OK mesbah 0 1,249 Nov-30-2019, 04:42 PM
Last Post: mesbah
  utf-8 decoding failed every time i try adnanahsan 21 6,897 Aug-27-2019, 04:25 PM
Last Post: adnanahsan
  hex decoding in Python 3 rdirksen 2 3,624 May-12-2019, 11:49 AM
Last Post: rdirksen
  Decoding log files in binary using an XML file. captainfantastic 1 1,701 Apr-04-2019, 02:24 AM
Last Post: captainfantastic
  decoding sub.process output with multiple \n? searching1 2 1,957 Feb-24-2019, 12:00 AM
Last Post: searching1
  URL DECODING UnionSystems 5 2,933 Jan-02-2019, 05:28 PM
Last Post: UnionSystems
  Decoding Hex / understanding Code NoWay 4 3,043 Mar-20-2018, 02:48 PM
Last Post: NoWay

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020