Access Web pages through Python multithreading

Source: Internet
Author: User
Tags python script
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.