python實現用於測試網站訪問速率的方法

來源:互聯網
上載者:User
這是python編寫的用於測試網站訪問速率的程式碼片段,可以輸出開啟某url的時間,訪問100次的平均時間,最大時間和最小時間等等

import urllib2from datetime import * import timedef Process(url,n):  minSpan = 10.0  maxSpan = 0.0  sumSpan= 0.0  over1s = 0  for i in range(n):    startTime = datetime.now()    try:      res = urllib2.urlopen(url,timeout=10)    except:      pass    endTime = datetime.now()    span = (endTime-startTime).total_seconds()    sumSpan = sumSpan + span    if span < minSpan:      minSpan = span    if span > maxSpan:      maxSpan = span    #超過一秒的    if span>1:      over1s=over1s + 1    print(u'%s Spent :%s seconds'%(url,span))  print(u'requested:%s times,Total Spent:%s seconds,avg:%s seconds, max:%s seconds,min:%s seconds,over 1 secnod:%s times'%(n,sumSpan,sumSpan/n,maxSpan,minSpan,over1s))  print('\n')if __name__=='__main__':  Process('http://www.baidu.com',100)

運行結果如下:

http://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.094 secondshttp://www.baidu.com Spent :0.016 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.032 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.032 secondshttp://www.baidu.com Spent :0.016 secondshttp://www.baidu.com Spent :0.015 secondshttp://www.baidu.com Spent :0.015 secondshttp://www.baidu.com Spent :0.016 secondshttp://www.baidu.com Spent :0.032 secondshttp://www.baidu.com Spent :0.046 secondshttp://www.baidu.com Spent :0.032 secondshttp://www.baidu.com Spent :0.094 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.109 secondshttp://www.baidu.com Spent :0.094 secondshttp://www.baidu.com Spent :0.094 secondshttp://www.baidu.com Spent :0.032 secondshttp://www.baidu.com Spent :0.062 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.047 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.032 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.015 secondshttp://www.baidu.com Spent :0.032 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.032 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.047 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.032 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.032 secondshttp://www.baidu.com Spent :0.016 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.032 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.016 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.032 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.015 secondshttp://www.baidu.com Spent :0.016 secondshttp://www.baidu.com Spent :0.016 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.015 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.032 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.032 secondshttp://www.baidu.com Spent :0.078 secondshttp://www.baidu.com Spent :0.109 secondshttp://www.baidu.com Spent :0.015 secondshttp://www.baidu.com Spent :0.094 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.032 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.109 secondshttp://www.baidu.com Spent :0.094 secondshttp://www.baidu.com Spent :0.032 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.047 secondshttp://www.baidu.com Spent :0.032 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.032 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondshttp://www.baidu.com Spent :0.031 secondsrequested:100 times,Total Spent:3.67 seconds,avg:0.0367 seconds, max:0.109 seconds,min:0.015 seconds,over 1 secnod:0 times

希望本文所述對大家的Python程式設計有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.