重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
创新互联www.cdcxhl.cn八线动态BGP香港云服务器提供商,新人活动买多久送多久,划算不套路!
创新互联公司是专业的盖州网站建设公司,盖州接单;提供成都做网站、成都网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行盖州网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!小编给大家分享一下利用Python对网站进行测速的方法,相信大部分人都还不怎么了解,因此分享这篇文章给大家学习,希望大家阅读完这篇文章后大所收获,下面让我们一起去学习方法吧!
利用python可以编写的用于测试网站访问速率的代码,实现原理是输出打开某url的时间,并计算出访问100次的平均时间,大时间和最小时间等等。根据时间的变化判断网站速度。
完整代码:
import urllib2 from datetime import * import time def 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 seconds http://www.baidu.com Spent :0.094 seconds http://www.baidu.com Spent :0.016 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.016 seconds http://www.baidu.com Spent :0.015 seconds http://www.baidu.com Spent :0.015 seconds http://www.baidu.com Spent :0.016 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.046 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.094 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.109 seconds http://www.baidu.com Spent :0.094 seconds http://www.baidu.com Spent :0.094 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.062 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.047 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.015 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.047 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.016 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.016 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.015 seconds http://www.baidu.com Spent :0.016 seconds http://www.baidu.com Spent :0.016 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.015 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.078 seconds http://www.baidu.com Spent :0.109 seconds http://www.baidu.com Spent :0.015 seconds http://www.baidu.com Spent :0.094 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.109 seconds http://www.baidu.com Spent :0.094 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.047 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.032 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds http://www.baidu.com Spent :0.031 seconds requested: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对网站进行测速的方法的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联-成都网站建设公司行业资讯频道!