[Python] python编写的用于测试网站访问速率的代码片段 →→→→→进入此内容的聊天室

来自 , 2019-02-28, 写在 Python, 查看 171 次.
URL http://www.code666.cn/view/f4612859
  1. def Process(url,n):
  2.     minSpan = 10.0
  3.     maxSpan = 0.0
  4.     sumSpan= 0.0
  5.     over1s = 0
  6.     for i in range(n):
  7.         startTime = datetime.datetime.now()
  8.         try:
  9.             res = urllib2.urlopen(url,timeout=10)
  10.         except:
  11.             pass
  12.         endTime = datetime.datetime.now()
  13.         span = (endTime-startTime).total_seconds()
  14.         sumSpan = sumSpan + span
  15.         if span < minSpan:
  16.             minSpan = span
  17.         if span > maxSpan:
  18.             maxSpan = span
  19.         #超过一秒的
  20.         if span>1:
  21.             over1s=over1s + 1
  22.         print(u'%s Spent :%s seconds'%(url,span))
  23.     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))
  24.     print('\n')
  25.  
  26.  
  27. if __name__=='__main__':
  28.     Process('http://www.baidu.com',100)
  29. #//python/5983

回复 "python编写的用于测试网站访问速率的代码片段"

这儿你可以回复上面这条便签

captcha