首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用并发线程使函数更快?

如何使用并发线程使函数更快?
EN

Stack Overflow用户
提问于 2020-05-08 17:48:16
回答 2查看 462关注 0票数 1

我想要建立一个工具来扫描一个网站的子域,我知道如何做他的,但我的功能比较慢,我查了一下gobuster的使用,我看到gobuster可以使用许多并发线程,我如何实现这一点?

我问过谷歌很多次了,但我看不出这方面的任何情况,有人能给我举个例子吗?

gobuster usage: -t Number of concurrent threads (default 10)

我的当前程序:

代码语言:javascript
复制
def subdomaines(url, wordlist):
    checks(url, wordlist) # just checking for valid args
    num_lines = get_line_count(wordlist) # number of lines in a file
    count = 0
    for line in open(wordlist).readlines():
        resp = requests.get(url + line) # resp
        if resp.status_code in (301, 200):
            print(f'Valid - {line}')
        print(f'{count} / {num_lines}')
        count += 1

注:gobuster是一个快速搜索网站子域的工具。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-05-08 18:24:32

如果您想在python中使用线程,您应该从基础开始,了解可用的内容。但是下面是一个从https://pymotw.com/2/threading/获取的简单例子

代码语言:javascript
复制
import threading

def worker():
    """thread worker function"""
    print 'Worker'
    return

threads = []
for i in range(5):
    t = threading.Thread(target=worker)
    threads.append(t)
    t.start()

要将其应用于您的任务,一个简单的方法是为每个请求生成一个线程。就像下面的代码。注意:如果你的单词列表很长,这可能会非常昂贵。查看python中的一些线程池库,以便更好地管理不需要显式控制自己的线程。

代码语言:javascript
复制
import threading

def subdomains(url, wordlist):
    checks(url, wordlist) # just checking for valid args
    num_lines = get_line_count(wordlist) # number of lines in a file
    count = 0
    threads = []
    for line in open(wordlist).readlines():
        t = threading.Thread(target=checkUrl,args=(url,line))
        threads.append(t)
        t.start()
    for thread in threads:  #wait for all threads to complete
       thread.join()


 def checkUrl(url,line):
    resp = requests.get(url + line) 
    if resp.status_code in (301, 200):
          print(f'Valid - {line}')

要实现计数器,您需要控制线程之间的共享访问,以防止争用条件(两个线程同时访问变量,从而导致.问题)。在上面的链接中提供了一个具有受保护访问的计数器对象:

代码语言:javascript
复制
class Counter(object):
    def __init__(self, start=0):
        self.lock = threading.Lock()
        self.value = start
    def increment(self):
        #Waiting for lock
        self.lock.acquire()
        try:
            #Acquired lock
            self.value = self.value + 1
        finally:
            #Release lock, so other threads can count
            self.lock.release()

#usage:

#in subdomains()...
  counter = Counter()
  for ...
     t = threading.Thread(target=checkUrl,args=(url,line,counter))


#in checkUrl()...
  c.increment()

最后注意:我没有编译或测试任何这段代码。

票数 0
EN

Stack Overflow用户

发布于 2020-05-08 18:03:50

Python有穿线模块。

使用Thread最简单的方法是使用目标函数实例化它,并调用start()让它开始工作。

代码语言:javascript
复制
import threading

def subdomains(url, wordlist):
    checks(url, wordlist) # just checking for valid args
    num_lines = get_line_count(wordlist) # number of lines in a file
    count = 0
    for line in open(wordlist).readlines():
        resp = requests.get(url + line) # resp
        if resp.status_code in (301, 200):
            print(f'Valid - {line}')
        print(f'{count} / {num_lines}')
        count += 1

threads = []
for i in range(10):
    t = threading.Thread(target=subdomains)
    threads.append(t)
    t.start()
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61684992

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档