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

 

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

 

 

二、代码解析

复制代码
#!/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')
复制代码

 

三、解析效果

 

posted @   淡怀  阅读(788)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2020-04-03 【JMeter】+响应内容显示乱码
点击右上角即可分享
微信分享提示

目录导航