Access Web pages through Python multithreading
@for &ever 2010-07-03
Use Python to access Web pages in a multi-threaded fashion.
A simple small example, access to three different pages, each page open 20 threads concurrent access.
The Python script is as follows:
#!/usr/bin/env python #-*-coding:utf-8-*-' Created on 2010-7-2 @author: Forever ' ' Import sys import threading fro M urllib import Urlopen urls = ["/archive/2010/07/03/5711257.aspx", "/archive/2010/06/28/5699311.aspx", "/archive/2010 /07/03/5711270.aspx ",] visittimesperpage = def usage (): print ' usage: ', sys.argv[0], ' Host ' def Main (argv): host = arg V[1] if host = = ': Usage () sys.exit (2) else:for I in Range (visittimesperpage): for URL in urls:visitpagethread = VISITP Agethread (url + str (i), host, URL) Visitpagethread.start () class Visitpagethread (threading. Thread): Def __init__ (self, threadname, host, URL): Threading. Thread.__init__ (self, name = ThreadName) Self.host = host Self.url = URL def run (self): url = self.host + self.url try:do c = Urlopen (URL). Read () #print doc except Exception, E:print "Urlopen Exception:%s"%e if __name__== ' __main__ ': sys.arg V.append (' Http://blog.csdn.net/forandever ') Main (SYS.ARGV)