Python - 使用HTTPS的urllib2异步/多线程请求示例

18 投票
5 回答
26755 浏览
提问于 2025-04-16 16:32

我在用Python的urllib2库进行异步或多线程的HTTPS请求时遇到了很多麻烦。

有没有人能提供一个简单的例子,展示如何使用urllib2.Request、urllib2.build_opener和urllib2.HTTPSHandler的子类?

谢谢!

5 个回答

5

这里有一个例子,使用了urllib2(支持https)和线程。每个线程会循环遍历一个网址列表,并获取这些网址的资源。

import itertools
import urllib2
from threading import Thread


THREADS = 2
URLS = (
    'https://foo/bar',
    'https://foo/baz',
    )


def main():
    for _ in range(THREADS):
        t = Agent(URLS)
        t.start()


class Agent(Thread):
    def __init__(self, urls):
        Thread.__init__(self)
        self.urls = urls

    def run(self):
        urls = itertools.cycle(self.urls)
        while True:
            data = urllib2.urlopen(urls.next()).read()


if __name__ == '__main__':
    main()
8

有一种非常简单的方法,可以使用urllib2的处理器,你可以在这里找到相关内容:http://pythonquirks.blogspot.co.uk/2009/12/asynchronous-http-request.html

#!/usr/bin/env python

import urllib2
import threading

class MyHandler(urllib2.HTTPHandler):
    def http_response(self, req, response):
        print "url: %s" % (response.geturl(),)
        print "info: %s" % (response.info(),)
        for l in response:
            print l
        return response

o = urllib2.build_opener(MyHandler())
t = threading.Thread(target=o.open, args=('http://www.google.com/',))
t.start()
print "I'm asynchronous!"

t.join()

print "I've ended!"
10

下面的代码同时发起了7个HTTP请求,都是异步进行的。它没有使用线程,而是利用了twisted这个库来进行异步网络操作。

from twisted.web import client
from twisted.internet import reactor, defer

urls = [
 'http://www.python.org', 
 'http://stackoverflow.com', 
 'http://www.twistedmatrix.com', 
 'http://www.google.com',
 'http://launchpad.net',
 'http://github.com',
 'http://bitbucket.org',
]

def finish(results):
    for result in results:
        print 'GOT PAGE', len(result), 'bytes'
    reactor.stop()

waiting = [client.getPage(url) for url in urls]
defer.gatherResults(waiting).addCallback(finish)

reactor.run()

撰写回答