Python-使用HTTPS的urllib2异步/线程请求示例 [英] Python - Example of urllib2 asynchronous / threaded request using HTTPS

查看:345
本文介绍了Python-使用HTTPS的urllib2异步/线程请求示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Python的urllib2来获取异步/线程化HTTPS请求非常麻烦.

I'm having a heck of a time getting asynchronous / threaded HTTPS requests to work using Python's urllib2.

那里是否有人有实现urllib2.Request,urllib2.build_opener和urllib2.HTTPSHandler的子类的基本示例?

Does anyone out there have a basic example that implements urllib2.Request, urllib2.build_opener and a subclass of urllib2.HTTPSHandler?

谢谢!

推荐答案

下面的代码同时异步执行7个HTTP请求. 它不使用线程,而是使用带有 twisted 库的异步联网.

The code below does 7 http requests asynchronously at the same time. It does not use threads, instead it uses asynchronous networking with the twisted library.

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()

这篇关于Python-使用HTTPS的urllib2异步/线程请求示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