#!/usr/bin/env python
# Encoding:utf-8
#
# Write by the flying snail-bob
Import Os,sys
Import Getopt,re
Import Socket,threading,urllib2
def usage ():
print ' # This is check the URI or Socket of the script # '
print ' Usage: '
print "%s-d URL; This is Http protocol "%sys.argv[0]
Print "%s-s socket IP or domain; This is Socket protocol "%sys.argv[0]
print "%s-p port; This is Socket Port '%sys.argv[0]
print "%S-N; Total number of requests "%sys.argv[0]
print "%S-C; Number of concurrent requests "%sys.argv[0]
print "%S-T; Timeout time (s), the socket default is 1s,http the default is 5s "%sys.argv[0]
Print "for Exampale:%s-d www.weibo.com/index.php-n 200-c 10-t 2"%sys.argv[0]
Print "for Exampale:%s-s 10.210.214.249-p 80-n 200-c 50-t 3"%sys.argv[0]
def detect_url (url,sign):
If timeout:
time = Int (timeout)
Else
Time = 5
Urllib2.socket.setdefaulttimeout (Time)
Request = Urllib2. Request (' http://%s '% (URL))
Try
ret = Urllib2.urlopen (Request)
Except Urllib2. Urlerror,e:
If Hasattr (E, "Reason"):
Port_timeout.append (' 1 ')
Elif hasattr (E, "code"):
If Re.findall (' ^3\d* ', '%s '%e.code):
Port_normal.append (' 1 ')
If Re.findall (' ^404\d* ', '%s '%e.code):
Port_404.append (' 1 ')
If Re.findall (' ^403\d* ', '%s '%e.code):
Port_403.append (' 1 ')
If Re.findall (' ^500\d* ', '%s '%e.code):
Port_500.append (' 1 ')
If Re.findall (' ^502\d* ', '%s '%e.code):
Port_502.append (' 1 ')
If Re.findall (' ^503\d* ', '%s '%e.code):
Port_503.append (' 1 ')
Else
Port_other.append (' 1 ')
Else
Port_normal.append (' 1 ')
Def detect_socket (server,port):
sign = 0
if timeout:
time = Int (timeout)
Else:
time = 1
socket.setdefaulttimeout (time)
s = Socket.socket (socket.af_inet, socket. SOCK_STREAM)
try:
ret = S.connect ((server, int (port))
except Socket.error, E:
if re.findall (' ^timed\ out* ', '%s '%e):
socket_timeout.append (' 1 ')
sign = 1
else:
print '%s '%e
sys.exit (2)
else:
socket_normal.append (' 1 ')
sign = 1
if sign = 0:
S.close ()
Def print_out ():
if url_mod:
print ' url: '
print ' timeout:[%s] '%len (port_timeout)
print ' normal:[%s] '%len (port_normal)
print ' \033[;31merror_403:[%s] \terror_404:[%s]\033[0m '% (Len (port_403), Len (port_404))
print ' \033[;31merror_500:[%s]\terror_502:[%s]\terror_503:[%s]\033[0m '% (Len (port_500), Len (port_502), Len (Port_ 503))
print ' \033[;31merror_other:[%s]\033[0m '%len (port_other)
if sock_mod:
print ' Socket: '
print ' timeout:[%s] '%len (socket_ Timeout)
print ' normal:[%s] '%len (socket_normal)
def main ():
If Sock_mod:
DEST_ARG1 = Sock_mod
DEST_ARG2 = Dport
Dest_function = Detect_socket
Elif Url_mod:
DEST_ARG1 = Url_mod
Dest_arg2 = ' '
Dest_function = Detect_url
Else
Sys.exit ()
Total = Int (DCount)
concurrent = Int (dconcurrent)
n = 0
Sign = 0
LASTNU = Total%concurrent
While 1:
Threads = []
If n + Concurrent > Total:
Nloops = Range (n,total)
Sign = 1
Else
Nloops = Range (n,n+concurrent)
For I in Nloops:
t = Threading. Thread (target=dest_function,args= (DEST_ARG1,DEST_ARG2))
Threads.append (t)
If sign = 1:
Th_nu = Lastnu
Else
Th_nu = Concurrent
For I in Range (TH_NU):
Threads[i].start ()
For I in Range (TH_NU):
Threads[i].join ()
n = n + concurrent
If sign = 1:
Break
Print_out ()
If __name__== ' __main__ ':
Try
Opts,args=getopt.getopt (sys.argv[1:], "hd:s:p:n:c:t:")
Except Getopt. Getopterror:
Usage ()
Sys.exit (2)
Port_timeout = []
Port_normal = []
port_403= []
port_404 = []
port_500 = []
port_502 = []
port_503 = []
Port_other = []
Socket_normal = []
Socket_timeout = []
DCount = 0
Url_mod = ' '
Sock_mod = ' '
Dport = ' '
dconcurrent = 0
Timeout = 0
If opts:
For Opt,arg in opts:
if opt = = '-h ':
Usage ()
Sys.exit ()
if opt = = ' d ':
Url_mod = arg
if opt = = ' s ':
Sock_mod = arg
if opt = = ' P ':
Dport = arg
if opt = = ' n ':
DCount = arg
if opt = = ' C ':
Dconcurrent = arg
if opt = = ' t ':
Timeout = arg
If Url_mod and DCount and dconcurrent:
Main ()
Elif Sock_mod and Dport and DCount and dconcurrent:
Main ()
Else
Usage ()
Else
Usage ()
Sys.exit ()