欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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調(diào)用Google?Bard的圖文詳解

    Python調(diào)用Google?Bard的圖文詳解

    Google?Bard?是一種開源數(shù)據(jù)可視化和探索工具,可為?開發(fā)人員?提供支持,本文主要為大家介紹了Python調(diào)用Google?Bard的方法,需要的可以參考下
    2023-08-08
  • python通過pil為png圖片填充上背景顏色的方法

    python通過pil為png圖片填充上背景顏色的方法

    這篇文章主要介紹了python通過pil為png圖片填充上背景顏色的方法,實例分析了Python使用pil模塊操作png圖片的技巧,非常具有實用價值,需要的朋友可以參考下
    2015-03-03
  • 使用Python刪除PDF文件名中的特定文字

    使用Python刪除PDF文件名中的特定文字

    在處理大量PDF文件時,有時候我們可能需要對文件名進(jìn)行批量修改,例如,我們可能需要將文件名中的特定文字刪除或替換,今天,我將向大家介紹如何使用Python編寫一個簡單的程序,選擇一個文件夾,并刪除文件名中的指定文字,需要的朋友可以參考下
    2023-09-09
  • pytorch使用nn.Moudle實現(xiàn)邏輯回歸

    pytorch使用nn.Moudle實現(xiàn)邏輯回歸

    這篇文章主要為大家詳細(xì)介紹了pytorch使用nn.Moudle實現(xiàn)邏輯回歸,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • python+VTK環(huán)境搭建及第一個簡單程序代碼

    python+VTK環(huán)境搭建及第一個簡單程序代碼

    這篇文章主要介紹了python+VTK環(huán)境搭建及第一個簡單程序代碼,簡單介紹了vtk,然后分享了安裝步驟,最后涉及一個簡單的代碼示例,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • 教你使用Python從文件中提取IP地址

    教你使用Python從文件中提取IP地址

    Python提供了高效的高級數(shù)據(jù)結(jié)構(gòu),還能簡單有效地面向?qū)ο缶幊?下面這篇文章主要給大家介紹了關(guān)于如何使用Python從文件中提取IP地址的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • tensorflow+k-means聚類簡單實現(xiàn)貓狗圖像分類的方法

    tensorflow+k-means聚類簡單實現(xiàn)貓狗圖像分類的方法

    這篇文章主要介紹了tensorflow+k-means聚類簡單實現(xiàn)貓狗圖像分類,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • 使用 Python 創(chuàng)建一個基于規(guī)則的聊天機(jī)器人

    使用 Python 創(chuàng)建一個基于規(guī)則的聊天機(jī)器人

    這篇文章主要介紹了使用 Python 創(chuàng)建一個基于規(guī)則的聊天機(jī)器人,使用 Python 創(chuàng)建一個簡單的基于規(guī)則的聊天機(jī)器人 聊天機(jī)器人本身是一種機(jī)器或軟件,它通過文本或句子模仿人類交互。 簡而言之,可以使用類似于與人類對話的軟件進(jìn)行聊天。
    2021-10-10
  • Python3中省略號(...)用法介紹

    Python3中省略號(...)用法介紹

    本文主要介紹了Python3中省略號(...)用法介紹,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • python 字典操作提取key,value的方法

    python 字典操作提取key,value的方法

    這篇文章主要介紹了python 字典操作提取key,value的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06

最新評論