当前位置:   article > 正文

Python---密码生成器_python密码生成器

python密码生成器

Python—密码生成器

有一批服务器需要定时更新密码,密码要求最小14位,带大小写,数字,字符.由于这些机器需要我这边给密码,每次想密码又很麻烦,且这个环境与外网绝对隔离,也用不了一些其他工具.只能手写一个py用来创建

1. 代码部分

import tkinter

# 随机密码生成器
# 密码类
class Password:
    def __init__(self, length):
        self.length = length
        self.passwordlist = []
        self.num = 0
        self.az = 0
        self.AZ = 0
        self.other = 0
        self.prefix = ""

    def generate(self):
        import random
        temp = ''
        self.passwordlist = []
        pwsd = ''
        if self.num == 1:
            pwsd += '0123456789'
        if self.az == 1:
            pwsd += 'abcdefghijklmnopqrstuvwxyz'
        if self.AZ == 1:
            pwsd += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        if self.other == 1:
            pwsd += '~!@#$%^&*()_+-=,./<>?;:[]{}|'
        # 生成5个随机密码
        for i in range(5):
            for j in range(self.length - len(self.prefix)):
                temp += random.choice(pwsd)
            self.passwordlist.append(self.prefix+temp)
            temp = ''

# 窗口类
class Window:
    def __init__(self):
        self.window = tkinter.Tk()
        self.window.title('随机密码生成器--Infra 仇沁')
        self.window.geometry('400x340')
        self.window.resizable(0, 0)
        # 生成5格密码 5个复制按钮
        self.passwordlist = []
        self.copybuttonlist = []
        for i in range(5):
            self.passwordlist.append(tkinter.StringVar())
            self.passwordlist[i].set('密码' + str(i + 1))
            tkinter.Label(self.window, textvariable=self.passwordlist[i]).place(x=20, y=20 + i * 40, width=260, height=30)

        # 生成复制按钮,每个按钮对应一个密码
        for i in range(5):
            self.copybuttonlist.append(tkinter.Button(self.window, text='复制', command=lambda i=i: self.copy(i)))
            self.copybuttonlist[i].place(x=300, y=20 + i * 40, width=80, height=30)
        # 生成密码按钮
        self.generatebutton = tkinter.Button(self.window, text='生成密码', command=self.generate)
        self.generatebutton.place(x=20, y=300, width=360, height=30)
        # 密码长度
        tkinter.Label(self.window, text='密码长度').place(x=20, y=260, width=80, height=30)
        self.length = tkinter.StringVar()
        self.length.set('14')
        tkinter.Entry(self.window, textvariable=self.length).place(x=100, y=260, width=20, height=30)
        # 密码类型

        self.num = tkinter.IntVar()
        self.num.set(1)
        tkinter.Checkbutton(self.window, text='数字', variable=self.num).place(
            x=120, y=260, width=80, height=30)
        self.az = tkinter.IntVar()
        self.az.set(1)
        tkinter.Checkbutton(self.window, text='小写', variable=self.az).place(
            x=180, y=260, width=80, height=30)
        self.AZ = tkinter.IntVar()
        self.AZ.set(1)
        tkinter.Checkbutton(self.window, text='大写', variable=self.AZ).place(
            x=240, y=260, width=80, height=30)
        self.other = tkinter.IntVar()
        self.other.set(1)
        tkinter.Checkbutton(self.window, text='特殊字符', variable=self.other).place(
            x=300, y=260, width=80, height=30)
        # 密码前缀
        tkinter.Label(self.window, text='密码前缀').place(x=20, y=220, width=80, height=30)
        self.prefix = tkinter.StringVar()
        self.prefix.set('Pana#')
        tkinter.Entry(self.window, textvariable=self.prefix).place(x=100, y=220, width=80, height=30)

    def generate(self):
        # 实例化密码类
        pwsd1 = Password(int(self.length.get()))
        pwsd1.num = self.num.get()
        pwsd1.az = self.az.get()
        pwsd1.AZ = self.AZ.get()
        pwsd1.other = self.other.get()
        pwsd1.prefix = self.prefix.get()
        pwsd1.generate()
        for i in range(5):
            self.passwordlist[i].set(pwsd1.passwordlist[i])

    def copy(self,i):
        print(i)
        pass
        #print(i)
        self.window.clipboard_clear()
        self.window.clipboard_append(self.passwordlist[i].get())

if __name__ == "__main__":
    window = Window()
    window.window.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107

2. 效果

在这里插入图片描述
在这里插入图片描述

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号