吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1110|回复: 15
收起左侧

[Python 原创] 请求头格式化小工具(HeadersConverter)

[复制链接]
FeiyuYip 发表于 2024-11-4 22:44
[Python] 纯文本查看 复制代码
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.7
Accept-Encoding:
gzip, deflate, br
Accept-Language:
zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6
Cache-Control:
max-age=0
Connection:
keep-alive
Cookie:
BIDUPSID=4F35A99C2D0F8233AADB36BA2B6909ED; PSTM=1696333643; BAIDUID=E4EC93535B0310C56190403CD6CD43A6:FG=1; BD_HOME=1; BD_UPN=12314753; BA_HECTOR=0h01ah8ga18h20248l0l85803335ao1jihl831u; BAIDUID_BFESS=E4EC93535B0310C56190403CD6CD43A6:FG=1; ZFY=0cEH4d4x9VocBj26zMuDuxmYT3S:BDocX4TImHafZKfs:C
Host:
www.baidu.com
Sec-Ch-Ua:
"Not_A Brand";v="8", "Chromium";v="120", "Microsoft Edge";v="120"
Sec-Ch-Ua-Mobile:
?0
Sec-Ch-Ua-Platform:
"Windows"
Sec-Fetch-Dest:
document
Sec-Fetch-Mode:
navigate
Sec-Fetch-Site:
none
Sec-Fetch-User:
?1
Upgrade-Insecure-Requests:
1
User-Agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
X-Forwarded-For:
192.192.27.29
X-Forwarded-Proto:
https

复制网页中的请求头时,往往出来的是上面这种格式——key和value各占一行,尝试了论坛中的请求头转换工具,好像没有能处理这种格式的
一般处理的格式为key与value在同一行,用英文格式冒号分隔,如下:
[Python] 纯文本查看 复制代码
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.7
Accept-Encoding:gzip, deflate, br
Accept-Language:zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6
Cache-Control:max-age=0
Connection:keep-alive
Cookie:BIDUPSID=4F35A99C2D0F8233AADB36BA2B6909ED; PSTM=1696333643; BAIDUID=E4EC93535B0310C56190403CD6CD43A6:FG=1; BD_HOME=1; BD_UPN=12314753; BA_HECTOR=0h01ah8ga18h20248l0l85803335ao1jihl831u; BAIDUID_BFESS=E4EC93535B0310C56190403CD6CD43A6:FG=1; ZFY=0cEH4d4x9VocBj26zMuDuxmYT3S:BDocX4TImHafZKfs:C
Host:www.baidu.com
Sec-Ch-Ua:"Not_A Brand";v="8", "Chromium";v="120", "Microsoft Edge";v="120"
Sec-Ch-Ua-Mobile:?0
Sec-Ch-Ua-Platform:"Windows"
Sec-Fetch-Dest:document
Sec-Fetch-Mode:navigate
Sec-Fetch-Site:none
Sec-Fetch-User:?1
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
X-Forwarded-For:192.192.27.29
X-Forwarded-Proto:https

于是写了一个小工具,用于实现将如上两种格式的请求头输入,转换成标准的headers格式,效果如下:
11.jpg 22.jpg
另外,由于我经常使用只带ua的请求头,所以当输入框为空时,点击“转换”,可生成一个默认的请求头
[Python] 纯文本查看 复制代码
headers = {
   "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
}

33.jpg
————————————————————————————
源代码如下:
[Python] 纯文本查看 复制代码
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QTextEdit, QPushButton, QVBoxLayout, QHBoxLayout, QMessageBox
from PyQt5.QtGui import QClipboard, QFont, QColor
from PyQt5.QtCore import QMimeData


