Section C Http_banner get

Source: Internet
Author: User

#-*-coding=utf-8-*-# __author__  =  ' Sanr ' # __email__   =  ' [email  protected] ' # __url__     =  ' http://0x007.blog.51cto.com/' # __ version__ =  ' 0.1 ' import requestsimport refrom threading import thread, lockimport timeimport sysimport chardetimport netaddrimport structimport  Socketlock = 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):" Smart get page encoding first step find charset second step using Chardect smart Match "charset =  noneif pagehtml !=  ':# print  ' Use charset dect ' enc =  Chardet.detect (pagehtml) # print  ' enc=  ',  encif enc[' encoding '] and enc[' Confidence '] > 0.9:charset = enc[' encoding ']if charset&nbsP;== 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 charsettry:if charset:unicode (' Test ', charset,errors= ' replace ') except exception, e:print  ' Exception ',echarset = none# print  ' charset= ', charsetreturn  Charsetdef http_banner (URL): Ip=urltry:url=requests.get (url,timeout=2) body = url.contentcharset  = Noneif 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# Get webserver information server=url.headers[' server '][0:13]#Get Titleif struts==200 or struts==403 or struts==401:title=re.findall (R <title> ( . *) <\/title> ", body) If len (title): Title = title[0].strip () else:title = " #输出加锁   Prevent the second line from entering # request lock Lock.acquire () print  ('%s\t%d\t%-10s\t%s '% (ip.lstrip ('/HTTP '), Struts,server,title)) # Release lock Lock.release () except  (requests. Httperror,requests. Requestexception,attributeerror,keyerror),e:passif __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 '  %ipt = thread (target=http_ Banner,args= (URL,)) T.daemon=falset.start () elif  '/' in ips:ips = netaddr. Ipnetwork (IPS) for ip in list (IPS): url= ' http://%s '  %ipt = thread (target=http_ banner,args= (URL,)) T.daemon=falset.start ()

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/73/FB/wKiom1YLV4mAhyZVAARzoClci9E168.jpg "title=" http_ Banner.png "alt=" Wkiom1ylv4mahyzvaarzoclci9e168.jpg "/>

This article is from the "Sanr" blog, make sure to keep this source http://0x007.blog.51cto.com/6330498/1699463

Section C Http_banner get

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.