Linux——curl

 

window下安装

下载 curl for Windows

将 bin 目录添加到环境变量 Path:

查看版本:curl --version

测试:curl www.baidu.com

 

超时参数

使用CURL时,有两个超时时间:

  • 一个是连接超时时间(用--connect-timeout参数来指定)
  • 一个是数据传输的最大允许时间(用--max-time参数来指定)

 

–connect-timeout 选项

curl --connect-timeout 5 https://linuxhandbook.com

使用 ‘5’ 和 ‘–connect-timeout’ 标志意味着 cURL 将尝试连接到 ‘linuxhandbook.com’,如果超过 5 秒,连接将被终止。

连接超时的话,出错提示形如:

curl: (28) connect() timed out!

 

–max-time 选项

$ curl --max-time 20 https://github.com/aristocratos/btop/releases/download/v1.2.3/btop-aarch64-linux-musl.tbz

这个 cURL 使用示例将下载一个文件。当“连接服务器”+“下载文件时间”的总时间大于我们这里指定的 20 秒时,将终止下载。

数据传输的最大允许时间超时的话,出错提示形如:

curl: (28) Operation timed out after 2000 milliseconds with 0 bytes received

 

-w参数

curl的-w参数用于在一次完整且成功的操作后输出指定格式的内容到标准输出。

输出格式由普通字符串和任意数量的变量组成,输出变量需要按照%{variable_name}的格式,如果需要输出%,double一下即可,即%%,同时,\n是换行,\r是回车,\t是TAB。curl会用合适的值来替代输出格式中的变量。

  • 变量说明
    • url_effective:最终获取的url地址,尤其是当你指定给curl的地址存在301跳转,且通过-L继续追踪的情形。
    • http_codehttp状态码,如200成功,301转向,404未找到,500服务器错误等。
    • http_connectThe 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_namelookupDNS解析时间,从请求开始到DNS解析完毕所用时间。
    • time_connect连接时间,从开始到建立TCP连接完成所用时间,包括前边DNS解析时间,如果需要单纯的得到连接时间,用这个time_connect时间减去前边time_namelookup时间。以下同理,不再赘述。
    • time_appconnect连接建立完成时间,如SSL/SSH等建立连接或者完成三次握手时间。
    • time_pretransfer从开始到准备传输的时间。
    • time_redirect重定向时间,包括到最后一次传输前的几次重定向的DNS解析,连接,预传输,传输时间。
    • time_starttransfer开始传输时间。在发出请求之后,Web 服务器返回数据的第一个字节所用的时间
    • size_download下载大小。
    • size_upload上传大小。
    • size_header下载的header的大小
    • size_request请求的大小。
    • speed_download下载速度,单位-字节每秒。
    • speed_upload上传速度,单位-字节每秒。
    • content_type就是content-Type,不用多说了,这是一个访问我博客首页返回的结果示例(text/html; charset=UTF-8);(The Content-Type of the requested document, if there was any.)
    • num_connectsNumber of new connects made in the recent transfer. (Added in 7.12.3)
    • num_redirectsNumber of redirects that were followed in the request. (Added in 7.12.3)
    • redirect_urlWhen 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_pathThe initial path libcurl ended up in when logging on to the remote FTP server. (Added in 7.15.4)
    • ssl_verify_resultssl认证结果,返回0表示认证成功。
  • 案例1

取得HTTP返回的状态码:

curl -I -m 10 -o /dev/null -s -w %{http_code} www.baidu.com

说明:

  • -I 仅测试HTTP头
  • -m 10 最多查询10s
  • -o /dev/null 屏蔽原有输出信息
  • -s silent 模式,不输出任何东西
  • -w %{http_code} 控制额外输出

shell 脚本:

result_code=`curl -I -m 10 -o /dev/null -s -w %{http_code} www.baidu.com`
echo $result_code
  • 案例2

模板文件:

[root@k8smaster01 ~]# cat template.txt 
\n----------------------------------------------------\n
       url_effective: %{url_effective} \n
           http_code: %{http_code} \n
        http_connect: %{http_connect} \n
       size_download: %{size_download} bytes \n
         size_upload: %{size_upload} bytes \n
         size_header: %{size_header} bytes \n
        size_request: %{size_request} bytes \n
      speed_download: %{speed_download} bytes/s \n
        speed_upload: %{speed_upload} bytes/s \n
        content_type: %{content_type} \n
        num_connects: %{num_connects} \n
       num_redirects: %{num_redirects} \n
        redirect_url: %{redirect_url} \n
      ftp_entry_path: %{ftp_entry_path} \n
   ssl_verify_result: %{ssl_verify_result} \n
        time_connect: %{time_connect} s \n
     time_appconnect: %{time_appconnect} s \n
    time_pretransfer: %{time_pretransfer} s \n
       time_redirect: %{time_redirect} s \n
  time_starttransfer: %{time_starttransfer} s \n
                 dns: %{time_namelookup} s \n
------------------------------------------------------\n
          time_total: %{time_total} s \n
[root@k8smaster01 ~]#

案例使用:

[root@k8smaster01 ~]# curl -w "@template.txt" http://192.168.23.1:8080/demo/wx/address/list

 

 

引用:

  • https://blog.csdn.net/zhaikaiyun/article/details/117467144
  • https://blog.csdn.net/u013690521/article/details/52598731
  • https://www.jianshu.com/p/07c4dddae43a
  • https://blog.csdn.net/whatday/article/details/121342941

posted on 2022-08-06 19:04  曹伟雄  阅读(419)  评论(0编辑  收藏  举报

导航