Curl命令

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

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.只打印响应头部信息(不包括数据部分)html

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

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

   经过-I或--head就能够只打印HTTP头部信息,而无须下载远程文件。linux

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.指定客户端.nginx

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

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机器.apache


4.用curl进行认证windows

   登陆某些页面或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:下载文件类型.


9 指定方式请求:       

curl -I  -H "ContentType: text" --request GET\DELETE\PUT http://www.xxxx.com


10 curl命令绑定host访问网页的方法:

假设访问页面地址为: http://www.abc.com/test.jsp

www.abc.com 域名解析到ip: 192.168.0.1 则curl访问方式为以下:


curl -H "Host:www.abc.com"  "http://192.168.1.1/test.jsp"

相关文章
相关标签/搜索