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

更新如下

目前創(chuàng)新互聯(lián)公司已為上1000+的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站托管運(yùn)營(yíng)、企業(yè)網(wǎng)站設(shè)計(jì)、永勝網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
  1. 端口可自定義 默認(rèn)80

  2. 線程可自定義 默認(rèn)10

  3. 使用了隊(duì)列存消息

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


lock = Lock()

q = Queue.Queue()

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):
    '''
	智能獲取頁(yè)面編碼
	第一步查找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():

	while True:
		try:
			if q.qsize() == 0:
				break;
			ip = q.get()
			
			url='http://%s:%s'%(ip,port)
			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 = ''
			#獲取狀態(tài)碼
			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"<title>(.*)<\/title>",body)
				if len(title):
					title = title[0].strip()
				else:
					title = ''
				#輸出加鎖 防止第二行輸入
				#申請(qǐng)鎖
				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


def main(ips,threads=10):
	if '-' in ips:
		start, end = ips.split('-')
		startlong = ip2int(start)
		endlong = ip2int(end)
		ips = netaddr.IPRange(start,end)
		for ip in list(ips):
			q.put(str(ip))
	elif '/'	in ips:
		ips = netaddr.IPNetwork(ips)
		for ip in ips:
			q.put(str(ip))
			
	ths = []
	for i in xrange(threads):
		th = Thread(target=http_banner,name='thread'+str(i))
		th.start()
		ths.append(th)
		
if __name__ == '__main__':
	parser = optparse.OptionParser('usage: %prog [options] target')
	parser.add_option('-p', '--port', dest='port', default='80',type='string', help='Port.default = 80')
	parser.add_option('-t', '--threads', dest='threads_num',default=10, type='int',help='Number of threads. default = 10')
	(options,args) = parser.parse_args()
	if len(args) < 1:
		parser.print_help()
		print 'usage: python %s 218.92.227.1-218.92.227.254'%os.path.basename(sys.argv[0])
		print 'usage: python %s 218.92.227.1/24 '%os.path.basename(sys.argv[0])
		print 'usage: python %s 218.92.227.1/24 -p 8080'%os.path.basename(sys.argv[0])
		print 'usage: python %s 218.92.227.1/24 -t 100 -p 8080'%os.path.basename(sys.argv[0])

		sys.exit(0)
	ips=args[0]
	port=options.port
	threads = options.threads_num
	main(ips,int(threads))	
	

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

網(wǎng)站欄目:http_banner獲取2.0-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://muchs.cn/article40/dddoho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)Google、搜索引擎優(yōu)化、網(wǎng)站設(shè)計(jì)、網(wǎng)站內(nèi)鏈、網(wǎng)站策劃

廣告

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

外貿(mào)網(wǎng)站建設(shè)