zl程序教程

您现在的位置是:首页 >  后端

当前栏目

【Python】解析浏览器中的header请求头为JSON格式

Python浏览器JSONJSON 解析 请求 格式 header
2023-09-11 14:16:50 时间

 

一、浏览器中的请求头复制到文件中

 

 

二、代码解析

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
@Time    :2022/4/3 22:37
@Author  :
@File    :header.py
@Version :1.0
@Function:
"""
import json


def headerAnalysis(headerPath):
    """
    浏览器中的header解析成JSON
    :param headerPath:
    :return:
    """
    headerJson = {}
    with open(headerPath, 'r', encoding='utf-8') as f:
        data = f.readlines()
        for i in data:
            h = i.split(': ')
            if h[1].endswith('\n'):
                h[1] = h[1][:-1]
            headerJson.update({h[0]: h[1]})
    print(json.dumps(headerJson, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False))


if __name__ == '__main__':
    headerAnalysis(r'C:\Users\Desktop\header.txt')

 

三、解析效果