[Pycurl] Pycurl 沒辦法設定 CURLOPT_STDERR?

[Pycurl] Pycurl 沒辦法設定 CURLOPT_STDERR?

今天同事回報了一個問題,pycurl (底層是 libcurl) 遇到了一個磁碟空間不足的問題,

然後就在 stderr 那邊吐了一個錯誤訊息~

因為吐的錯誤訊息會被 redirect 到我們的記錄檔,

跟平常記錄檔的內容格式不同,想看看有沒有辦法攔截這個寫 stderr 的動作…

 

查了一下,pycurl 有 pycurl.STDERR 這個變數,

應該是用來設定 CURLOPT_STDERR 的~

問題是下面的寫法,不管怎麼設定都不會成功:

>>> curl = pycurl.Curl()
>>> curl.setopt(pycurl.STDERR, None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: invalid arguments to setopt
>>> curl.setopt(pycurl.STDERR, "")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: strings are not supported for this option
>>> fer = open("/tmp/stderr", "w")
>>> curl.setopt(pycurl.STDERR, fer)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: files are not supported for this option
>>> curl.setopt(pycurl.STDERR, fer.write)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: functions are not supported for this option
>>> curl.setopt(pycurl.STDERR, fer.fileno())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: integers are not supported for this option
>>> import cStringIO
>>> sio = cStringIO.StringIO()
>>> curl.setopt(pycurl.STDERR, sio)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: invalid arguments to setopt
>>> curl.setopt(pycurl.STDERR, sio.write)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: functions are not supported for this option

 

查了一下 pycurl 的原始碼裡的 do_curl_setopt() 函式,

這才發現裡面好像沒有處理到 CURLOPT_STDERR 這個選項,

因此不管傳什麼值來設定,都會得到 not supported 的結果…

看來要嘛等 pycurl 作者修改,要嘛就自己 patch 囉~

 

 

 

(本頁面已被瀏覽過 269 次)

3 thoughts on “[Pycurl] Pycurl 沒辦法設定 CURLOPT_STDERR?

  1. 您好~~
    我是個python 初學
    想請問看看您 在使用pycurl的時候
    是否有將process meter 存取的經驗嗎
    版主回覆:(04/21/2016 02:41:54 PM)
    您好,我有用過喔,
    我想你可以直接參考 pycurl 的文件:
    https://media.readthedocs.org/pdf/pycurl/latest/pycurl.pdf
    第 8.7.1 Example: Download/upload progress callback 節這邊~

  2. 感謝您不吝嗇的分享!!!!
    嘗試過發現似乎只提供download_t, download_d, upload_t, upload_d
    這四個變數做使用
    若需要取得curl cmd line 回覆的格式如下ex 就只能自行對那四個變數處理 請問我的認知是正確的嗎
    ex:
    curl -o test.mp4 "url" 2> test.log
    % Total % Received % Xferd Average Speed Time Time Time Current
    0 274M 0 365k 0 0 502k 0 0:09:20 –:–:– 0:09:20 502k
    版主回覆:(04/28/2016 06:06:48 AM)
    應該是,但我沒試過喔…
    另外也可以參考 https://curl.haxx.se/libcurl/c/chkspeed.html
    這裡有一些檢查下載速度的範例:
    /* check for average download speed */
    res = curl_easy_getinfo(curl_handle, CURLINFO_SPEED_DOWNLOAD, &val);
    if((CURLE_OK == res) && (val>0))
    printf("Average download speed: %0.3f kbyte/sec.n", val / 1024);

發佈回覆給「蘇辰宇」的留言 取消回覆

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料