references
Linux curl 使用简单介绍,这篇文章超强,有条理的理清了 curl 命令的强大;下载
指令
curl
1 | $ curl www.baidu.com |
返回页面的 HTML 文档
1 |
|
-o / -O
_-o_
1 | $ curl www.baidu.com -o out.html |
将返回的 HTML 内容重定向到文件 out.html 中;
_-O_
1 | $ curl -O https://www.baidu.com/img/bd_logo1.png |
将会自动的下载图片,并且在当前目录中将其保存为 bd_logo1.png
加入有十张照片,可以用正则表达式的方式进行下载,
1 | $ curl -O http://cgi2.tky.3web.ne.jp/~zzh/screen[1-10].JPG |
-D
在文件中保存当前 cookie 信息;
1 | $ curl http://www.baidu.com -o www.baidu.com.output -D baidu.cookie.txt |
本地会保存一份当前访问的 cookie 的信息
1 | $ cat baidu.cookie.txt |
-b
使用本地的 cookie 文件
1 | $ curl http://www.baidu.com -b baidu.cookie.txt -o www.baidu.com.output -D baidu.cookie2.txt |
使用本地的 cookie 文件 baidu.cookie.txt 进行访问,并将新的 cookie 保存为 baidu.cookie2.txt;
-A
设置 User Agent,
1 | $ curl -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" http://www.baidu.com |
这个有点万恶,这让那些防爬虫的,防盗链的情何以堪?
-e
设置 referer,既是访问的源地址(网址),
1 | curl -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" -e "www.baidu.com" http://www.baidu.com |
这个更万恶,至此,通过判断客户端信息的方式,彻底失败;访问来源以及客户端程序是什么,什么版本这些都可以伪造… IP 呢?
–limit-rate
1 | $ curl http://www.tutorialspoint.com/unix/ --limit-rate 1k -o unix.html |
用来限制传输速度的。来看官网上的解释,
Specify the maximum transfer rate. This feature is useful if you have a limited pipe and you’d like your transfer not to use your entire bandwidth. The given speed is measured in bytes/second, unless a suffix is appended. Appending ‘k’ or ‘K’ will count the number as kilobytes/sec, ‘m’ or M’ megabytes, while ‘g’ or ‘G’ makes it gigabytes/sec. Eg: 200K, 3m, 1G.
有疑问,这里限速的话,应该指的是服务器的下载速度,那么是不是意味着服务器端必须实现 –limit-rate 相关协议的内容?如果是,该如何实现?
-d
或者使用 –data
使用 ASCII 码向服务器发送键值对,注意,区别于 -d @file/“string”,该命令不接文件和双引号作为参数;
Thus, using ‘-d name=daniel -d skill=lousy’ would generate a post that looks like ‘name=daniel&skill=lousy’
使用 –data-urlencode
发送过程中使用 URL-encode;
–data-binary
–data-binary 和 _-d_ 作用类似,却别是发送 binary 信息
-d @file/“string”
表示使用 POST 协议提交数据,注意,区别于 -d,该命令必须接文件或者带双引号的字符串
1 | $ curl -d "user=nickwolfe&password=12345" http://www.yahoo.com/login.cgi |
-XPOST
想服务器发送 POST 请求
1 | $ curl -XPOST www.baidu.com |
-u
或者使用 –user
指定用户账户和密码,用于服务器端验证;
1 | $ curl -XPOST -u username:password http://localhost:8080 |
-x
使用代理,或者使用 –proxy,
1 | $ curl -x proxy.example.com:3128 http://www.tutorialspoint.com/unix/ |
如果需要身份验证,
1 | $ curl -x username:password@proxy.example.com:3128 http://www.tutorialspoint.com/unix/ |
使用语法,
1 | -x host:port |
-i
或者使用 –head
查看 header 的信息
-H
添加或者删除 header 的内容
添加
1 | -H "name: value" |
删除
1 | -H "name:" |