如何将cURL详细输出传递给日志记录模块?

时间:2017-11-08 18:28:47

标签: python curl

有谁知道如何或者是否可以将cURL详细输出重定向到日志记录模块?现在详细输出都是stdout,但我想将它传递给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())

谢谢!

1 个答案:

答案 0 :(得分:2)

curl有一个DEBUGFUNCTION回调选项,其工作方式与WRITEFUNCTION选项类似,不同之处在于它使用VERBOSE输出而不是响应主体调用。

official documentation有参考信息和简短示例,您应该能够适应您的需求:

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()
相关问题