class HeadersConverter(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        self.setGeometry(300, 300, 800, 600)
        self.setWindowTitle('Headers Converter')
        self.setStyleSheet("background-color: #f0f0f0;")

        self.input_text_edit = QTextEdit()
        self.input_text_edit.setStyleSheet(
            "background-color: #ffffff; border: 1px solid #cccccc; padding: 10px; border-radius: 10px;")
        self.input_text_edit.setFont(QFont("微软雅黑", 12))

        self.output_text_edit = QTextEdit(readOnly=True)
        self.output_text_edit.setStyleSheet(
            "background-color: #ffffff; border: 1px solid #cccccc; padding: 10px; border-radius: 10px;")
        self.output_text_edit.setFont(QFont("微软雅黑", 12))

        self.convert_button = QPushButton('转换')
        self.convert_button.setStyleSheet(
            "background-color: #4CAF50; color: #ffffff; border: none; padding: 10px 20px; border-radius: 5px;")
        self.convert_button.setFont(QFont("微软雅黑", 12))

        self.clear_button = QPushButton('清空')
        self.clear_button.setStyleSheet(
            "background-color: #FF9800; color: #ffffff; border: none; padding: 10px 20px; border-radius: 5px;")
        self.clear_button.setFont(QFont("微软雅黑", 12))

        self.copy_button = QPushButton('复制')
        self.copy_button.setStyleSheet(
            "background-color: #2196F3; color: #ffffff; border: none; padding: 10px 20px; border-radius: 5px;")
        self.copy_button.setFont(QFont("微软雅黑", 12))

        self.convert_button.clicked.connect(self.convert_headers)
        self.clear_button.clicked.connect(self.clear_input)
        self.copy_button.clicked.connect(self.copy_output)

        button_layout = QHBoxLayout()
        button_layout.addWidget(self.convert_button)
        button_layout.addWidget(self.clear_button)
        button_layout.addWidget(self.copy_button)

        main_layout = QVBoxLayout()
        main_layout.addWidget(self.input_text_edit)
        main_layout.addWidget(self.output_text_edit)
        main_layout.addLayout(button_layout)

        self.setLayout(main_layout)

    def convert_headers(self):
        input_text = self.input_text_edit.toPlainText()
        if not input_text:
            output_text = 'headers = {\n'
            output_text += '   "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",\n'
            output_text += '}'
            self.output_text_edit.setText(output_text)
        else:
            lines = input_text.split('\n')
            lines = [line for line in lines if line.strip() != '']
            formatted_headers = {}
            i = 0
            while i < len(lines):
                line = lines[i]
                if ':' in line:
                    parts = line.split(':', 1)
                    key = parts[0].strip().lower()
                    if parts[1].strip() != '':
                        value = parts[1].strip()
                        i += 1
                    else:
                        value = lines[i + 1].strip()
                        i += 2
                    formatted_headers[key] = value
                else:
                    i += 1

            output_text = "headers = {\n"
            for key, value in formatted_headers.items():
                output_text += f"    '{key}': '{value}',\n"
            output_text += "}"

            self.output_text_edit.setText(output_text)

    def clear_input(self):
        self.input_text_edit.clear()
        self.output_text_edit.clear()

    def copy_output(self):
        clipboard = QApplication.clipboard()
        mime_data = QMimeData()
        mime_data.setText(self.output_text_edit.toPlainText())
        clipboard.setMimeData(mime_data)
        QMessageBox.information(self, '提示', '转换的headers已被复制!')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = HeadersConverter()
    ex.show()
    sys.exit(app.exec_())

——————————————————————————
成品如下:

通过百度网盘分享的文件:HeadersConverter.rar
链接:https://pan.baidu.com/s/1v3TY56Qp_2CW2S40AeAVQw?pwd=7q4j
提取码:7q4j

免费评分

参与人数 4吾爱币 +11 热心值 +3 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
99910369 + 1 + 1 谢谢@Thanks!
wkdxz + 2 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
naonao211 + 1 我很赞同!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

xixicoco 发表于 2024-11-4 23:41
好工具,顶你一下
huakai200 发表于 2024-11-5 05:30
电脑维修008 发表于 2024-11-5 07:12
FYL11162022 发表于 2024-11-5 07:26
有用,感谢分享
Wapj_Wolf 发表于 2024-11-5 07:56
这个实用,谢谢楼主分享。
zhqh 发表于 2024-11-5 08:03
好工具,拿来用用。
不苦小和尚 发表于 2024-11-5 08:40
遇到过,有的控制台就是这样的
HHJ200318 发表于 2024-11-5 08:57
感谢分享,好用
Defienite 发表于 2024-11-5 09:13
好工具!我也想用python写一个小工具,看到PyQt5做的界面有点思路了。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-22 02:40

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表