There are some limitations to some of the options which can be used concurrently with the pycurl callbacks compared to the libcurl callbacks. This is to allow different callback functions to be associated with different Curl objects. More specifically, WRITEFUNCTION cannot be used with WRITEDATA, READFUNCTION cannot be used with READDATA, HEADERFUNCTION cannot be used with WRITEHEADER, PROGRESSFUNCTION cannot be used with PROGRESSDATA, PASSWDFUNCTION cannot be used with PASSWDDATA and DEBUGFUNCTION cannot be used with DEBUGDATA. In practice, these limitations can be overcome by having a callback function be a class method and rather use the class attributes to store per object data used in the callbacks.
The signature of each callback used in pycurl is as follows:## Callback function invoked when body data is ready def body(buf): # Print body data to stdout import sys sys.stdout.write(buf) # Returning None implies that all bytes were written ## Callback function invoked when header data is ready def header(buf): # Print header data to stderr import sys sys.stderr.write(buf) # Returning None implies that all bytes were written c = pycurl.Curl() c.setopt(pycurl.URL, 'http://www.python.org/') c.setopt(pycurl.WRITEFUNCTION, body) c.setopt(pycurl.HEADERFUNCTION, header) c.perform()
## Callback function invoked when download/upload has progress def progress(download_t, download_d, upload_t, upload_d): print 'Total to download', download_t print 'Total downloaded', download_d print 'Total to upload', upload_t print 'Total uploaded', upload_d c.setopt(c.URL, 'http://slashdot.org/') c.setopt(c.NOPROGRESS, 0) c.setopt(c.PROGRESSFUNCTION, progress) c.perform()
def test(debug_type, debug_msg): print "debug(%d): %s" % (debug_type, debug_msg) c = pycurl.Curl() c.setopt(pycurl.URL, 'http://curl.haxx.se/') c.setopt(pycurl.VERBOSE, 1) c.setopt(pycurl.DEBUGFUNCTION, test) c.perform()