Python多並發訪問網站

來源:互聯網
上載者:User
這篇文章主要介紹了Python實現多並發訪問網站功能,結合具體執行個體形式分析了Python線程結合URL模組並發訪問網站的相關操作技巧,需要的朋友可以參考下

本文執行個體講述了Python實現多並發訪問網站功能。分享給大家供大家參考,具體如下:


# Filename:visitweb_threads.py# Description:python visit web, get startTime, endTime, everytimes spentTime,threadingimport threadingimport urllibimport timeimport datetimeprint '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()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.