欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實現(xiàn)多并發(fā)訪問網(wǎng)站功能示例

 更新時間:2017年06月19日 11:03:47   作者:微煙波  
這篇文章主要介紹了Python實現(xiàn)多并發(fā)訪問網(wǎng)站功能,結(jié)合具體實例形式分析了Python線程結(jié)合URL模塊并發(fā)訪問網(wǎng)站的相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了Python實現(xiàn)多并發(fā)訪問網(wǎng)站功能。分享給大家供大家參考,具體如下:

# 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 網(wǎng)站的Url和每個進程的訪問次數(shù)
    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): # 多少次同時并發(fā)訪問
    t = ThreadClass()
    t.start()

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python進程與線程操作技巧總結(jié)》、《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設(shè)計有所幫助。

相關(guān)文章

最新評論