Search code examples
pythonpycurl

what is the use of pycurl.INFOTYPE_HEADER_OUT?


In php there are lines of code like:

curl_setopt($ch, CURLINFO_HEADER_OUT, true);

I don't get the exact meaning of it; I assume it is something when set to true means you get the header information along with the response. But it is called against the set methods.

In python's pycurl library the closest I could get to was pycurl.INFOTYPE_HEADER_OUT. But it throws error when I call it against set methods:

>>> c = pycurl.Curl()
>>> c.setopt(pycurl.INFOTYPE_HEADER_OUT, 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
pycurl.error: (48, '')
>>> c.getinfo(pycurl.INFOTYPE_HEADER_OUT)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid argument to getinfo

What is the correct python statement I have to type in this case?


Solution

  • This is not a setting, but rather a flag that is used for debugging purposes to print the raw headers that are sent out by the library. To use it you need to define a callback as your DEBUGFUNCTION and then process the return type:

    sent_headers = []
    received_headers = []
    def collect_headers(debug_type, debug_msg):
        if debug_type == pycurl.INFOTYPE_HEADER_OUT:
                sent_headers.append(debug_msg)
        if debug_type == pycurl.INFOTYPE_HEADER_IN:
                received_headers.append(debug_msg)
    
    # somewhere later in the code
    
    c = pycurl.Curl()
    c.setopt(pycurl.URL, 'http://www.google.com/')
    c.setopt(pycurl.DEBUGFUNCTION, collect_headers)
    c.perform()
    

    Here is the relevant bits from the documentation:

    CURLOPT_DEBUGFUNCTION

    Pass a pointer to a function that matches the following prototype: int curl_debug_callback (CURL *, curl_infotype, char *, size_t, void *); CURLOPT_DEBUGFUNCTION replaces the standard debug function used when CURLOPT_VERBOSE is in effect. This callback receives debug information, as specified with the curl_infotype argument. This function must return 0. The data pointed to by the char * passed to this function WILL NOT be zero terminated, but will be exactly of the size as told by the size_t argument.

    Available curl_infotype values:

    CURLINFO_TEXT

    The data is informational text.

    CURLINFO_HEADER_IN

    The data is header (or header-like) data received from the peer.

    CURLINFO_HEADER_OUT

    The data is header (or header-like) data sent to the peer.

    CURLINFO_DATA_IN

    The data is protocol data received from the peer.

    CURLINFO_DATA_OUT

    The data is protocol data sent to the peer.