http_banner獲取v0.2-創(chuàng)新互聯(lián)

#-*-coding=utf-8-*-
# __author__  = 'sanr'
# __email__   = '5754190@qq.com'
# __url__     = 'http://0x007.blog.51cto.com/'
# __version__ = '0.2'
import requests
import re
from threading import Thread,Lock
import time
import sys
import chardet
import netaddr
import struct
import socket

lock = Lock()

def ip2int(addr):
        return struct.unpack("!I", socket.inet_aton(addr))[0]
def int2ip(addr):
        return socket.inet_ntoa(struct.pack("!I", addr))
def int_dec(pagehtml):

        charset = None
        if pagehtml != '':
                # print 'use charset dect'
                enc = chardet.detect(pagehtml)
                # print 'enc= ', enc
                if enc['encoding'] and enc['confidence'] > 0.9:
                        charset = enc['encoding']

                if charset == None:
                        charset_re = re.compile("((^|;)\s*charset\s*=)([^\"']*)", re.M)
                        charset=charset_re.search(pagehtml[:1000]) 
                        charset=charset and charset.group(3) or None

                # test charset
                try:
                        if charset:
                                unicode('test',charset,errors='replace')
                except Exception,e:
                        print 'Exception',e
                        charset = None
        # print 'charset=', charset
        return charset


def http_banner(url):
        ip=url
        try:
                url=requests.get(url,timeout=2)        

                body = url.content
                
                charset = None
                if body != '':
                        charset = int_dec(body)

                if charset == None or charset == 'ascii':
                        charset = 'ISO-8859-1'

                if charset and charset != 'ascii' and charset != 'unicode':
                        try:
                                body = unicode(body,charset,errors='replace')
                        except Exception, e:
                                body = ''
                Struts=url.status_code
                Server=url.headers['server'][0:13]
                if Struts==200 or Struts==403 or Struts==401:
                        title=re.findall(r"<title>(.*)<\/title>",body)
                        if len(title):
                                title = title[0].strip()
                        else:
                                title = ''
                        lock.acquire()
                        print ('%s\t%d\t%-10s\t%s'%(ip.lstrip('http://'),Struts,Server,title))
                        lock.release()
        except (requests.HTTPError,requests.RequestException,AttributeError,KeyError),e:
                pass



if __name__ == '__main__':
        if len(sys.argv) >= 2:
                ips = sys.argv[1]
        else:
                print 'usage: python http_banner.py 192.168.1./24 '
                print 'usage: python http_banner.py 192.168.1.1-192.168.1.254 '
                print 'usage: python http_banner.py 192.168.1./24 8080'
                print 'usage: python http_banner.py 192.168.1.1-192.168.1.254 8080'
                sys.exit()
        port = '80'
        if len(sys.argv) == 3:
                port = sys.argv[2]
                
        if '-' in ips:
                start, end = ips.split('-')
                startlong = ip2int(start)
                endlong = ip2int(end)
                ips = netaddr.IPRange(start,end)
                for ip in list(ips):
                        url='http://%s:%s'%(ip,port)
                        t = Thread(target=http_banner,args=(url,))
                        t.daemon=False
                        t.start()
        elif '/'        in ips:
                ips = netaddr.IPNetwork(ips)
                for ip in list(ips):
                        url='http://%s:%s'%(ip,port) 
                        t = Thread(target=http_banner,args=(url,))
                        t.daemon=False
                        t.start()

更新了端口自定義 默認端口為80

創(chuàng)新互聯(lián)于2013年創(chuàng)立,先為泰和等服務(wù)建站,泰和等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為泰和企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

http_banner獲取v0.2

http_banner獲取v0.2

http_banner獲取v0.2

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

當前名稱:http_banner獲取v0.2-創(chuàng)新互聯(lián)
瀏覽地址:http://bm7419.com/article18/cdgsdp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗、手機網(wǎng)站建設(shè)微信公眾號、靜態(tài)網(wǎng)站全網(wǎng)營銷推廣、微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計