批量获取网站状态码及标题的脚本

1C 2020-01-16 PM 804℃ 0条

实在是懒得动
水一篇

下面是从网上扒的 批量获取网站状态码及标题的脚本,以我幼儿园的python水平改了一下。
效果图:
get_title.jpg

import chardet
import requests,re
import csv
from threading import Thread,activeCount
from sys import argv
from queue import Queue


requests.packages.urllib3.disable_warnings()
new_targets = []

def get_banner(url):
        if '://' not in url.strip():
            target = 'http://' + url.strip()
        else:
            target = url.strip()
        try:
            req = requests.get(target,verify=False,allow_redirects=True,timeout=(10,30))
            if 'charset' not in req.headers.get('Content-Type', " "):
                req.encoding = chardet.detect(req.content).get('encoding')
            code = req.status_code
            if '30' in str(code):
                if req.headers['Location'] == 'https://' + target.strip('http://') + '/':
                    req_30x = requests.get('https://{}'.format(target.strip('http://')),verify=False,timeout=(5,20))
                    code_30x = str(req_30x.status_code).strip()
                    if 'charset' not in req_30x.headers.get('Content-Type', " "):
                        req_30x.encoding = chardet.detect(req_30x.content).get('encoding')
                    try:
                        title_30x = re.findall(r'<title>(.*?)</title>',req_30x.text,re.S)[0].strip()
                    except:
                        title_30x = 'None'
                    if 'Server' in req_30x.headers:
                        server_30x = req_30x.headers['Server'].strip()
                    else:
                        server_30x = ''
                    if 'Content-Type' in req_30x.headers:
                        type_30x = req_30x.headers['Content-Type'].strip()
                    else:
                        type_30x = ''
                    if 'X-Powered-By' in req_30x.headers:
                        x_powered_by_30x = req_30x.headers['X-Powered-By'].strip()
                    else:
                      x_powered_by_30x = ''
                    print('[+] {} {} {} {} {} {} '.format(code_30x,target,title_30x,server_30x,type_30x,x_powered_by_30x))
                    write_info(target,code_30x,title_30x, server_30x, type_30x,x_powered_by_30x)
                else:
                    title = '302_redirection'
                    location = req.headers['Location']
                    print('[+] {} {} {} Location:{}'.format(code,target,title,location))
                    write_info(target,code,title,location=location)
            else:
                try:
                    title = re.findall(r'<title>(.*?)</title>',req.text,re.S)[0].strip()
                except:
                    title = 'None'
                if 'Server' in req.headers:
                    server = req.headers['Server'].strip()
                else:
                    server = ''
                if 'Content-Type' in req.headers:
                    type = req.headers['Content-Type'].strip()
                else:
                    type = ''
                if 'X-Powered-By' in req.headers:
                    x_powered_by = req.headers['X-Powered-By'].strip()
                else:
                    x_powered_by = ''
                write_info(target,code,title,server,type,x_powered_by)
                print('[+] {} {} {} {} {}'.format(code,target,title,server,x_powered_by))
        except:
            code = '无web响应'
            title = '服务器已爆炸'
            print('[+]{} {} {}'.format(code,target,title))
            with open('./title.csv','a+',newline='',encoding='GBK') as f:
                writer = csv.writer(f)
                writer.writerow([code,target,title])


def write_info(url,code,title='',server='',type='',x_power_by='',location=''):
    with open('./title.csv','a+',newline='',encoding='GBK') as f:
        writer = csv.writer(f)
        writer.writerow([code,url,title,server,type,x_power_by,location])

if __name__ == '__main__':
    try:
        queue = Queue()
        with open('./ip.txt','r+') as f:
            for url in f:
                url = url.strip()
                if url not in new_targets:
                    new_targets.append(url)
        for new_url  in new_targets:
            queue.put(new_url)
        while queue.qsize() > 0:
            if activeCount() <= 10:
                Thread(target=get_banner, args=(queue.get(),)).start()
    except IndexError:
        print('1111')
标签: 工具, Python

非特殊说明,本博所有文章均为博主原创。

评论啦~