Python Forum

Full Version: Screen special characters
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

The code below receives a Submit from an url and extracts the ssid and password names from it.

To screen the special characters, a 'replace' is used, but it makes it rather complex to use it for each special character.

Is there a simpler way to filter/convert them?


import re

capture = '''POST /configure HTTP/1.1\r\nHost:\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0)
 Gecko/20100101 Firefox/74.0\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\nAccept-
Language: es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3\r\nAccept-Encoding: gzip, deflate\r\nContent-Type: application/x-www-form-
urlencoded\r\nContent-Length: 43\r\nOrigin:\r\nConnection: keep-alive\r\nReferer:
/\r\nUpgrade-Insecure-Requests: 1\r\n\r\nssid=mySSID&password=myPass%40home''' #<--- interested info

    NOTE: the ssid should be myssid
    and the password should be mypass@home

def send_response(client, payload, status_code=200):
    print("Error 400")

def handle_configure(client, request):
    match ="ssid=([^&]*)&password=(.*)", request)
    print(match) #prints match='ssid=mySSID&password=myPass%40home' <-- @ is converted into hex %40

    if match is None:
        send_response(client, "Parameters not found", status_code=400)
        return False

        ssid ="%3F", "?").replace("%21", "!").replace("%40", "@")
        #ssid ="utf-8").replace("%3F", "?").replace("%21", "!").replace("%40", "@")
        password ="%3F", "?").replace("%21", "!").replace("%40", "@")
        #password ="utf-8").replace("%3F", "?").replace("%21", "!").replace("%40", "@")
        print("ssid={} password={}".format(ssid, password)) # <-- prints ssid=mySSID password=myPass@home which is correct!!
    except Exception:
        ssid ="utf-8").replace("%3F", "?").replace("%21", "!").replace("%40", "@")
        password ="utf-8").replace("%3F", "?").replace("%21", "!").replace("%40", "@")

handle_configure(0, capture)
(Apr-14-2020, 05:56 PM)ebolisa Wrote: [ -> ]Is there a simpler way to filter/convert them?
>>> from urllib.parse import unquote
>>> password = 'mySSID&password=myPass%40home'
>>> unquote(password)
Also look at this Thread here.
He has written a parser for header data kiss-headers