Python实现多并发访问网站功能示例

yipeiwu_com6年前Python基础

本文实例讲述了Python实现多并发访问网站功能。分享给大家供大家参考,具体如下:

# Filename:visitweb_threads.py
# Description:python visit web, get startTime, endTime, everytimes spentTime,threading
import threading
import urllib
import time
import datetime
print 'num    web       SpentTime'
def Process(url,n):
  minSpan = 0.0
  maxSpan = 0.0
  sumSpan= 0.0
  over1s = 0
  file = open('data.txt','a') # save Data
  for i in range(n):
    startTime =datetime.datetime.now()
    try:
      urlItem = urllib.urlopen(url)
      htmSource = urlItem.read()
      urlItem.close()
    except:
      pass
    endTime = datetime.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'%4d %s Spent:%7s seconds'%(i,url,span))
    file.write(u'%4d %s ST:%s ET:%s Spent :%s seconds\n'%(i,url,startTime,endTime,span))
  file.write('\n')
  print(u'\n requested:%s times\n Total Spent:%s seconds\n avg:%s seconds\n max:%s seconds\n min:%s seconds\n over 1 secnod:%s times\n'%(n,sumSpan,sumSpan/n,maxSpan,minSpan,over1s))
  file.write(u' requested:%s times\n Total Spent:%s seconds\n avg:%s seconds\n max:%s seconds\n min:%s seconds\n over 1 secnod:%s times\n'%(n,sumSpan,sumSpan/n,maxSpan,minSpan,over1s))
  file.close()
class ThreadClass(threading.Thread):
  def run(self):
    now = datetime.datetime.now()
    print "%s says Hello World at time: %s" % (self.getName(), now)
    file = open('threads_data.txt','a') # save threads_data
    file.write( "%s says Hello World at time: %s\n" % (self.getName(), now))
    Process('http://222.20.6.184/main.aspx',10) # visit website 网站的Url和每个进程的访问次数
    now = datetime.datetime.now()
    print "%s says Goodbye at time: %s" % (self.getName(), now)
    file.write( "%s says Goodbye at time: %s\n" % (self.getName(), now))
    file.close()
if __name__=='__main__':
#  file = open('threads_data.txt','w')
#  file.close()
#  file = open('data.txt','w')
#  file.close()
  for i in range(1000): # 多少次同时并发访问
    t = ThreadClass()
    t.start()

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python进程与线程操作技巧总结》、《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

相关文章

几个适合python初学者的简单小程序,看完受益匪浅!(推荐)

几个适合python初学者的简单小程序,看完受益匪浅!(推荐)

我们在刚刚开始学习python的时候,基础部分很重要,常常要告诫自己不要好高骛远,把基础打好才是重中之重。 在写程序之前应我们要注意一个知识点: 结果是这样: 当我们使它们缩进一...

Python File(文件) 方法整理

open() 方法 Python open() 方法用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出 OSError。 注意:...

解决Python获取字典dict中不存在的值时出错问题

描述:Python2.7中如果想要获取字典中的一个值,但是这个值可能不存在,此时应该加上判断: 举个例子: t= {} if t.get('1'): # right:这种通过key来...

Pytorch抽取网络层的Feature Map(Vgg)实例

这边我是需要得到图片在Vgg的5个block里relu后的Feature Map (其余网络只需要替换就可以了) 索引可以这样获得 vgg = models.vgg19(pretra...

Python的Flask框架中使用Flask-SQLAlchemy管理数据库的教程

Python的Flask框架中使用Flask-SQLAlchemy管理数据库的教程

使用Flask-SQLAlchemy管理数据库 Flask-SQLAlchemy是一个Flask扩展,它简化了在Flask应用程序中对SQLAlchemy的使用。SQLAlchemy是一...