顧名思義,write-out的作用就是輸出點什么。curl的-w參數用於在一次完整且成功的操作后輸出指定格式的內容到標准輸出。
輸出格式由普通字符串和任意數量的變量組成,輸出變量需要按照 %{variable_name} 的格式,如果需要輸出%,double一下即可,即 %% ,同時,\n 是換行,\r 是回車,\t 是TAB。curl會用合適的值來替代輸出格式中的變量,所有可用變量如下:
url_effective 最終獲取的url地址,尤其是當你指定給curl的地址存在301跳轉,且通過-L繼續追蹤的情形。
http_code http狀態碼,如200成功, 301轉向, 404未找到, 500服務器錯誤等。
http_connect The numerical code that was found in the last response (from a proxy) to a curl CONNECT request. (Added in 7.12.4)
time_total 總時間,按秒計。精確到小數點后三位。
time_namelookup DNS解析時間, 從請求開始到DNS解析完畢所用時間。
time_connect 連接時間, 從開始到建立TCP連接完成所用時間, 包括前邊DNS解析時間,如果需要單純的得到連接時間,用這個time_connect時間減去前邊time_namelookup時間。
time_appconnect 連接建立完成時間,如SSL/SSH等建立連接或者完成三次握手時間。
time_pretransfer 從開始到准備傳輸的時間。
time_redirect 重定向時間,包括到最后一次傳輸前的幾次重定向的DNS解析,連接,預傳輸,傳輸時間。
time_starttransfer 開始傳輸時間。在 發出請求 之后,Web 服務器 返回數據的第一個字節 所用的時間.
size_download 下載大小。(The total amount of bytes that were downloaded.)
size_upload 上傳大小。(The total amount of bytes that were uploaded.)
size_header下載的header的大小(The total amount of bytes of the downloaded headers.)
size_request 請求的大小。(The total amount of bytes that were sent in the HTTP request.)
speed_download 下載速度,單位-字節每秒。
speed_upload 上傳速度,單位-字節每秒。(The average upload speed that curl measured for the complete upload. Bytes per second.)
content_type 就是content-Type,不多說,結果示例(text/html; charset=UTF-8)。
num_connects Number of new connects made in the recent transfer. (Added in 7.12.3)
num_redirects Number of redirects that were followed in the request. (Added in 7.12.3)
redirect_url When a HTTP request was made without -L to follow redirects, this variable will show the actual URL a redirect would take you to. (Added in 7.18.2)
ftp_entry_path The initial path libcurl ended up in when logging on to the remote FTP server. (Added in 7.15.4)
ssl_verify_result ssl認證結果,返回0表示認證成功。
注意:
1、若多次使用-w參數,按最后一個的格式輸出。
2、在使用上面變量的時候,注意看后面小括號中的 Added in XXX,這個表示支持該變量curl所需的最低版本,查看curl版本使用curl -V。如果版本不夠,curl會提示類似下面的錯誤。
curl: unknown --write-out variable: 'redirect_url'
curl -w舉例
檢查一批URL的HTTP狀態:
cat url.txt|while read line;
do curl -I $line -m 5 --connect-timeout 5 -o /dev/null -s -w "$line "%{http_code}"\n";
done>ok.txt
取URL返回狀態碼:curl -s -m 10 -o /dev/null -w %{http_code} https://www.baidu.com