curl-and-python

Re: How to cancel a download?

From: <johansen_at_sun.com>
Date: Sat, 21 Feb 2009 20:23:26 -0800

On Sat, Feb 21, 2009 at 10:23:58PM -0300, V?ctor Torres Ortiz wrote:
> Hello list,
>
> I am working with pycurl, and would like to cancel a download (for
> example, with a clic button event).
>
> I start a download with this:
>
> def button_download_clicked(self, widget):
> ...
> self.D = Download(url, file, self.progress)
> self.D.start()
>
> and this is the Download class:
>
> class Download(threading.Thread):
> def __init__(self, url, target_file, progress):
> threading.Thread.__init__(self)
> self.target_file = target_file
> self.progress = progress
> self.curl = pycurl.Curl()
> self.curl.setopt(pycurl.URL, url)
> self.curl.setopt(pycurl.WRITEDATA, self.target_file)
> self.curl.setopt(pycurl.FOLLOWLOCATION, 1)
> self.curl.setopt(pycurl.NOPROGRESS, 0)
> self.curl.setopt(pycurl.PROGRESSFUNCTION, self.progress)
> self.curl.setopt(pycurl.MAXREDIRS, 5)
> self.curl.setopt(pycurl.NOSIGNAL, 1)
> self.curl.setopt(pycurl.RESUME_FROM,
> os.stat(target_file.name).st_size)
>
> def run(self):
> self.curl.perform()
> self.curl.close()
> self.target_file.close()
> self.progress(1.0, 1.0, 0, 0)
>
>
>
> There is something like self.curl.stop() ?

I would use the Multi interface instead of the easy interface. Multi
allows the caller to determine when libcurl will try to read from the
socket. Since the caller must invoke the multi interface, instead of
waiting for it to run to completion, you could cancel a download by
intermittently looking at a variable that contains the status
information.

A pycurl example that uses the multi interface is here:

http://pycurl.cvs.sourceforge.net/viewvc/pycurl/pycurl/examples/retriever-multi.py?revision=1.29&view=markup

This is the libcurl documentation for the multi interface:

http://curl.haxx.se/libcurl/c/libcurl-multi.html

There are some good examples in C available here:

http://curl.haxx.se/libcurl/c/example.html

HTH,

-j

_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-python
Received on 2009-02-22