您的位置:首页 > Web前端 > HTML

分享一个自己写的py扫描路径工具

2018-08-01 01:11 405 查看
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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Strip Gecko readline KHTML def
相关文章推荐