【问题标题】:How to tell a curl max-time apart from a connect-timeout?如何区分 curl 最大时间和连接超时?
【发布时间】:2015-08-12 09:44:36
【问题描述】:

示例命令

curl -s -w "%{http_code} %{http_connect}" --connect-timeout 10 --max-time 50

将返回000 000 用于达到连接超时和达到最大时间。区分这两个错误的最佳方法是什么?

据我所知,唯一的区别是 -s 标志被删除的时间:

  • 连接超时返回curl: (28) connect() timed out
  • 最大超时返回curl: (28) Operation timed out

【问题讨论】:

  • %{time_total} 和/或%{time_connect} 帮忙?
  • 在 n 秒内没有响应的连接上(触发--connect-timeout)我注意到curl 的详细输出中缺少 HTTP 状态代码。相反,通过--max-time 阈值的已建立连接将包含此信息。您能否对详细输出进行一些检查?
  • @Lewis Norton:如果--connect-timeout 被触发,则没有 HTTP 状态,因为一开始就没有连接。但是,如果--max-time 不一定意味着您将拥有一个状态,因为连接可以在建立后切断,但在收到第一个 HTTP 响应之前。
  • @lcd047 实际上非常正确。我没想到。
  • 你有没有找到更好的方法来区分这两种故障?

标签: bash unix curl


【解决方案1】:

你的问题有两点:

  • HTTP 代码
  • 区分差异

HTTP 代码

下单失败时不会有http码。因为没有响应。只有下单成功时才能获取http码。

区分差异

您可以按照以下顺序获取错误。

result=`curl  --connect-timeout $connectiontimeout --max-time $maxtimeout -s -S -X POST -H 'Content-Type: text/plain' -d "$DATA" "$resturl" 1>&1 2>&1`

if [ "$result" = "curl: (28) connect() timed out" ] ;then
    echo "curl: (28) connect() timed out"

fi

然后就可以通过判断结果来区分这两个错误了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 2014-01-29
    • 2014-11-19
    • 1970-01-01
    • 2015-10-31
    相关资源
    最近更新 更多