详解Curl的使用方法

2016年3月1日09:27:04系统教程评论1,259

Curl是一个很强大的http命令行工具,被广泛应用与Unix、多种Linux发行版本。经常使用curl查看网站返回的http头信息,其实curl还有很多用处,今天为大家详细介绍下curl的用法:文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

Curl命令结构:文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

Usage: curl [options...]文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

1、 直接获取url对应网页代码文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl http://www.duoluodeyu.com文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

回车之后,www.duoluodeyu.com 的html就稀里哗啦地显示在屏幕上了。文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

2、保存网页内容到本地文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl http://www.duoluodeyu.com > page.html文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

或用curl的内置-o存下http的结果:文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl -o page.html http://www.duoluodeyu.com文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

这样,你就可以看到屏幕上出现一个下载页面进度指示。文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

3、使用指定代理文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

使用curl的时候,用-x可以指定http访问所使用的proxy服务器及其端口:文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl -x 123.45.67.89:1080 -o page.html http://www.duoluodeyu.com文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

4、 获取并保存cookies文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

option: -D文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

把http的response里面的cookie信息存到一个特别的文件中去文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl -x 123.45.67.89:1080 -o page.html -D cookie0001.txt http://www.duoluodeyu.com文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

这样,当页面被存到page.html的同时,cookie信息也被存到了cookie0001.txt里面了文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

5、使用保存的cookies文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

使用option:-b把上次的cookie信息追加到http request里面去:文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl -x 123.45.67.89:1080 -o page1.html -D cookie0002.txt -b cookie0001.txt http://www.duoluodeyu.com文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

这样,我们就可以几乎模拟所有的IE操作,去访问网页了!文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

6、添加浏览器信息文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

指定自己这次访问所宣称的自己的浏览器信息的option: -A文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" -x 123.45.67.89:1080 -o page.html -D cookie0001.txt http://www.duoluodeyu.com文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

模拟一个运行在Windows 2000上的 IE6.0,访问服务器文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

"Mozilla/4.73

(X11; U; Linux 2.2; 15 i686"文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

则可以告诉对方你是一台 PC上跑着的Linux,用的是Netscape 4.73文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

7、指定referer文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

服务器端常用的限制方法,就是检查http访问的referer。文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

比如你先访问首页,再访问里面所指定的下载页,文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

这第二次访问的 referer地址就是第一次访问成功后的页面地址。文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

这样,服务器端只要发现对下载页面某次访问的referer地址不是首页的地址,就可以断定那是个盗链文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

设定referer的option: -e文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" -x 123.45.67.89:1080 -e "mail.duoluodeyu.com" -o page.html -D cookie0001.txt http://www.duoluodeyu.com文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

这样,就可以骗对方的服务器,你是从mail.duoluodeyu.com点击某个链接过来的文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

8、利用curl下载文件文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

下载页面到一个文件里,可以使用 -o ,下载文件也是一样。比如,文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl -o 1.jpg http://cgi2.tky.3web.ne.jp/~zzh/screen1.JPG文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

一个新的option:-O 大写的O,这么用:文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl -O http://cgi2.tky.3web.ne.jp/~zzh/screen1.JPG文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

这样,就可以按照服务器上的文件名,自动存在本地了!文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

如果screen1.JPG以外还有screen2.JPG、screen3.JPG、....、screen10.JPG需要下载,文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

在curl里面,这么写就可以了:文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl -O http://cgi2.tky.3web.ne.jp/~zzh/screen[1-10].JPG文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

9、下载多个目录下的文件文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl -O http://cgi2.tky.3web.ne.jp/~{zzh,nick}/[001-201].JPG文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

这样产生的下载,就是文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

~zzh/001.JPG文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

~zzh/002.JPG文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

...文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

~zzh/201.JPG文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

~nick/001.JPG文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

~nick/002.JPG文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

...文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

~nick/201.JPG文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

由于zzh/nick下的文件名都是001,002...,201,下载下来的文件重名,后面的把前面的文件都给覆盖掉了文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl -o #2_#1.jpg http://cgi2.tky.3web.ne.jp/~{zzh,nick}/[001-201].JPG文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

自定义出来下载下来的文件名,就变成了这样:文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

原来: ~zzh/001.JPG --- 下载后: 001-zzh.JPG文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

原来: ~nick/001.JPG --- 下载后: 001-nick.JPG文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

这样一来,就不怕文件重名文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

10、断点续传与分块下载文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

断点续传option: -c文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

比如我们下载screen1.JPG中,突然掉线了,我们就可以这样开始续传文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl -c -O http://cgi2.tky.3wb.ne.jp/~zzh/screen1.JPG文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

分块下载option: -r文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

比如我们有一个http://cgi2.tky.3web.ne.jp/~zzh/zhao1.mp3 要下载文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

我们就可以用这样的命令:文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl -r 0-10240 -o "zhao.part1" http:/cgi2.tky.3web.ne.jp/~zzh/zhao1.mp3 &\文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl -r 10241-20480 -o "zhao.part1" http:/cgi2.tky.3web.ne.jp/~zzh/zhao1.mp3 &\文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl -r 20481-40960 -o "zhao.part1" http:/cgi2.tky.3web.ne.jp/~zzh/zhao1.mp3 &\文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl -r 40961- -o "zhao.part1" http:/cgi2.tky.3web.ne.jp/~zzh/zhao1.mp3文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

这样就可以分块下载啦。文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

不过你需要自己把这些破碎的文件合并起来如果你用UNIX或苹果,文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

用 cat zhao.part* > zhao.mp3就可以文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

如果用的是Windows,用copy /b 来解决吧文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

上面讲的都是http协议的下载,其实ftp也一样可以用。文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl -u name:passwd ftp://ip:port/path/file文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

或者大家熟悉的文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl ftp://name:passwd@ip:port/path/file文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

11、上传文件文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

上传的option是 -T文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

对于ftp服务器,使用 -u 指定用户名与密码文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

比如我们向ftp传一个文件:文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl -T localfile -u name:passwd ftp://upload_site:port/path/文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

当然,向http服务器上传文件也可以比如文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl -T localfile http://cgi2.tky.3web.ne.jp/~zzh/abc.cgi文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

注意,这时候,使用的协议是HTTP的PUT method文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

http提交一个表单,比较常用的是POST模式和GET模式文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

GET模式什么option都不用,只需要把变量写在url里面就可以了比如:文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl http://www.duoluodeyu.com/login.cgi?user=nickwolfe&password=12345文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

而POST模式的option则是 -d文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

比如,文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl -d "user=nickwolfe&password=12345" http://www.duoluodeyu.com/login.cgi文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

就相当于向这个站点发出一次登陆申请文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

到底该用GET模式还是POST模式,要看对面服务器的程序设定。文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

一点需要注意的是,POST模式下的文件上的文件上传,比如:文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

  1. <form method="POST" enctype="multipar/form-data" action="http://cgi2.tky.3web.ne.jp/~zzh/up_file.cgi">
  2. <input type=file name=upload>
  3. <input type=submit name=nick value="go">
  4. </form>

这样一个HTTP表单,我们要用curl进行模拟,就该是这样的语法:文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl -F upload=@localfile -F nick=go http://cgi2.tky.3web.ne.jp/~zzh/up_file.cgi文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

其实curl连接https的时候使用本地证书,就可以这样文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl -E localcert.pem https://remote_server文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

再比如,你还可以用curl通过dict协议去查字典 ~文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

$ curl dict://dict.org/d:computer文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html

文章源自堕落的鱼-https://www.duoluodeyu.com/2287.html
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定