如何将 cURL 详细输出通过管道传输到日志记录模块?

Spa*_*eak 2 python curl

有谁知道如何或是否可以将 cURL 详细输出重定向到日志记录模块?现在,详细输出全部发送到标准输出,但我想将其通过管道传输到正在保存到文件中的logging.debug。

请注意,我不想将所有控制台输出通过管道传输到日志记录。另外,我确实有一个字符串缓冲区来捕获 pycurl.WRITEFUNCTION 但是它似乎没有捕获 VERBOSE 输出。

当前代码:

logging.debug('starting setopt_put for url: %s',api_url)
self._podium_curl_setopt_base(api_url.url + api_args)
self._podium_curl.setopt(pycurl.HTTPGET, 1)
self._podium_curl.setopt(pycurl.WRITEFUNCTION, self._string_buffer.write)
self._podium_curl.setopt(pycurl.VERBOSE, True)
self._podium_curl.perform()
logging.info(_string_buffer.getvalue())
Run Code Online (Sandbox Code Playgroud)

谢谢!

Wan*_*uta 5

curl 有一个DEBUGFUNCTION回调选项,其工作方式与该选项类似WRITEFUNCTION,只不过它是使用 VERBOSE 输出而不是响应正文来调用的。

官方文档提供了参考信息以及一个简短的示例,您应该能够适应您的需求:

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()
Run Code Online (Sandbox Code Playgroud)