什么是curl
curl是我們?cè)贚inux下常用的下載文件或者測(cè)試web服務(wù)的工具阅畴,羅列下他們常
用的一些場(chǎng)景,當(dāng)做總結(jié)却汉。curl支持多種網(wǎng)絡(luò)協(xié)議,包括最新的HTTP/2廊酣。
獲取網(wǎng)頁源碼
~> curl www.google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A >here</A>.
</BODY></HTML>
文件下載
curl
缺省會(huì)輸出文件的內(nèi)容锉罐,用下面的命令可以用curl
將文件保存到指定的目錄帆竹,curl比較苦逼的地方在于,
如果不是保存當(dāng)前目錄脓规,需要自己指定要保存的文件名栽连。
~> cd /opt/ && curl -O http://ergonlogic.com/files/boxes/debian-current.box
~> curl -o /opt/current.box http://ergonlogic.com/files/boxes/debian-current.box
多線程下載
curl
不支持多線程下載,我用過的一個(gè)多線程下載的工具是aria2侨舆,蠻好用的秒紧,而且這貨還支持bt,ed2k等p2p協(xié)議下載。
~> aria2c -x5 https://github.com/jose-lpa/veewee-openbsd/releases/download/v0.5.5/openbsd55.box
通過代理訪問資源
有兩種方式可以讓curl
使用proxy挨下,第一種是通過設(shè)置環(huán)境變量熔恢,http_proxy, https_proxy
等,用no_proxy
去設(shè)置屏蔽代理的列表臭笆。
~> export http_proxy="http://proxy:3128"
讓proxy對(duì)一些地址無效叙淌。
~> export no_proxy="127.0.0.1,localhost.localdomain"
另外一種是在運(yùn)行命令時(shí)直接指定使用的proxy或者不使用proxy。
~> curl -x "http://proxy:3128" www.google.com
~> curl -x "socks5://proxy:3128" www.google.com
~> curl --noproxy * www.google.com
curl的--noproxy
是需要指定一個(gè)exclude的列表愁铺。
花式下載和斷點(diǎn)續(xù)傳
對(duì)于很規(guī)則的下載url鹰霍,可以通配符或者正則來處理,比如我想下載自己博客的幾個(gè)圖片茵乱。
~> curl -O http://iambowen.github.io/images/[1-3].png
[1/3]: http://iambowen.github.io/images/1.png --> 1.png
--_curl_--http://iambowen.github.io/images/1.png
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 222k 100 222k 0 0 59816 0 0:00:03 0:00:03 --:--:-- 59808
[2/3]: http://iambowen.github.io/images/2.png --> 2.png
--_curl_--http://iambowen.github.io/images/2.png
100 342k 100 342k 0 0 33117 0 0:00:10 0:00:10 --:--:-- 48607
[3/3]: http://iambowen.github.io/images/3.png --> 3.png
--_curl_--http://iambowen.github.io/images/3.png
100 208k 100 208k 0 0 32245 0 0:00:06 0:00:06 --:--:-- 39914
斷點(diǎn)續(xù)傳,除了保證文件完整性茂洒,還可以檢查文件是否有變動(dòng),如果有變化則更新似将,沒有則保持文件原有狀態(tài)获黔。
~> curl -O -C http://ergonlogic.com/files/boxes/debian-current.box
獲取headers信息
有時(shí)候,我們只需要看到返回的headers信息在验,查看cache是否命中玷氏,或者返回碼。
~> curl -I www.baidu.com
HTTP/1.1 200 OK
Date: Tue, 10 Feb 2015 08:25:12 GMT
Content-Type: text/html; charset=utf-8
Connection: Keep-Alive
......
傳說使用telnet
命令可能拿到更多的http headers信息腋舌,無法驗(yàn)證盏触,方式如下。
~> telnet 0 4000
Trying 0.0.0.0...
Connected to 0.
Escape character is '^]'.
GET / HTTP/1.1
HOST: 127.0.0.1:4000
HTTP/1.1 200 OK
Etag: 232a2c-320c-54d9ce22
Content-Type: text/html
Content-Length: 12812
Last-Modified: Tue, 10 Feb 2015 09:23:46 GMT
Server: WEBrick/1.3.1 (Ruby/1.9.3/2014-11-13)
Date: Tue, 10 Feb 2015 09:29:24 GMT
Connection: Keep-Alive
follow redirection信息
有時(shí)候訪問的資源块饺,可能被臨時(shí)或者永久轉(zhuǎn)移赞辩,所以會(huì)有中間跳轉(zhuǎn)的過程,但是直接去curl
是拿不到完整信息的授艰。
~> curl -I www.google.com
HTTP/1.1 302 Found
Location: http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/%3Fgws_rd%3Dcr&ust=1423564147298386&usg=AFQjCNHZ7kXJXOfOQyCSOQ6ZGOPjeVaIYg
Cache-Control: private
Content-Type: text/html; charset=UTF-8
......
通過下面的方式可以拿到所有的返回信息辨嗽。
~> curl -LI www.google.com
HTTP/1.1 302 Found
Location: http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/%3Fgws_rd%3Dcr&ust=1423559822825114&usg=AFQjCNEEY2qyq9HghaQVZ89ugMv9kvDlLA
......
HTTP/1.1 302 Found
Location: http://www.google.com.hk/?gws_rd=cr
......
HTTP/1.1 200 OK
Date: Tue, 10 Feb 2015 09:16:35 GMT
Expires: -1
......
加入驗(yàn)證的請(qǐng)求
有時(shí)候在請(qǐng)求一些資源時(shí),需要通過驗(yàn)證才能完成訪問淮腾,可以用username:password
加URL即可糟需。
~> curl http://user:password@echo.httpkit.com?queryString
偽裝user agent的請(qǐng)求
有的服務(wù)器可能會(huì)限制訪問的User-Agent類型,用curl測(cè)試的時(shí)候可以用相應(yīng)得參數(shù)進(jìn)行偽裝谷朝。
~> curl echo.httpkit.com
"headers": {
"host": "echo.httpkit.com",
"user-agent": "curl/7.37.1",
"accept": "*/*"
}
~> curl -A "Bad Ass" echo.httpkit.com
"headers": {
"host": "echo.httpkit.com",
"user-agent": "Bad Ass",
"accept": "*/*"
},
發(fā)起HTTP請(qǐng)求
用-X
可以指定發(fā)起請(qǐng)求的HTTP方法, 如果用POST
或者PUT
等方法洲押,可以用 -d
指定request body,-H
可以指定請(qǐng)求的一些Headers圆凰。
~> curl -X PUT -H 'Content-Type: application/json' -d '{"firstName":"Kris", "lastName":"Jordan"}' echo.httpkit.com
{
"method": "PUT",
"uri": "/",
"path": {
"name": "/",
"query": "",
"params": {}
},
"headers": {
"x-forwarded-for": "210.74.157.146",
"host": "echo.httpkit.com",
"user-agent": "curl/7.37.1",
"accept": "*/*",
"content-type": "application/json",
"content-length": "41"
},
"body": "{\"firstName\":\"Kris\", \"lastName\":\"Jordan\"}",
"ip": "127.0.0.1",
"powered-by": "http://httpkit.com",
"docs": "http://httpkit.com/echo"
}
上傳文件
~> curl --form "fileupload=@filename.txt" http://hostname/resource
處理cookies
保存cookies到本地
~> curl -c echo.cookies http://www.baidu.com > /dev/null
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 88672 0 88672 0 0 298k 0 --:--:-- --:--:-- --:--:-- 297k
~> less echo.cookies
# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
.baidu.com TRUE / FALSE 3571557287 BAIDUID 287BE3DB1621FDE977C39D21BD02CA45:FG=1
.baidu.com TRUE / FALSE 3571557287 BAIDUPSID 287BE3DB1621FDE977C39D21BD02CA45
www.baidu.com FALSE / FALSE 0 BDSVRTM 0
www.baidu.com FALSE / FALSE 0 BD_HOME 0
.baidu.com TRUE / FALSE 0 H_PS_PSSID 10422_1449_11089
使用cookie 發(fā)起請(qǐng)求
~> curl -b echo.cookies http://www.baidu.com > /dev/null
fail on error
對(duì)于服務(wù)器端錯(cuò)誤杈帐,http請(qǐng)求沒有任何輸出,curl
的返回為0专钉,使用-f
參數(shù)可以在遇到服務(wù)器錯(cuò)誤是返回非0(22)挑童。
最近在完成一個(gè)用Bamboo host的package去部署的任務(wù)的時(shí)候用到了這個(gè)參數(shù),一旦下載package出錯(cuò)跃须,部署會(huì)中斷炮沐。
~> curl -f http://iambowen.github.io/ksjdkfsjdf
curl: (22) The requested URL returned error: 404 Not Found
~> echo $?
22
從瀏覽器"偷取"curl的命令
覺得curl
指令太難記憶,可以直接從瀏覽器中"偷"取回怜, Chrome的Developer Tools里面提供了這樣的功能大年,右鍵點(diǎn)擊請(qǐng)求的url,可以提取出curl
完整的curl請(qǐng)求玉雾。