curl命令基本使用小总结

curl 下载文件

  • -o:将文件保存为命令行中指定的文件名的文件中
  • -O:使用URL中默认的文件名保存文件到本地
  • --limit-rate:限速
  • -C:对大文件使用断点续传功能
  • --silent:不显示进度信息
# 当文件在下载完成之前结束该进程
[root@ ~]# curl -O http://www.gnu.org/software/gettext/manual/gettext.html
###### ########             20.1%

# 通过添加-C选项继续对该文件进行下载,已经下载过的文件不会被重新下载
[root@ ~]# curl -C - -O http://www.gnu.org/software/gettext/manual/gettext.html
###### #########            21.1%

认证授权

在访问需要授权的页面时,可通过-u选项提供用户名和密码进行授权

[root@ ~]# curl -u username:password URL
通常的做法是在命令行只输入用户名,之后会提示输入密码,这样可以保证在查看历史记录时不会将密码泄露
[root@ ~]# curl -u username URL

传递请求数据

默认curl使用GET方式请求数据,可以通过 --data/-d 方式指定使用POST方式传递数据

# GET
[root@ ~]#curl -u username https://XXXXXXXXXX

# POST
[root@ ~]# curl -u username --data "param1=value1&param2=value" https://XXXXXXXXXX

# 也可以指定一个文件,将该文件中的内容当作数据传递给服务器端
[root@ ~]# curl --data @filename https://XXXXXXXXXX

注:默认情况下,通过POST方式传递过去的数据中若有特殊字符,首先需要将特殊字符转义在传递给服务器端,如value值中包含有空格,则需要先将空格转换成%20,如:
[root@ ~]# curl -d "value%201" http://hostname.com

在新版本的CURL中,提供了新的选项 --data-urlencode,通过该选项提供的参数会自动转义特殊字符。
[root@ ~]# curl --data-urlencode "value 1" https://XXXXXXXXXX

除了使用GET和POST协议外,还可以通过 -X 选项指定其它协议,如:
[root@ ~]# curl -I -X DELETE https://XXXXXXXXXX

上传文件
[root@ ~]#  curl --form "[email protected]" https://XXXXXXXXXX
 

curl 查看响应时间

[root@ ~]# curl -o /dev/null -s -w "time_namelookup: "%{time_namelookup}"\ntime_connect: "%{time_connect}"\ntime_appconnect: "%{time_appconnect}"\ntime_pretransfer: "%{time_pretransfer}"\ntime_starttransfer: "%{time_starttransfer}"\ntime_total: "%{time_total}"\n" "https://www.google.com"  

time_namelookup: 0.005
time_connect: 0.016
time_appconnect: 0.162
time_pretransfer: 0.162
time_starttransfer: 0.217
time_total: 0.261
其中几个参数
  • -o:把curl 返回的html、js 写到垃圾回收站[ /dev/null]
  • -s:掉所有状态信息
  • -w, --write-out FORMAT What to output after completion
时间变量
  • 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:开始传输时间。在client发出请求之后,Web 服务器返回数据的第一个字节所用的时间。

在客户端发出请求之后,服务器处理请求并开始发回数据所用的时间是:time_starttransfer – time_connec = 0.201 秒

客户端从服务器下载数据所用的时间是 time_total –time_starttransfer = 0.044 秒.

其他变量:
  • url_effective:The URL that was fetched last. This is most meaningful if you've told curl to follow location: headers.
  • filename_effective: The ultimate filename that curl writes out to. This is only meaningful if curl is told to write to a file with the --remote-name or --output option. It's most useful in combination with the --remote-header-name option.
  • 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)
  • size_download :下载大小
  • size_upload :上传大小
  • size_header:下载的header的大小
  • size_request:请求的大小
  • speed_download:下载速度,单位-byte/s
  • speed_upload:上传速度,单位-byte/s
  • content_type:content-Type
  • num_connects:Number of new connects made in the recent transfer.
  • num_redirects:Number of redirects that were followed in the request.
  • 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.
  • ftp_entry_path:The initial path libcurl ended up in when logging on to the remote FTP server.
  • ssl_verify_resul:ssl认证结果,返回0表示认证成功。

猜你喜欢

转载自www.cnblogs.com/wshenjin/p/9144540.html
今日推荐