Example of Python implementing multiple concurrent access to a web site
- 2020-06-07 04:42:06
- OfStack
This article illustrates how Python can achieve multiple concurrent access to websites. To share for your reference, specific 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'%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 The web site Url 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" % (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()
More about Python related topics: interested readers to view this site "Python process and thread skills summary", "Python Socket programming skills summary", "Python data structure and algorithm tutorial" and "Python function using techniques", "Python string skills summary", "Python introduction and advanced tutorial" and "Python file and directory skills summary"
I hope this article has been helpful in Python programming.