详解Curl的使用方法

Curl是一个很强大的http命令行工具,被广泛应用与Unix、多种Linux发行版本。经常使用curl查看网站返回的http头信息,其实curl还有很多用处,今天为大家详细介绍下curl的用法:

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、使用指定代理

使用curl的时候,用-x可以指定http访问所使用的proxy服务器及其端口:

$ curl -x 123.45.67.89:1080 -o page.html http://www.duoluodeyu.com

4、 获取并保存cookies

option: -D

把http的response里面的cookie信息存到一个特别的文件中去

$ curl -x 123.45.67.89:1080 -o page.html -D cookie0001.txt http://www.duoluodeyu.com

这样,当页面被存到page.html的同时,cookie信息也被存到了cookie0001.txt里面了

5、使用保存的cookies

使用option:-b把上次的cookie信息追加到http request里面去:

$ curl -x 123.45.67.89:1080 -o page1.html -D cookie0002.txt -b cookie0001.txt http://www.duoluodeyu.com

这样,我们就可以几乎模拟所有的IE操作,去访问网页了!

6、添加浏览器信息

指定自己这次访问所宣称的自己的浏览器信息的option: -A

$ 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

模拟一个运行在Windows 2000上的 IE6.0,访问服务器

"Mozilla/4.73

(X11; U; Linux 2.2; 15 i686"

则可以告诉对方你是一台 PC上跑着的Linux,用的是Netscape 4.73

7、指定referer

服务器端常用的限制方法,就是检查http访问的referer。

比如你先访问首页,再访问里面所指定的下载页,

这第二次访问的 referer地址就是第一次访问成功后的页面地址。

这样,服务器端只要发现对下载页面某次访问的referer地址不是首页的地址,就可以断定那是个盗链

设定referer的option: -e

$ 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

这样,就可以骗对方的服务器,你是从mail.duoluodeyu.com点击某个链接过来的

8、利用curl下载文件

下载页面到一个文件里,可以使用 -o ,下载文件也是一样。比如,

$ curl -o 1.jpg http://cgi2.tky.3web.ne.jp/~zzh/screen1.JPG

一个新的option:-O 大写的O,这么用:

$ curl -O http://cgi2.tky.3web.ne.jp/~zzh/screen1.JPG

这样,就可以按照服务器上的文件名,自动存在本地了!

如果screen1.JPG以外还有screen2.JPG、screen3.JPG、....、screen10.JPG需要下载,

在curl里面,这么写就可以了:

$ curl -O http://cgi2.tky.3web.ne.jp/~zzh/screen[1-10].JPG

9、下载多个目录下的文件

$ curl -O http://cgi2.tky.3web.ne.jp/~{zzh,nick}/[001-201].JPG

这样产生的下载,就是

~zzh/001.JPG

~zzh/002.JPG

...

~zzh/201.JPG

~nick/001.JPG

~nick/002.JPG

...

~nick/201.JPG

由于zzh/nick下的文件名都是001,002...,201,下载下来的文件重名,后面的把前面的文件都给覆盖掉了

$ curl -o #2_#1.jpg http://cgi2.tky.3web.ne.jp/~{zzh,nick}/[001-201].JPG

自定义出来下载下来的文件名,就变成了这样:

原来: ~zzh/001.JPG --- 下载后: 001-zzh.JPG

原来: ~nick/001.JPG --- 下载后: 001-nick.JPG

这样一来,就不怕文件重名

10、断点续传与分块下载

断点续传option: -c

比如我们下载screen1.JPG中,突然掉线了,我们就可以这样开始续传

$ curl -c -O http://cgi2.tky.3wb.ne.jp/~zzh/screen1.JPG

分块下载option: -r

比如我们有一个http://cgi2.tky.3web.ne.jp/~zzh/zhao1.mp3 要下载

我们就可以用这样的命令:

$ curl -r 0-10240 -o "zhao.part1" http:/cgi2.tky.3web.ne.jp/~zzh/zhao1.mp3 &\

$ curl -r 10241-20480 -o "zhao.part1" http:/cgi2.tky.3web.ne.jp/~zzh/zhao1.mp3 &\

$ curl -r 20481-40960 -o "zhao.part1" http:/cgi2.tky.3web.ne.jp/~zzh/zhao1.mp3 &\

$ curl -r 40961- -o "zhao.part1" http:/cgi2.tky.3web.ne.jp/~zzh/zhao1.mp3

这样就可以分块下载啦。

不过你需要自己把这些破碎的文件合并起来如果你用UNIX或苹果,

用 cat zhao.part* > zhao.mp3就可以

如果用的是Windows,用copy /b 来解决吧

上面讲的都是http协议的下载,其实ftp也一样可以用。

$ curl -u name:passwd ftp://ip:port/path/file

或者大家熟悉的

$ curl ftp://name:passwd@ip:port/path/file

11、上传文件

上传的option是 -T

对于ftp服务器,使用 -u 指定用户名与密码

比如我们向ftp传一个文件:

$ curl -T localfile -u name:passwd ftp://upload_site:port/path/

当然,向http服务器上传文件也可以比如

$ curl -T localfile http://cgi2.tky.3web.ne.jp/~zzh/abc.cgi

注意,这时候,使用的协议是HTTP的PUT method

http提交一个表单,比较常用的是POST模式和GET模式

GET模式什么option都不用,只需要把变量写在url里面就可以了比如:

$ curl http://www.duoluodeyu.com/login.cgi?user=nickwolfe&password=12345

而POST模式的option则是 -d

比如,

$ curl -d "user=nickwolfe&password=12345" http://www.duoluodeyu.com/login.cgi

就相当于向这个站点发出一次登陆申请

到底该用GET模式还是POST模式,要看对面服务器的程序设定。

一点需要注意的是,POST模式下的文件上的文件上传,比如:

  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进行模拟,就该是这样的语法:

$ curl -F upload=@localfile -F nick=go http://cgi2.tky.3web.ne.jp/~zzh/up_file.cgi

其实curl连接https的时候使用本地证书,就可以这样

$ curl -E localcert.pem https://remote_server

再比如,你还可以用curl通过dict协议去查字典 ~

$ curl dict://dict.org/d:computer

 
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证