This article mainly describes the Python implementation of multiple concurrent access to the site features, combined with specific examples of the Python thread combined with the URL module concurrent access to the site's related operations skills, the need for friends can refer to the following
This example describes Python's ability to implement multiple concurrent access sites. Share to everyone for your reference, as follows:
# 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): min Span = 0.0 Maxspan = 0.0 sumspan= 0.0 over1s = 0 File = open (' Data.txt ', ' a ') # Save data for I in range (n): Startt IME =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 = su Mspan + 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 Secon ds\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,maxS Pan,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 (), no W) file = open (' Threads_data.txt ', ' a ') # Save Threads_data file.write ("%s says Hello World at time:%s\n"% (self.g Etname (), now)) process (' http://222.20.6.184/main.aspx ') # visit website The URL of the site and the number of visits per process now = Datetime.datetime . Now () print '%s says Goodbye at time:%s '% (Self.getname (), now) File.write ('%s says Goodbye at time:%s\n '% (SE Lf.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 times concurrent access T = ThreadClass() T.start ()