2013-07-25 35 views
1

我只是想发送请求,但不想浪费时间等待响应。因为这些回复对我来说毫无用处。 我抬头看python文档,但没有找到解决方案。 感谢您的任何建议。 我试图使用 urllib2.urlopen(url, timeout=0.02) 但我不能确定请求是否实际发出。使用urllib2.urlopen()加载一个URL而不等待回复

回答

4

这被称为异步加载,这里是一个blog post explaining how to do it with urllib2。示例代码:

#!/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!" 

这将加载URL而不等待响应。