Curl命令

from : http://my.oschina.net/davehe/blog/102036



        curl支持包括http,https,ftp在内的众多协议,它还支持POST,cookie,认证,从指定偏移处下载部分文件,参照页,用户代理字符串,扩展头部,限速,文件大小限制,进度条等特性。 

1.访问http页面内容,通常将下载文件输出到stdout,将进度信息输出到stderr,要想避免显示进度信息,使用--silent,-o用来将下载的数据写入指定名称的文件中.如果需要在下载过程中显示如#的进度条,用--progress代替--silent 

?
1
2
3
4
5
6
7
8
9
10
11
12
root@10.1.1.200:curl # curl http://www.163.com/index.html -o index.html
   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                  Dload  Upload   Total   Spent    Left  Speed
   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100  465k    0  465k    0     0   374k      0 --:--:--  0:00:01 --:--:--  415k
root@10.1.1.200:curl #
root@10.1.1.200:curl # curl http://www.163.com/index.html -o index1.html  --progress
######################################################################## 100.0%
######################################################################## 100.0%
root@10.1.1.200:curl # curl http://www.163.com/index.html -o index.html --silent
root@10.1.1.200:curl # ls
index1.html  index.html


2.只打印响应头部信息(不包括数据部分) 

   只打印响应头部(response header) 有助于进行各种检查或统计,例如,要求无须下载整个页面内容就能够检验某个页面是否能够打开,那么我们只用读取http响应头部就能够知道这个页面是否可用。

   检查HTTP头部的一个用例就是在下载之前先查看文件大小,我们可以在下载之前,通过检查HTTP头部中的Content-Length参数来得知文件的长度,同样还可以从头部检索出其他一些有用的参数,last-modified参数能告诉我们远程文件最后的改动时间

   通过-I或--head就可以只打印HTTP头部信息,而无须下载远程文件。 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
root@10.1.1.200:curl # curl -I http://www.163.com
HTTP /1 .1 200 OK
Expires: Mon, 14 Jan 2013 03:53:33 GMT
Date: Mon, 14 Jan 2013 03:52:13 GMT
Server: nginx
Content-Type: text /html ; charset=GBK
Transfer-Encoding: chunked
Vary: Accept-Encoding,User-Agent,Accept
Cache-Control: max-age=80
Age: 66
X-Via: 1.1 jssq185:88 (Cdn Cache Server V2.0), 1.1 zjfy81:8361 (Cdn Cache Server V2.0)
Connection: keep-alive
 
root@10.1.1.200:curl # curl -I http://www.sina.com
HTTP /1 .0 301 Moved Permanently
Date: Mon, 14 Jan 2013 03:11:55 GMT
Server: Apache
Location: http: //www .sina.com.cn/
Cache-Control: max-age=3600
Expires: Mon, 14 Jan 2013 04:11:55 GMT
Vary: Accept-Encoding
Content-Length: 231
Content-Type: text /html ; charset=iso-8859-1
Age: 2491
X-Cache: HIT from sh201-20.sina.com.cn
Connection: close
 
root@10.1.1.200:curl # curl --head http://www.taobao.com
HTTP /1 .1 200 OK
Server: Tengine
Date: Mon, 14 Jan 2013 03:53:37 GMT
Content-Type: text /html ; charset=gbk
Connection: keep-alive
Vary: Accept-Encoding
Expires: Mon, 14 Jan 2013 04:53:37 GMT
Cache-Control: max-age=3600


3.指定客户端. 

   有些网络资源首先需要判断用户使用的是什么浏览器,符合标准了才能够下载或者浏览,。此时curl可以把自己“伪装”成任何其他浏览器: 

?
1
2
3
4
5
6
7
root@10.1.1.200:curl # curl http://www.test.com
root@10.1.1.200:apache2 #tail -f www.test.com_access.com
10.1.1.200 - - [14 /Jan/2013 :00:33:59 +0800] "GET / HTTP/1.1" 200 54646 "-" "curl/7.18.2 (x86_64-pc-linux-gnu) libcurl/7.18.2 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.8 libssh2/0.18"
 
root@10.1.1.200:curl # curl -A "Mozilla/6.0 (compatible; MSIE 5.01; Windows NT 5.0)"  http://www.test.com
root@10.1.1.200:apache2 #tail -f www.test.com_access.com
10.1.1.200 - - [14 /Jan/2013 :00:36:45 +0800] "GET / HTTP/1.1" 200 54499 "-" "Mozilla/6.0 (compatible; MSIE 6.01; Windows NT 6.0)"

      -A ""该选项可以指定客户端类型,服务器通过该选项判断用户应用的平台及浏览器信息,上面这个指令表示curl伪装成了windows机器. 


4.用curl进行认证 

   登录某些页面或ftp需要先进行认证,输入用户名和密码。curl的这个选项可以直接处理这类操作,用指定的账号和密码进行登录认证。后面的选项指定代理的用户名和密码,这样便可以直接用这个代理访问网页了.-x选项为http指定代理及端口,如果不指定端口,默认为1080 

?
1
curl -U user:password -x 201.36.208.19:3128 http: //www . test .com


5.断点续传 

   -C提供断点续传功能,如果未指定offset,或者直接用"-C -",则curl会自己分析该从什么位置开始续传。 

?
1
root@10.1.1.200:curl # curl  -C - http://www.sina.com


6.限定curl可占用的带宽 

?
1
root@10.1.1.200:curl # curl  http://www.baidu.com --limit-rate 20k


7.分块下载 

   -r选项指定下载字节的范围,常应用于分块下载文件。range的表示方式有多种,如100-500,则指定从100开始的400个字节数据;-500表示 最后的500个字节;5000-表示从第5000个字节开始的所有字节;另外还可以同时指定多个字节块,中间用","分开. 

?
1
2
root@10.1.1.200:curl # curl -r 0-1024 http://www.test.com/a.zip
root@10.1.1.200:curl # curl -r 1025- http://www.test.com/a.zip

      先下1M,然后再下剩下的部分 


8.curl上传 

   用POST方法向服务器提交数据。这时的URL是看不到的,因此需要使用-d参数来抓取这个页面 

?
1
url -d "action=add&name=davehe" -d "id=1" http: //10 .1.1.200 /example .php  

   如果使用了-F参数,这个命令的实质是将本地的文件用POST上传到服务器。-F参数以name=value的方式来指定参数内容,如果值是一个文件,则需要以name=@file的方式来指定。 

?
1
curl -F "filename=@/dave/test.tar.gz" http: //10 .1.1.200 /example .php


8.测试参数 

测试连接www.163.com需要多少时间 

?
1
2
root@10.1.1.200:curl # curl -o /dev/null -s -w %{time_connect}:%{time_starttransfer}:%{time_total} www.163.com
0.013:0.022:0.089
查看网页文件的大小 
?
1
2
root@10.1.1.200:curl # curl -o /dev/null -s -w %{size_header} http://www.163.com
368

其他常用常用http变量 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
http_code:http返回类似404,200,500等
  
time_total:总相应时间
  
time_namelookup:域名解析时间
  
time_connect:连接到目标地址耗费的时间
  
time_pretransfer:从执行到开始传输文件的时间间隔
  
time_starttransfer:从执行到开始传输文件的时间间隔
  
size_download:下载网页或文件大小
  
size_upload:上传文件大小
  
size_header:响应头
  
size_request:发送请求参数大小
  
speed_download:传输速度
  
speed_upload:平均上传速度
  
content_type:下载文件类型.

        

       curl非常强大,除了学习参数之外,还要深刻理解http的各种协议与URL的各个语法。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值