C段http_banner获取-创新互联
#-*-coding=utf-8-*- # __author__ = 'sanr' # __email__ = '5754190@qq.com' # __url__ = 'http://0x007.blog.51cto.com/' # __version__ = '0.1' 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 第二步使用chardect智能匹配 ''' 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 #获取webserver信息 Server=url.headers['server'][0:13] #获取title if Struts==200 or Struts==403 or Struts==401: title=re.findall(r"目前创新互联建站已为上千的企业提供了网站建设、域名、雅安服务器托管、网站托管运营、企业网站设计、鄂城网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。 附件:http://down.51cto.com/data/2366174(.*)<\/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 ' sys.exit() 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' %ip 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' %ip t = Thread(target=http_banner,args=(url,)) t.daemon=False t.start()
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
文章标题:C段http_banner获取-创新互联
当前链接:http://lswzjz.com/article/dddggj.html