Python multiple concurrent access to the website

巴扎黑
Release: 2023-03-14 18:46:02
Original
1783 people have browsed it

This article mainly introduces the function of Python to implement multiple concurrent website accesses. It analyzes the relevant operating skills of Python threads combined with URL modules to concurrently access websites based on specific examples. Friends who need it can refer to the examples in this article

Describes how Python implements multiple concurrent website access functions. Share it with everyone for your reference, the details are as follows:


# 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&#39;%4d %s Spent:%7s seconds&#39;%(i,url,span))
    file.write(u&#39;%4d %s ST:%s ET:%s Spent :%s seconds\n&#39;%(i,url,startTime,endTime,span))
  file.write(&#39;\n&#39;)
  print(u&#39;\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&#39;%(n,sumSpan,sumSpan/n,maxSpan,minSpan,over1s))
  file.write(u&#39; 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&#39;%(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(&#39;threads_data.txt&#39;,&#39;a&#39;) # save threads_data
    file.write( "%s says Hello World at time: %s\n" % (self.getName(), now))
    Process(&#39;http://222.20.6.184/main.aspx&#39;,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__==&#39;__main__&#39;:
#  file = open(&#39;threads_data.txt&#39;,&#39;w&#39;)
#  file.close()
#  file = open(&#39;data.txt&#39;,&#39;w&#39;)
#  file.close()
  for i in range(1000): # 多少次同时并发访问
    t = ThreadClass()
    t.start()
Copy after login

The above is the detailed content of Python multiple concurrent access to the website. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!