利用python并发模块进行网站的状态检测

python  curl.py


 
 
  1. #!/usr/bin/python    

  2. # -*- coding: utf-8 -*-

  3. import httplib

  4. #连接服务器

  5. conn=httplib.HTTPConnection('www.dnspod.cn')

  6. #发送HTTP请求

  7. conn.request('GET','url')

  8. #得到结果

  9. result=conn.getresponse()

  10. #获取HTTP请求结果值。200为成功

  11. resultresultStatus=result.status

  12. print resultStatus

  13. #获取请求的页面内容  

  14. content=result.read()

  15. #关闭连接

  16. conn.close()

  17. #如果要模拟客户端进行请求,可以发送HTTP请求头

  18. headers={"Content-Type":"text/html;charset=gb2312"}

  19. conn.requeset('POST','url',headersheaders=headers)

  20. #带参数传送  

  21. params=urllib.urlencode({'key':'value'});

  22. conn.request('POST','url',body=params)

还有一个 模拟 浏览器的方式~


 
 
  1. #!/usr/bin/python

  2. # -*- coding: utf-8 -*-

  3. import httplib

  4. conn = httplib.HTTPConnection('www.hao123.com')

  5. conn.request('GET', '/', headers = {

  6.                                        "User-Agent" : "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1) Gecko/20090624 Firefox/3.5",

  7.                                        "Accept" : "*/*",

  8.                                        "Accept-Encoding" : "gzip,deflate",

  9. })

  10. res = conn.getresponse()

  11. #print conn.getresponse().status

  12. print res.status

  13. print res.msg

  14. #print res.read()

  15. conn.close()



174318208.png

下面是 并发的测试~    类似 ab 和 webbench~~~~


 
 
  1. # -*- coding: utf8 -*-

  2. import threading, time, httplib

  3. HOST = "www.baidu.com"; #主机地址 例如192.168.1.101

  4. PORT = 80 #端口

  5. URI = "/?123" #相对地址,加参数防止缓存,否则可能会返回304

  6. TOTAL = 0 #总数

  7. SUCC = 0 #响应成功数

  8. FAIL = 0 #响应失败数

  9. EXCEPT = 0 #响应异常数

  10. MAXTIME=0 #最大响应时间

  11. MINTIME=100 #最小响应时间,初始值为100秒

  12. GT3=0 #统计3秒内响应的

  13. LT3=0 #统计大于3秒响应的

  14. # 创建一个 threading.Thread 的派生类

  15. class RequestThread(threading.Thread):

  16.    # 构造函数

  17.    def __init__(self, thread_name):

  18.        threading.Thread.__init__(self)

  19. self.test_count = 0

  20.    # 线程运行的入口函数

  21.    def run(self):

  22.        self.test_performace()

  23.    def test_performace(self):

  24.            global TOTAL

  25.            global SUCC

  26.            global FAIL

  27.            global EXCEPT

  28.            global GT3

  29.            global LT3

  30.            try:

  31. st = time.time()

  32. conn = httplib.HTTPConnection(HOST, PORT, False)  

  33.                conn.request('GET', URI)

  34. res = conn.getresponse()  

  35.                #print 'version:', res.version  

  36.                #print 'reason:', res.reason  

  37.                #print 'status:', res.status  

  38.                #print 'msg:', res.msg  

  39.                #print 'headers:', res.getheaders()

  40.                start_time

  41.                if res.status == 200:

  42.                    TOTAL+=1

  43.                    SUCC+=1

  44.                else:

  45.                    TOTAL+=1

  46.                    FAIL+=1

  47. timetime_span = time.time()-st

  48.                print '%s:%f\n'%(self.name,time_span)

  49.                self.maxtime(time_span)

  50.                self.mintime(time_span)

  51.                if time_span>3:

  52.                    GT3+=1

  53.                else:

  54.                    LT3+=1                    

  55.            except Exception,e:

  56.                print e

  57.                TOTAL+=1

  58.                EXCEPT+=1

  59.            conn.close()

  60.    def maxtime(self,ts):

  61.            global MAXTIME

  62.            print ts

  63.            if ts>MAXTIME:

  64. MAXTIME=ts

  65.    def mintime(self,ts):

  66.            global MINTIME

  67.            if ts<MINTIME:

  68. MINTIME=ts

  69. # main 代码开始

  70. print '===========task start==========='

  71. # 开始的时间

  72. start_time = time.time()

  73. # 并发的线程数

  74. thread_count = 300

  75. i = 0

  76. while i <= thread_count:

  77. t = RequestThread("thread" + str(i))

  78.    t.start()

  79.    i += 1

  80. t=0

  81. #并发数所有都完成或大于50秒就结束

  82. while TOTAL<thread_count|t>50:

  83.        print "total:%d,succ:%d,fail:%d,except:%d\n"%(TOTAL,SUCC,FAIL,EXCEPT)

  84.        print HOST,URI

  85.        t+=1

  86.        time.sleep(1)

  87. print '===========task end==========='

  88. print "total:%d,succ:%d,fail:%d,except:%d"%(TOTAL,SUCC,FAIL,EXCEPT)

  89. print 'response maxtime:',MAXTIME

  90. print 'response mintime',MINTIME

  91. print 'great than 3 seconds:%d,percent:%0.2f'%(GT3,float(GT3)/TOTAL)

  92. print 'less than 3 seconds:%d,percent:%0.2f'%(LT3,float(LT3)/TOTAL)


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值