请求头格式化小工具(HeadersConverter)
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在同一行,用英文格式冒号分隔,如下:
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格式,效果如下:
另外,由于我经常使用只带ua的请求头,所以当输入框为空时,点击“转换”,可生成一个默认的请求头
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",
}
————————————————————————————
源代码如下:
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 =
formatted_headers = {}
i = 0
while i < len(lines):
line = lines
if ':' in line:
parts = line.split(':', 1)
key = parts.strip().lower()
if parts.strip() != '':
value = parts.strip()
i += 1
else:
value = lines.strip()
i += 2
formatted_headers = 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 好工具,顶你一下 感谢分享 虽然用不上但还是要顶你一下{:1_921:} 有用,感谢分享 这个实用,谢谢楼主分享。 好工具,拿来用用。 遇到过,有的控制台就是这样的 感谢分享,好用 好工具!我也想用python写一个小工具,看到PyQt5做的界面有点思路了。
页:
[1]
2