Python Forum
Screen special characters - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Screen special characters (/thread-25879.html)



Screen special characters - ebolisa - Apr-14-2020

Hi,

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?

TIA

import re

capture = '''POST /configure HTTP/1.1\r\nHost: 192.168.4.1\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: http://192.168.4.1\r\nConnection: keep-alive\r\nReferer: http://192.168.4.1
/\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")
    pass

def handle_configure(client, request):
    match = re.search("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

    try:
        ssid = match.group(1).replace("%3F", "?").replace("%21", "!").replace("%40", "@")
        #ssid = match.group(1).decode("utf-8").replace("%3F", "?").replace("%21", "!").replace("%40", "@")
        password = match.group(2).replace("%3F", "?").replace("%21", "!").replace("%40", "@")
        #password = match.group(2).decode("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 = match.group(1).decode("utf-8").replace("%3F", "?").replace("%21", "!").replace("%40", "@")
        password = match.group(2).decode("utf-8").replace("%3F", "?").replace("%21", "!").replace("%40", "@")

handle_configure(0, capture)



RE: Screen special characters - snippsat - Apr-14-2020

(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)
'mySSID&password=myPass@home'
Also look at this Thread here.
He has written a parser for header data kiss-headers