python目录扫描
其实做的有一段时间了,忘了把他放上来。前几天看了下多线程的实现,就把这个目录扫描也加上了多线程。
放出脚本
import requests
import linecache
import re
import threading
import time
url=input('输入要扫描的网址>>:')
threadnum=int(input("输入线程数>>:"))
allnum=int(len(open('D:/script/dir.txt','r',encoding='utf-8').readlines()))
def getdir(num):
dir_=linecache.getline('D:/script/dir.txt',num)
scannerdir=''.join(str(s) for s in dir_)
scannerdir=scannerdir.replace("\n", "")
return scannerdir
def getpayload(dirname,url):
payload=url+dirname
return payload
#扫描并返回状态码为的目录
def scanner(url):
r=requests.get(url)
code=r.status_code
if code == 200 or code == 403:
print(url+'-------------'+str(code))
#线程任务分配
def thread_distribute(num,threadnum,allnum):
t=[]
a=int(int(allnum)/int(threadnum))
for i in range(0,a+1):
number=int(num)+threadnum*i
if number <= int(allnum):
t.append(number)
return t
def startscan(t,url):
for i in t:
it=getdir(i)
targeturl=getpayload(it,url)
scanner(targeturl)
def main():
threads=[]
for i in range(1,threadnum+1):
_list=thread_distribute(i,threadnum,allnum)
intolist=threading.Thread(target=startscan,args=(_list,url))
threads.append(intolist)
#开始线程
for t in range(threadnum):
threads[t].start()
time.sleep(2)
#主线程等待分线程
for t in range(threadnum):
threads[t].join()
time.sleep(2)
print("扫描完成")
if __name__ == "__main__":
main()
没太多需要解释的地方,不过改了过后,包括那个端口扫描的脚本,都少了那个进度提示的功能。暂时还想不出多线程内该如何去实现。后续会去完成。