Skip to content

Instantly share code, notes, and snippets.

@dokipen
Created February 9, 2012 20:23
Show Gist options
  • Select an option

  • Save dokipen/1782820 to your computer and use it in GitHub Desktop.

Select an option

Save dokipen/1782820 to your computer and use it in GitHub Desktop.
crap
#!/usr/bin/python
import os
import tempfile
import tornado.httpclient
import tornado.curl_httpclient
import tornado.ioloop
class HttpDownload(object):
def __init__(self, url, ioloop):
self.ioloop = ioloop
self.tempfile = tempfile.NamedTemporaryFile(delete=False)
req = tornado.httpclient.HTTPRequest(
url = url,
streaming_callback = self.streaming_callback)
http_client = tornado.curl_httpclient.CurlAsyncHTTPClient()
#http_client = tornado.httpclient.AsyncHTTPClient()
http_client.fetch(req, self.async_callback)
def streaming_callback(self, data):
print data
self.tempfile.write(data)
def async_callback(self, response):
print response
self.tempfile.flush()
self.tempfile.close()
if response.error:
print "Failed"
os.unlink(self.tempfile.name)
else:
print("Success: %s" % self.tempfile.name)
self.ioloop.stop()
def main():
ioloop = tornado.ioloop.IOLoop.instance()
dl = HttpDownload("http://zenhabits.net/2009/06/9-ways-to-twitter-your-personal-development/", ioloop)
ioloop.start()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment