最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實現(xiàn)探測socket和web服務(wù)示例

 更新時間:2014年03月28日 09:44:05   投稿:zxhpj  
這篇文章主要介紹了python實現(xiàn)探測socket和web服務(wù)示例,需要的朋友可以參考下

操作系統(tǒng):linux
軟件環(huán)境:Python 2.7.3

用法:

復(fù)制代碼 代碼如下:

$ ./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 1s,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

代碼:

復(fù)制代碼 代碼如下:

#!/usr/bin/env python
# encoding: utf-8

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 1s,http 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()

相關(guān)文章

  • python虛擬環(huán)境遷移方法

    python虛擬環(huán)境遷移方法

    今天小編就為大家分享一篇python虛擬環(huán)境遷移方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Windows自動執(zhí)行python腳本操作步驟

    Windows自動執(zhí)行python腳本操作步驟

    我們想在Windows中運行一個Python腳本,我們可以通過CMD,首先進入python文件所在的目錄,之后運行。但是這樣很麻煩,跟著本文操作就可以解決啦
    2021-09-09
  • python 實現(xiàn)提取log文件中的關(guān)鍵句子,并進行統(tǒng)計分析

    python 實現(xiàn)提取log文件中的關(guān)鍵句子,并進行統(tǒng)計分析

    今天小編就為大家分享一篇python 實現(xiàn)提取log文件中的關(guān)鍵句子,并進行統(tǒng)計分析,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • python實現(xiàn)處理Excel表格超詳細系列

    python實現(xiàn)處理Excel表格超詳細系列

    這篇文章主要介紹了python實現(xiàn)處理Excel表格超詳細系列,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-08-08
  • 基于python和flask實現(xiàn)http接口過程解析

    基于python和flask實現(xiàn)http接口過程解析

    這篇文章主要介紹了基于python和flask實現(xiàn)http接口過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • python實現(xiàn)兩個字典合并,兩個list合并

    python實現(xiàn)兩個字典合并,兩個list合并

    今天小編就為大家分享一篇python實現(xiàn)兩個字典合并,兩個list合并,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • 使用python-opencv讀取視頻,計算視頻總幀數(shù)及FPS的實現(xiàn)

    使用python-opencv讀取視頻,計算視頻總幀數(shù)及FPS的實現(xiàn)

    今天小編就為大家分享一篇使用python-opencv讀取視頻,計算視頻總幀數(shù)及FPS的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • 在django項目中,如何單獨運行某個python文件

    在django項目中,如何單獨運行某個python文件

    這篇文章主要介紹了在django項目中單獨運行某個python文件的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • Pycharm快捷鍵配置詳細整理

    Pycharm快捷鍵配置詳細整理

    這篇文章主要介紹了Pycharm快捷鍵配置詳細整理,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-10-10
  • python通過http上傳文件思路詳解

    python通過http上傳文件思路詳解

    這篇文章主要介紹了python通過http上傳文件,在post請求中,用files參數(shù)來接受文件對象相關(guān)的參數(shù),通過data/json參數(shù)接受post請求體的其他參數(shù)
    2021-07-07

最新評論

河间市| 安泽县| 乾安县| 康乐县| 黄大仙区| 石楼县| 开化县| 哈巴河县| 临漳县| 宾川县| 洞头县| 东莞市| 鄂托克前旗| 土默特左旗| 普陀区| 行唐县| 临高县| 高雄县| 崇州市| 成安县| 霍州市| 静乐县| 吉安市| 仁化县| 紫阳县| 平原县| 宁城县| 宽甸| 谢通门县| 健康| 辉县市| 海安县| 建平县| 永丰县| 长宁区| 安阳县| 静安区| 星座| 尉氏县| 太湖县| 崇礼县|