基于python ttkbootstarp的密钥生成器
生活随笔
收集整理的這篇文章主要介紹了
基于python ttkbootstarp的密钥生成器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
App key和App Secret
App key簡稱API接口驗證序號,是用于驗證API接入合法性的。接入哪個網站的API接口,就需要這個網站允許才能夠接入,如果簡單比喻的話:可以理解成是登陸網站的用戶名。
App Secret簡稱API接口密鑰,是跟App Key配套使用的,可以簡單理解成是密碼。
App Key 和 App Secret 配合在一起,通過其他網站的協議要求,就可以接入API接口調用或使用API提供的各種功能和數據。
比如淘寶聯盟的API接口,就是淘寶客網站開發的必要接入,淘客程序通過API接口直接對淘寶聯盟的數據庫調用近億商品實時數據。做到了輕松維護,自動更新。
#!/usr/bin/env python3 # -*- coding:utf-8 -*- # pyinstaller --clean -w -F -i favicon.ico app_gen.py -n 密鑰生成器import datetime import hashlib import os.path import random import ttkbootstrap as ttk from ttkbootstrap.constants import * from tkinter import filedialog as tkFileDialog from ttkbootstrap.dialogs import Messagebox import pyperclip import base64 from icon import img# 獲取當前工作目錄 path = os.getcwd() # 默認文件名 filename = 'id_rsa.txt'# 實例化創建應用程序窗口 root = ttk.Window(title="密鑰生成器V1.0", # 設置窗口的標題themename="litera", # 設置主題minsize=(800, 400), # 窗口的最小寬高maxsize=(1920, 1080), # 窗口的最大寬高resizable=(True, True), # 設置窗口是否可以更改大小alpha=1.0, # 設置窗口的透明度(0.0完全透明) )def setIcon(root):tmp = open("tmp.ico", "wb+")tmp.write(base64.b64decode(img)) # 寫入到臨時文件中tmp.close()root.iconbitmap("tmp.ico") # 設置圖標os.remove("tmp.ico")setIcon(root)root.place_window_center()root.wm_attributes('-topmost', 1) # 讓窗口位置其它窗口之上ENTRY_WIDTH = 320 LABEL_LEFT_X = 160 ENTRY_LEFT_X = 280# 在窗口上創建組件(AppId) APP_ID_Y = 60 labeName = ttk.Label(root, text='AppId:', font=("微軟雅黑", 16), bootstyle=INFO) labeName.place(x=LABEL_LEFT_X, y=APP_ID_Y, width=125, height=40) varId = ttk.StringVar(root, value='') entryName = ttk.Entry(root, width=200, textvariable=varId, bootstyle=INFO) entryName.place(x=ENTRY_LEFT_X, y=APP_ID_Y, width=ENTRY_WIDTH, height=40)def callback1(event=None):if(varId.get() != ''):pyperclip.copy(varId.get())Messagebox.show_info(title='溫馨提示', message='復制成功!')buttonCopy1 = ttk.Button(root, text='復制', bootstyle=(PRIMARY, "outline-toolbutton"), command=callback1) buttonCopy1.place(x=ENTRY_LEFT_X+ENTRY_WIDTH+20,y=APP_ID_Y, width=60, height=40)# 在窗口上創建組件(AppKey) APP_KEY_Y = 120 labeName = ttk.Label(root, text='AppKey:', bootstyle=INFO, font=("微軟雅黑", 16)) labeName.place(x=LABEL_LEFT_X, y=APP_KEY_Y, width=125, height=40) varKey = ttk.StringVar(root, value='') entryPwd = ttk.Entry(root, width=120, textvariable=varKey) entryPwd.place(x=ENTRY_LEFT_X, y=APP_KEY_Y, width=ENTRY_WIDTH, height=40)def callback2(event=None):if(varKey.get() != ''):pyperclip.copy(varKey.get())Messagebox.show_info(title='溫馨提示', message='復制成功!')buttonCopy1 = ttk.Button(root, text='復制', bootstyle=(PRIMARY, "outline-toolbutton"), command=callback2) buttonCopy1.place(x=ENTRY_LEFT_X+ENTRY_WIDTH+20,y=APP_KEY_Y, width=60, height=40)# 在窗口上創建組件(AppSecret) APP_SECRET_Y = 180 labeName = ttk.Label(root, text='AppSecret:',bootstyle=INFO, font=("微軟雅黑", 16)) labeName.place(x=LABEL_LEFT_X, y=APP_SECRET_Y, width=125, height=40) varSecret = ttk.StringVar(root, value='') entrySecret = ttk.Entry(root, width=120, textvariable=varSecret) entrySecret.place(x=ENTRY_LEFT_X, y=APP_SECRET_Y, width=ENTRY_WIDTH, height=40)def callback3(event=None):if(varSecret.get() != ''):pyperclip.copy(varSecret.get())Messagebox.show_info(title='溫馨提示', message='復制成功!')buttonCopy1 = ttk.Button(root, text='復制', bootstyle=(PRIMARY, "outline-toolbutton"), command=callback3) buttonCopy1.place(x=ENTRY_LEFT_X+ENTRY_WIDTH+20,y=APP_SECRET_Y, width=60, height=40)OPENID_PREFIX = "sddf"def create_app_id() -> str:"""生成開放接口openId,使用sddf作為統一前綴:return: 返回值名: 返回值說明"""now = datetime.datetime.now().timestamp()app_id = hashlib.md5(str(now).encode('utf-8'))return OPENID_PREFIX + app_id.hexdigest()[4:]def create_app_key(module='', length=8) -> str:"""函數說明:param module: 模塊名稱,用作生成app_key的"鹽值":param length: 模塊名稱,用作生成app_key的長度,默認8位:return: app_key: 業務key"""source_letters = module + \"abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"str_list = [random.choice(source_letters) for i in range(length)]app_key = ''.join(str_list)return app_keydef create_app_secret(salt: str, length=40) -> str:"""函數說明:param salt: 模塊名稱,用作生成app_key的"鹽值":param length: 模塊名稱,用作生成app_key的長度,默認8位:return: app_secret: 密鑰"""source_letters = salt + "abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"str_list = [random.choice(source_letters) for i in range(length)]app_secret = ''.join(str_list)return app_secret# 生成按鈕事件處理函數 def gen():varId.set(create_app_id())varKey.set(create_app_key())varSecret.set(create_app_secret(create_app_id()))BUTTON_FUN_Y = 260 BUTTON_FUN_H = 30 # 創建按鈕組件,同時設置按鈕事件處理函數 # 參數解釋: text='生成'文本內容 activeforeground='#ff0000'按下按鈕時文字顏色 command=login關聯的函數 buttonOk = ttk.Button(root, text='生成', bootstyle=SUCCESS, command=gen) buttonOk.place(x=270, y=BUTTON_FUN_Y, width=80, height=BUTTON_FUN_H)# 創建保存組件,同時設置按鈕事件處理函數 file_opt = options = {} options['defaultextension'] = '.txt' options['filetypes'] = [('all files', '.*'), ('text files', '.txt')] options['initialdir'] = path options['initialfile'] = filename options['parent'] = root options['title'] = '請選擇保存路徑'def saveToFile():"""Returns an opened file in write mode.This time the dialog just returns a filename and the file is opened by your own code."""if varId.get() == '' or varKey.get() == '' or varSecret.get() == '':Messagebox.show_error(message='請生成密鑰')return# get filenamefilename = tkFileDialog.asksaveasfilename(**file_opt)# open file on your ownif filename:app_id = "AppId: " + varId.get()app_key = "AppKey: " + varKey.get()app_secret = "AppSecret: " + varSecret.get()list = [app_id, app_key, app_secret]with open(filename, "w", encoding="utf-8") as fw: # 可以寫入txt文件for i in list:fw.write(i + "\n")Messagebox.show_info(message='寫入成功!')buttonSave = ttk.Button(root, text='保存', bootstyle=SUCCESS, command=saveToFile) buttonSave.place(x=420, y=BUTTON_FUN_Y, width=80, height=BUTTON_FUN_H)# 清空按鈕的事件處理函數def clear():# 清空生成的數據varId.set('')varKey.set('')varSecret.set('')buttonClear = ttk.Button(root, text='清空', bootstyle=SUCCESS, command=clear) buttonClear.place(x=550, y=BUTTON_FUN_Y, width=80, height=BUTTON_FUN_H)root.mainloop()打包icon文件需要用到的工具類:
# 這段程序可將圖標gen.ico轉換成icon.py文件里的base64數據 import base64 open_icon = open("favicon.ico", "rb") # favicon.icon為你要放入的圖標 b64str = base64.b64encode(open_icon.read()) # 以base64的格式讀出 open_icon.close() write_data = "img=%s" % b64str f = open("icon.py", "w+") # 將上面讀出的數據寫入到icon.py的img數組中 f.write(write_data) f.close()效果如下:
總結
以上是生活随笔為你收集整理的基于python ttkbootstarp的密钥生成器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【小松教你手游开发】【unity实用技能
- 下一篇: 2022.5.29 第八次周报