How can I get the parameters sent via POST request in a dictionary format and parse the headers efficiently using Python?

I am implementing a simple HTTP server.

Main question:

Whenever client makes a POST request parameters are sent after headers with a blank line in between. Now I want to parse those parameters and get them in a dictionary format. How can I do that?

E.g. Suppose the received parameters are:

name=John%2BDoe&n=42&Submit=Submit

then what I want is :

{'name' : 'John+Doe', 'n' : '42', 'Submit' : 'Submit', }

Second question :

I also want to parse the headers and get their values properly. For now, I am doing it like this :

request = request.split('\r\n')
request_headers = request[1:]
for header in request_headers:
        header = header.split(": ")
        if len(header) > 1:
            headers[header[0].lower()] = header[1].lower()

Is there any way I can do better than this?



Read more here: https://stackoverflow.com/questions/64393528/how-can-i-get-the-parameters-sent-via-post-request-in-a-dictionary-format-and-pa

Content Attribution

This content was originally published by Atharva at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: