How to convert socket message to json format in Python?

Question:

I have the following line which receives a string upon incoming web request.

data_from_flask = c.recv(2048).decode('utf8')

The message is

'GET / HTTP/1.1rnHost: localhost:31477rnConnection: keep-alivernCache-Control: max-age=0rnsec-ch-ua: "Microsoft Edge";v="105", " Not;A Brand";v="99", "Chromium";v="105"rnsec-ch-ua-mobile: ?0rnsec-ch-ua-platform: "Windows"rnUpgrade-Insecure-Requests: 1rnUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.27rnAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9rnSec-Fetch-Site: cross-siternSec-Fetch-Mode: navigaternSec-Fetch-User: ?1rnSec-Fetch-Dest: documentrnAccept-Encoding: gzip, deflate, brrnAccept-Language: en-US,en;q=0.9rnrn'

I want to load this to a JSON format using

job_json = json.loads(data_from_flask)

If I directly run this, I get the following error

The exception has occurred: JSONDecodeError
Expecting value: line 1 column 1 (char 0)

because the incoming string is not entirely in JSON format.

What is the simplest way I could do this? Is there any special encoding I could use ?

Asked By: PCG

||

Answers:

What you have is a string that represents HTTP headers. You can parse this to make a Python dictionary which can subsequently be converted to JSON.

For example:

import json

headers = """GET / HTTP/1.1rnHost: localhost:31477rnConnection: keep-alivernCache-Control: max-age=0rnsec-ch-ua: "Microsoft Edge";v="105", " Not;A Brand";v="99", "Chromium";v="105"rnsec-ch-ua-mobile: ?0rnsec-ch-ua-platform: "Windows"rnUpgrade-Insecure-Requests: 1rnUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.27rnAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9rnSec-Fetch-Site: cross-siternSec-Fetch-Mode: navigaternSec-Fetch-User: ?1rnSec-Fetch-Dest: documentrnAccept-Encoding: gzip, deflate, brrnAccept-Language: en-US,en;q=0.9rnrn"""

tokens = headers.split('rn')
pd = {}

for t in tokens[1:]: # skip the protocol
    k, *v = t.split(':')
    if k:
        pd[k] = ':'.join(v).strip()

print(json.dumps(pd, indent=4))

Output:

{
    "Host": "localhost:31477",
    "Connection": "keep-alive",
    "Cache-Control": "max-age=0",
    "sec-ch-ua": ""Microsoft Edge";v="105", " Not;A Brand";v="99", "Chromium";v="105"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": ""Windows"",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.27",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "Sec-Fetch-Site": "cross-site",
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-User": "?1",
    "Sec-Fetch-Dest": "document",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "en-US,en;q=0.9"
}
Answered By: OldBill
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.