命令簡(jiǎn)介
curl是一個(gè)廣為使用的命令,可以幫助用戶將數(shù)據(jù)傳送或從一個(gè)遠(yuǎn)程服務(wù)器。 它支持許多協(xié)議包括HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT, TELNET, LDAP,以及 FILE。
curl提供了很多特性,包括文件斷點(diǎn)續(xù)傳,支持代理,F(xiàn)TP上傳,用戶認(rèn)證,POST方法,帶寬限制,等等。curl的底層采用libcurl 庫(kù)。
在新版的Linux 中,curl已經(jīng)內(nèi)嵌安裝,不需要額外安裝。如果運(yùn)行時(shí)候發(fā)現(xiàn)提示命令不存在,可以用系統(tǒng)自帶的管理工具安裝。
CentOS, RHEL, Fedora等系統(tǒng)的安裝命令:
sudo dnf install curl
Ubuntu, Debian等系統(tǒng)的安裝命令:
sudo apt install curl
實(shí)例
curl命令格式:
$ curl [option] url
1 顯示url的內(nèi)容
可以使用curl查看URL對(duì)應(yīng)的頁(yè)面源碼。
示例:
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl http://www.baidu.com
<!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>...</html>
2 只接收header
通過(guò)參數(shù)-I 或者 –head,讓curl只獲取header信息。
示例:
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl -I http://www.baidu.com
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 277
Content-Type: text/html
Date: Tue, 29 Nov 2022 03:21:15 GMT
Etag: "575e1f59-115"
Last-Modified: Mon, 13 Jun 2016 02:50:01 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
3 下載內(nèi)容并保存到文件
curl通過(guò)參數(shù)-o 或者 –output 來(lái)保存內(nèi)容。用戶需要指定文件名稱。
命令格式:
curl -o filename URL
或者
curl --output filename URL
示例:
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl -o baidu www.baidu.com
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2381 100 2381 0 0 12801 0 --:--:-- --:--:-- --:--:-- 12732
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ ls
baidu
如上例,curl會(huì)展示下載過(guò)程中的數(shù)據(jù),例如,傳輸速度,文件大小,耗時(shí)等。
4 通過(guò)curl下載文件
如果url代表的是一個(gè)文件,可以通過(guò)-o 或者 –output保存文件。
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl -o baidu.png https://b.bdstatic.com/searchbox/icms/searchbox/img/cheng_girl.png
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 9337 100 9337 0 0 21172 0 --:--:-- --:--:-- --:--:-- 21172
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ ll
total 16
-rw-r--r-- 1 yunzhong yunzhong 9337 Nov 29 12:19 baidu.png
5 下載文件并保存為URL相同名字
上面的例子,我們用-o 或者 –output指定下載文件的名稱。為了簡(jiǎn)化操作,我們可以使用參數(shù)-O 或者 –remote-name, 用URL中的文件名作為下載的文件名。
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl -O https://b.bdstatic.com/searchbox/icms/searchbox/img/cheng_girl.png
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 9337 100 9337 0 0 23459 0 --:--:-- --:--:-- --:--:-- 23401
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ ll
total 28
-rw-r--r-- 1 yunzhong yunzhong 9337 Nov 29 12:23 cheng_girl.png
6 同時(shí)下載多個(gè)文件
curl支持同時(shí)下載多個(gè)文件,每個(gè)文件設(shè)置參數(shù)-o 或者 -O
命令格式:
$ curl -O url1 -O url2
示例:
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl -O https://b.bdstatic.com/searchbox/icms/searchbox/img/cheng_girl.png -O https://b.bdstatic.com/searchbox/icms/searchbox/img/young_boy.png
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 9337 100 9337 0 0 22498 0 --:--:-- --:--:-- --:--:-- 22498
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 7730 100 7730 0 0 75048 0 --:--:-- --:--:-- --:--:-- 75048
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ ll
total 36
-rw-r--r-- 1 yunzhong yunzhong 9337 Nov 29 21:51 cheng_girl.png
-rw-r--r-- 1 yunzhong yunzhong 7730 Nov 29 21:51 young_boy.png
7 恢復(fù)下載
如果一個(gè)下載終止或者被打斷,可以使用-C 或者 –continue-at參數(shù) 恢復(fù)下載。當(dāng)設(shè)置了參數(shù),curl命令將會(huì)找出斷點(diǎn)位置并重新執(zhí)行。
示例:
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl -C - -O https://releases.ubuntu.com/22.04.1/ubuntu-22.04.1-desktop-amd64.iso
** Resuming transfer from byte position 311296
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 3649M 0 240k 0 0 54649 0 19:26:59 0:00:04 19:26:55 54637^C
8 使用curl跟隨重定向
默認(rèn)情況下,curl并不會(huì)跟蹤重定向。如果目標(biāo)頁(yè)被移動(dòng)到另外一個(gè)地址,我們可以通過(guò)-L 或者 –location參數(shù)來(lái)跟蹤。
首先明確下,怎么知道是重定向。http標(biāo)準(zhǔn)狀態(tài)碼可以明確告知,下面是狀態(tài)碼的基本列表:
編碼 |
子編碼 |
意義 |
100+ |
代表請(qǐng)求已被接受,需要繼續(xù)處理。 |
|
200+ |
請(qǐng)求已成功被服務(wù)器接收、理解、并接受。 |
|
300+ |
重定向 |
|
400+ |
請(qǐng)求錯(cuò)誤 |
|
403 |
服務(wù)器已經(jīng)理解請(qǐng)求,但是拒絕執(zhí)行。 |
|
404 |
請(qǐng)求失敗,服務(wù)器沒(méi)有對(duì)應(yīng)的資源。 |
|
500+ |
服務(wù)器內(nèi)部出現(xiàn)錯(cuò)誤。 |
|
因此,如果服務(wù)器返回的狀態(tài)碼為300+,則說(shuō)明進(jìn)行了重定向。
示例:
我們以必應(yīng)為例,當(dāng)訪問(wèn)www.bing.com時(shí),會(huì)自動(dòng)重定向到www.cn.bing.com。因此直接使用curl并不能獲得結(jié)果,添加-L參數(shù)后才能獲得頁(yè)面信息。
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl www.bing.com
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl -L www.bing.com
<!doctype html><html lang="zh" dir="ltr"><head>...
9 curl的靜默模式
curl可以使用參數(shù)-s 或者 –silent 隱藏參數(shù)信息或者錯(cuò)誤信息。
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl -O https://b.bdstatic.com/searchbox/icms/searchbox/img/young_girl.png
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 8647 100 8647 0 0 37271 0 --:--:-- --:--:-- --:--:-- 37271
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl -O -s https://b.bdstatic.com/searchbox/icms/searchbox/img/young_girl.png
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$
10 查看請(qǐng)求返回碼
配合grep,可以過(guò)濾出請(qǐng)求結(jié)果的狀態(tài)碼。
命令格式:
$ curl -IsL URL | grep HTTP
如果想單純的獲得狀態(tài)碼,可以配合awk命令。
$ curl -IsL URL | grep HTTP | awk '{print $2}'
示例:
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl -IsL www.baidu.com | grep HTTP
HTTP/1.1 200 OK
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl -IsL www.baidu.com | grep HTTP | awk '{print $2}'
200
11 設(shè)置最大傳輸速率
–limit-rate參數(shù)可以設(shè)定最大傳輸速率。如果未設(shè)置后綴,則參數(shù)值的單位是bytes/second ??捎玫暮缶Y為k,m,g。
示例:
Average Dload可以看出,速度限制生效。也可以看出,我的網(wǎng)速有多么慢。
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl --limit-rate 2 -O https://releases.ubuntu.com/22.04.1/ubuntu-22.04.1-desktop-amd64.iso
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 3649M 0 16384 0 0 1352 0 32d 18h 0:00:12 32d 18h 0^C
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl --limit-rate 2k -O https://releases.ubuntu.com/22.04.1/ubuntu-22.04.1-desktop-amd64.iso
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 3649M 0 49152 0 0 2890 0 15d 07h 0:00:17 15d 07h 3278^
12 從有認(rèn)證的FTP服務(wù)器下載文件
curl命令可以使用-u 或者 –user 參數(shù)設(shè)置訪問(wèn)服務(wù)器的用戶名密碼。
命令格式:
$ curl -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/file.txt
或者
$ curl --user FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/file.txt
13 上傳文件到ftp服務(wù)器
可以使用-T 或者 –upload-file 參數(shù)指定上傳ftp服務(wù)器的文件。一般的服務(wù)器都需要認(rèn)證,因此可以與-u 或者 –user參數(shù)一起使用,指定認(rèn)證信息。
命令格式:
$ curl -T filename -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/file.txt
或者
$ curl --upload-file filename -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/file.txt
14 使用curl發(fā)送POST請(qǐng)求
使用參數(shù)-d 或者 –data,可以為POST請(qǐng)求設(shè)定數(shù)據(jù)。默認(rèn)情況下,采用表單(form)的形式發(fā)送數(shù)據(jù),因此數(shù)據(jù)必須是key=value形式,且多個(gè)key之間采用&分割。
示例:
$ curl -d "fname=John&lname=Doe" https://www.example.com/post
或者
$ curl --data "fname=John&lname=Doe" https://www.example.com/post
其實(shí),很多時(shí)候,我們都采用了json數(shù)據(jù)格式,而并非是form表單形式。
可以使用如下命令:
curl -H "Content-Type: application/json" -X POST -d '{"json":"value", "jsonKey": "value2"}' https://www.example.com/post
其中,-H命令設(shè)定了請(qǐng)求header參數(shù)。
-X參數(shù),設(shè)定了請(qǐng)求的http方法。
-d參數(shù),設(shè)定了請(qǐng)求的發(fā)送數(shù)據(jù)。
和POST類似,可以使用-x和-d命令,實(shí)現(xiàn)http其他方法的支持,例如PUT等。
15 指定proxy
可以使用參數(shù)-x 或者 –proxy 指定代理,如果未設(shè)定端口則默認(rèn)使用1080端口。
命令格式:
$ curl -x proxy_host:port url
或者
$ curl --proxy proxy_host:port url
16 為proxy指定用戶名密碼
如果proxy服務(wù)需要認(rèn)證信息,我們可以使用參數(shù)-U 或者 –proxy-user 傳遞用戶名密碼信息。
命令格式:
$ curl -U username:password -x proxy_host:port url
或者
$ curl --proxy-user username:password -x proxy_host:port url
17 DICT協(xié)議支持
curl 支持DICT(dictionary network protocol)協(xié)議,對(duì)于發(fā)現(xiàn)名詞定義有很好的幫助。
不需要額外參數(shù),可以直接調(diào)用。
$ curl dict://dict.org/d:machine
版權(quán)聲明:本文內(nèi)容由互聯(lián)網(wǎng)用戶自發(fā)貢獻(xiàn),該文觀點(diǎn)僅代表作者本人。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如發(fā)現(xiàn)本站有涉嫌抄襲侵權(quán)/違法違規(guī)的內(nèi)容, 請(qǐng)發(fā)送郵件至2705686032@qq.com 舉報(bào),一經(jīng)查實(shí),本站將立刻刪除。原文轉(zhuǎn)載: 原文出處: