linux CURL 命令

references

Linux curl 官方解释

Linux curl 使用简单介绍,这篇文章超强,有条理的理清了 curl 命令的强大;下载

指令

curl

1
$ curl www.baidu.com

返回页面的 HTML 文档

1
2
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

-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
2
3
4
5
6
7
8
9
10
11
12
$ cat baidu.cookie.txt 
HTTP/1.1 200 OK
Server: bfe/1.0.8.18
Date: Fri, 12 May 2017 11:52:52 GMT
Content-Type: text/html
Content-Length: 2381
Last-Modified: Mon, 23 Jan 2017 13:27:28 GMT
Connection: Keep-Alive
ETag: "588604c0-94d"
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Pragma: no-cache
Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/

-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
2
3
-x host:port
-x [protocol://][user:password@]proxyhost[:port]
--proxy [protocol://][user:password@]proxyhost[:port]

-i

或者使用 –head

查看 header 的信息

-H

添加或者删除 header 的内容

添加

1
2
-H "name: value"
--header "name: value"

删除

1
2
-H "name:"
--header "name:"