Example of detecting socket and web services using python

Source: Internet
Author: User

Operating System: linux
Software environment: Python 2.7.3

Usage:
Copy codeThe Code is as follows:
$./MonSocket. py
# This is check the URI or Socket of the script #
Usage:
./MonSocket. py-d URL; This is Http protocol
./MonSocket. py-s socket IP or domain; This is Socket protocol
./MonSocket. py-p port; This is Socket port
./MonSocket. py-n; Total number of requests
./MonSocket. py-c; Number of concurrent requests
./MonSocket. py-t; Timeout time (s), socket default is 1 s, http default is 5S
For exampale:./MonSocket. py-d www.weibo.com/index.php-n 200-c 10-t 2
For exampale:./MonSocket. py-s 10.210.214.249-p 80-n 200-c 50-t 3

Code:
Copy codeThe Code is as follows:
#! /Usr/bin/env python
# Encoding: UTF-8

#
# Write by flying snail bait-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), socket default is 1 s, http default is 5 s" % 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. setdefatimetimeout (time)
Request = urllib2.Request ('HTTP: // % s' % (url ))
Try:
Ret = urllib2.urlopen (request)
Failed t 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. setdefatimetimeout (time)
S = socket. socket (socket. AF_INET, socket. SOCK_STREAM)
Try:
Ret = s. connect (server, int (port )))
Failed t 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 :")
Counter t 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 ()

Related Article

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.