bash 如何区分 curl 最大时间和连接超时?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30531033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:04:58  来源:igfitidea点击:

How to tell a curl max-time apart from a connect-timeout?

bashunixcurl

提问by vgm

Example command

示例命令

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

Will return 000 000for both the connect-timeout being reached and the max-time being reached. What is the best way to tell these two errors apart?

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

The only difference, as far as I can see, is when the -sflag is removed:

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

  • Connection timeout returns curl: (28) connect() timed outand
  • Max timeout returns curl: (28) Operation timed out
  • 连接超时返回curl: (28) connect() timed out
  • 最大超时返回 curl: (28) Operation timed out

回答by SeanYan

There are two points in your question:

你的问题有两点:

  • HTTP CODE
  • Distinguish the difference
  • 代码
  • 区分差异

HTTP CODE

代码

When the order is failed, there will be no http code.Because there is no reponse.You can acquire the http code only when the order is success.

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

Distinguish the difference

区分差异

You can gain the error by the follow order.

您可以通过以下顺序获得错误。

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

Then you can judge the result to distinguish the two error.

然后你可以判断结果来区分这两个错误。