在 php 中有这样几行代码:
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
我不明白它的确切含义;我认为当设置为 true 时,意味着您将获得 header 信息以及响应。但它是针对 set 方法调用的。
在 python 的 pycurl 库中,我能找到的最接近的是 pycurl.INFOTYPE_HEADER_OUT
。但当我针对 set 方法调用它时,它会抛出错误:
>>> 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
在这种情况下我必须输入的正确 python 语句是什么?
最佳答案
这不是一个设置,而是一个用于调试目的的标志,用于打印由库发送的原始 header 。要使用它,您需要将回调定义为 DEBUGFUNCTION
,然后处理返回类型:
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()
以下是 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.
关于python - pycurl.INFOTYPE_HEADER_OUT 有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11280684/