<commit_before><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title></title>
</head>
<body>
<h2>Callbacks</h2> For more fine-grained control, libcurl allows a
number of callbacks to be associated with each connection. &nbsp;In
pycurl, callbacks are defined using the <b>setopt()</b> method for
Curl objects with options WRITEFUNCTION, READFUNCTION, HEADERFUNCTION,
PROGRESSFUNCTION, PASSWDFUNCTION or DEBUGFUNCTION. These options
correspond to the libcurl options with CURLOPT_* prefix removed.  A
callback in pycurl must be either a regular Python function, a class
method or an extension type function.<br>
<br>
The signature of each callback used in pycurl is as follows:<br>
<br>
<b>WRITEFUNCTION(</b><i>string</i><b>) </b><i>-&gt; number of bytes written<br>
</i>
<br>
<b>READFUNCTION(</b><i>number of bytes to read</i><b>)<i> </i></b><i>-&gt;
string</i><br>
<br>
<b>HEADERFUNCTION(</b><i>string</i><b>)</b><i> -&gt; number of bytes written<br>
</i><br>
<b>PROGRESSFUNCTION(</b><i>download total, downloaded, upload total, uploaded</i><b>) </b><i>-&gt; status</i><br>
<br>
<b>PASSWDFUNCTION(</b><i>prompt string, max number of bytes to read</i><b>)</b>
<i>-&gt; string</i><br>
<br>
<b>DEBUGFUNCTION(</b><i>debug message type, debug message</i><b>)</b>
<i>-&gt; None<br></i>
<br>
<hr>
<h3>Example: Callbacks for document header and body</h3>

This example prints the header data to stderr and the body data to
stdout.  Also note that neither callback returns the number of bytes
written.  For WRITEFUNCTION and HEADERFUNCTION callbacks, returning
None implies that all bytes where written.

<pre>
    ## 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()
</pre>
</blockquote>

<h3>Example: Download/upload progress callback</h3>

This example shows how to use the progress callback.  When downloading
a document, the arguments related to uploads are zero, and vice versa.

<pre>
    ## 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)<commit_msg>sync with the web pages<commit_after><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title></title>
</head>
<body>
<h2>Callbacks</h2> For more fine-grained control, libcurl allows a
number of callbacks to be associated with each connection. &nbsp;In
pycurl, callbacks are defined using the <b>setopt()</b> method for
Curl objects with options WRITEFUNCTION, READFUNCTION, HEADERFUNCTION,
PROGRESSFUNCTION, PASSWDFUNCTION or DEBUGFUNCTION. These options
correspond to the libcurl options with CURLOPT_* prefix removed.  A
callback in pycurl must be either a regular Python function, a class
method or an extension type function.<br>

<p>
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 member function and rather use the class
attributes to store per object data used in the callbacks.
</p>

The signature of each callback used in pycurl is as follows:<br>
<br>
<b>WRITEFUNCTION(</b><i>string</i><b>) </b><i>-&gt; number of characters written<br>
</i>
<br>
<b>READFUNCTION(</b><i>number of characters to read</i><b>)<i> </i></b><i>-&gt;
string</i><br>
<br>
<b>HEADERFUNCTION(</b><i>string</i><b>)</b><i> -&gt; number of characters written<br>
</i><br>
<b>PROGRESSFUNCTION(</b><i>download total, downloaded, upload total, uploaded</i><b>) </b><i>-&gt; status</i><br>
<br>
<b>PASSWDFUNCTION(</b><i>prompt string, max number of characters to read</i><b>)</b>
<i>-&gt; string</i><br>
<br>
<b>DEBUGFUNCTION(</b><i>debug message type, debug message string</i><b>)</b>
<i>-&gt; None<br></i>
<br>
<hr>
<h3>Example: Callbacks for document header and body</h3>

This example prints the header data to stderr and the body data to
stdout.  Also note that neither callback returns the number of bytes
written.  For WRITEFUNCTION and HEADERFUNCTION callbacks, returning
None implies that all bytes where written.

<pre>
    ## 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()
</pre>
</blockquote>

<h3>Example: Download/upload progress callback</h3>

This example shows how to use the progress callback.  When downloading
a document, the arguments related to uploads are zero, and vice versa.

<pre>
    ## 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)
{'fraction_non_alphanumeric': 0.1169671752196024, 'fraction_numerical': 0.0033903529049160116, 'mean_word_length': 4.255060728744939, 'pattern_counts': {'":': 0, '<': 176, '<?xml version=': 0, '>': 176, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '464318', 'n_tokens_mistral': 2072, 'n_tokens_neox': 2013, 'n_words': 751}