curl-and-python

Aborting HTTPPOST operation

From: Abe Music <abe_at_zaffra.com>
Date: Tue, 12 Apr 2011 21:28:36 -0500

Hi all,

I've written a small wxPython-based application that periodically scans a directory and, when new files are found, will attempt to upload those files to a remote server automatically. The application will display a small window with individual and overall progress gauges while uploading, thus to keep the GUI responsive I've set the uploading and progress handling logic in its own thread.

Last night I spent a great deal of time trying to "break" my application's expected functionality by doing things like a) turn off my remote server during upload b) turn off my wireless internet c) change the file while it's being uploaded, etc. I've found that when (a) and (b) occurs I sometimes will receive a pycurl.error exception that I can catch and respond to. The individual error codes that I've seen so far are E_RECV_ERROR and E_ABORTED_BY_CALLBACK. There may have been another one that I wasn't explicitly looking for, but I can't remember it.

When the exception occurs it's a great thing because I can catch it and update the GUI appropriately. The problem is that sometimes an exception is not thrown and the progress function continuously reports the same number of bytes have been uploaded and it never increases. I would expect this functionality in the event no exceptions were being reported, but it's an absolute must that I can detect any anomalies during the upload and let the user know what's happening. To workaround this I tried to keep tabs on how many times the progress function reports the same number of uploaded bytes and, after an arbitrary threshold, I let the user know and close the application (to stop the thread.) I would love to be able to stop the thread and start again later, but it appears that the perform() method has the scope in my thread and I can't make it stop.

Perhaps I'm not doing something correctly, but if anyone has any tips I would gladly accept and appreciate them.

Below is my pycurl setup:

            c = pycurl.Curl()
            c.setopt(pycurl.URL, UPLOAD_URL)
            c.setopt(pycurl.POST, 1)
            c.setopt(pycurl.HTTPPOST, [
                ("checksum", str(checksum)),
                ("upload", (pycurl.FORM_FILE, str(file_path))),
            ])
            c.setopt(pycurl.NOPROGRESS, 0)
            c.setopt(pycurl.PROGRESSFUNCTION, self.frame.upload_progress)
            c.setopt(pycurl.WRITEFUNCTION, self.write_handler)
            c.perform()
            c.close()

Thanks in advance,

Abe
_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-python
Received on 2011-04-13