Python provides examples of multiple concurrent Website access functions and python Website Functions
This example describes how to implement the multi-concurrent Website access function in Python. We will share this with you for your reference. The details are as follows:
# Filename: visitweb_threads.py # Description: python visit web, get startTime, endTime, everytimes spentTime, 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 () handle T: 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: % 7 s 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 and the number of visits to each process 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): # How many concurrent accesses t = ThreadClass () t. start ()