分享一個自己寫的py掃描路徑工具

mac下懶得找掃描工具,自己寫了一個簡單的文件路徑如下:

#文件結構
-file文件夾
-result文件夾
-scan.py

# -*- coding: utf-8 -*-
import requests
import time
import os
from threading import Thread

global domain
domain = 'http://' + input('請輸入域名:')
print("\n")
global res_type
res_type = input('請選擇輸出方式:1-終端顯示,2-輸出到txt文檔:')
while res_type != '1' and res_type != '2' :
	res_type = input('輸入錯誤,請重新選擇輸出方式:1-終端顯示,2-輸出到txt文檔:')



root = 'file'
file = []
thread_num = 0
global start_time
start_time = time.time()

for dirpath, dirnames, filenames in os.walk(root):
    for filepath in filenames:
        file.append(filepath)

thread_num = len(file)
global num
num = 0
global total_num
total_num = 0
if res_type == '2':
	global handle
	name = str( 'result/' + time.strftime("%d%m%Y%H%M%S")) + '.txt'
	handle = open(name,'a')

def read(filename):
	time.sleep(3)
	global num
	global domain
	num = num+1
	name = 'file/' + filename
	path_list = []
	f = open(name,"r+")       # 返回一個文件對象   
	line = f.readline()                      # 調用文件的 readline()方法  
	while line:   
	    path_list.append(line.strip('\n'))                   
	    line = f.readline()    
	f.close() 
	headers =   {
		    'Accept': 'text/html, application/xhtml+xml, image/jxr, */*',
            'Accept - Encoding':'gzip, deflate',
            'Accept-Language':'zh-Hans-CN, zh-Hans; q=0.5',
            'Connection':'Keep-Alive',
            'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) 	Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063'
            }
	for i in range(0,len(path_list)):
		r = requests.get(domain + '/' + path_list[i],headers=headers,timeout=3,allow_redirects=False)
		if r.status_code in [200,403,500,501,502,503]:
			global total_num
			total_num = total_num+1
			global res_type
			global handle
			string = domain + '/' + path_list[i]+'                 '+ str(r.status_code)
			if res_type == '1':
				
				print(string)
			else :
				handle.write(string + '\n')
				
		
print('掃描準備中,請等待...')

for i in range(0,thread_num):
	name = file[i]
	t=Thread(target=read,args=(file[i],))
	t.start()
	if i == thread_num-1:
		t.join()
		end_time = time.time() - start_time
		end = '運行結束,總共用時 ' + str(end_time) + '秒,共掃描出 '+ str(total_num) + ' 個路徑'
		print(end) 
		if res_type == 2:
			handle.write(end)
			handle.close()	


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章