我只想发送请求,但不想浪费时间等待响应。因为回复对我没用。
我查阅了 python 文档,但没有找到解决方案。
感谢您的任何建议。
我试过使用
urllib2.urlopen(url, timeout=0.02)
但是我不确定请求是否真的发出了。
最佳答案
这叫做异步加载,这里是一个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。
关于python - 使用 urllib2.urlopen() 加载 URL 而无需等待响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17863457/