[Python] 纯文本查看 复制代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import json
from tkinter import *
import tkinter.font
def spawn_cookies(string):
data = {}
for line in string.split(';'):
line = str(line).strip()
if not line:
continue
chunk = line.split("=")
k = chunk[0]
v = "=".join(chunk[1:])
if 'JSESSIONID' == k and k in data:
continue
data[k] = v
return data
def spawn_headers(s):
headers = {}
cookies = {}
ignore_keys = ['Content-Length']
for line in s.split("\n"):
line = line.strip()
if not line:
continue
matched = re.match("^([a-zA-Z:\-]+):\s(.*?)$", line)
if matched is None:
raise ValueError("异常格式, line: %s" % line)
name = matched.group(1)
if name in ignore_keys:
continue
content = matched.group(2)
if name.lower() == "cookie":
cookies = spawn_cookies(content)
continue
headers[name] = content
return headers, cookies
class Header:
def __init__(self):
self.root = Tk()
self.root['bg'] = "white"
self.root.title("Headers格式化")
self.root.geometry('900x310')
self.root.resizable(0, 0)
self.ft = tkinter.font.Font(family='微软雅黑', size=10, weight='normal', slant='roman', underline=0, )
self.txt1 = tkinter.Text(self.root, relief="solid", wrap='none', font=self.ft)
self.txt1.place(x=10, y=0, width=250, height=300)
self.bind_menu(self.txt1)
self.txt2 = tkinter.Text(self.root, relief="solid", wrap='none', font=self.ft)
self.txt2.place(x=380, y=0, width=250, height=300)
self.bind_menu(self.txt2)
self.txt3 = tkinter.Text(self.root, relief="solid", wrap='none', font=self.ft)
self.txt3.place(x=640, y=0, width=250, height=300)
self.bind_menu(self.txt3)
self.btn = tkinter.Button(self.root, text="转换>>", width=10, height=4, relief="groove", font=self.ft,
command=self.format_str)
self.btn.place(x=270, y=50, width=100, height=80)
self.btn = tkinter.Button(self.root, text="清空", width=10, height=4, relief="groove", font=self.ft,
command=self.clear)
self.btn.place(x=270, y=150, width=100, height=80)
self.root.mainloop()
def deal_str(self):
data = {}
lt = [x for x in self.txt1.get(1.0, "end").strip().split('\n') if x]
for each in lt:
i = each.index(":", 1) if each[0] == ":" else each.index(":")
key = each[:i]
word = each[i + 1:]
if not word:
word = ""
key = key.strip()
word = word.strip()
data[key] = word
print(data)
return data
def format_str(self):
self.txt2.delete(0.0, "end")
try:
string = self.txt1.get(1.0, "end")
headers, cookies = spawn_headers(string)
except:
self.txt2.insert(END, "转换失败")
return
str_headers = json.dumps(headers, sort_keys=True, indent=2)
str_cookies = json.dumps(cookies, sort_keys=True, indent=2)
self.txt2.insert(END, str_headers)
self.txt3.insert(END, str_cookies)
self.txt2.focus()
return
def bind_menu(self, editor):
menubar = Menu(self.root, tearoff=False)
def rightkey(event, editor):
menubar.delete(0, END)
menubar.add_command(label='复制(Ctrl + C)', command=lambda: editor.event_generate('<<Copy>>'))
menubar.add_command(label='粘贴(Ctrl + V)', command=lambda: editor.event_generate('<<Paste>>'))
menubar.add_command(label='剪切(Ctrl + X)', command=lambda: editor.event_generate("<<Cut>>"))
menubar.add_command(label='全选(Ctrl + A)', command=lambda: editor.event_generate('<<SelectAll>>'))
menubar.post(event.x_root, event.y_root)
editor.bind("<Button-3>", lambda x: rightkey(x, editor))
return
def clear(self):
self.txt1.delete(0.0, "end")
self.txt2.delete(0.0, "end")
self.txt3.delete(0.0, "end")
return
if __name__ == '__main__':
Header()