一、curl 介紹
curl
是一個命令行訪問URL的計算機邏輯語言的工具,發(fā)出網(wǎng)絡(luò)請求,然后得到數(shù)據(jù)并提取出,顯示在標準輸出stdout
上面;
可以用它來構(gòu)造http request報文,curl(CommandLine Uniform Resource Locator)
,即在命令行中利用URL進行數(shù)據(jù)或者文件傳輸;
支持的協(xié)議包括 (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET and TFTP
),
curl設(shè)計為無用戶交互下完成工作;
curl提供了一大堆非常有用的功能,包括代理訪問、用戶認證、ftp上傳下載、HTTP POST、SSL連接、cookie支持、斷點續(xù)傳等;
在很多的操作系統(tǒng)中被使用,其中包括Unix、和Linux,除此之外,也有DOS和Win64等的版本。
二、常見功能
2.1、查看網(wǎng)頁源碼
直接在 curl 命令后加上網(wǎng)址,就可以看到網(wǎng)頁源碼。以網(wǎng)址www.baidu.com為例(選擇該網(wǎng)址,主要因為它的網(wǎng)頁代碼較短)。
$ curl 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><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&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登錄</a> </noscript> <script>document.write('<a + 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;">更多產(chǎn)品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>關(guān)于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必讀</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意見反饋</a> 京ICP證030173號 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
如果要把這個網(wǎng)頁保存下來,可以使用 -o
參數(shù):
$ curl -o /iso2/gyy/tmp/baidu.txt www.baidu.com
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2381 100 2381 0 0 12767 0 --:--:-- --:--:-- --:--:-- 12732
$ cat baidu.txt
<!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&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登錄</a> </noscript> <script>document.write('<a + 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;">更多產(chǎn)品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>關(guān)于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必讀</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意見反饋</a> 京ICP證030173號 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
2.2、自動跳轉(zhuǎn)
有的網(wǎng)址是自動跳轉(zhuǎn)的。使用 -L
參數(shù),curl 就會跳轉(zhuǎn)到新的網(wǎng)址。
$ curl -L www.sina.com
鍵入上面的命令,結(jié)果自動跳轉(zhuǎn)為 www.sina.com.cn。
2.3、顯示頭信息
-i
參數(shù)可以顯示 http response 的頭信息,連同網(wǎng)頁代碼一起。-I
參數(shù)則只顯示 http response 的頭信息。
$ curl -i 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: 2381
Content-Type: text/html
Date: Mon, 14 Nov 2022 06:06:56 GMT
Etag: "588604c8-94d"
Last-Modified: Mon, 23 Jan 2017 13:27:36 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
<!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&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登錄</a> </noscript> <script>document.write('<a + 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;">更多產(chǎn)品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>關(guān)于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必讀</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意見反饋</a> 京ICP證030173號 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
curl -I 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: Mon, 14 Nov 2022 06:07:34 GMT
Etag: "575e1f60-115"
Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
2.4、顯示通信過程
-v
參數(shù)可以顯示一次 http 通信的整個過程,包括端口連接和 http request 頭信息。
$ curl -v www.baidu.com
* About to connect() to www.baidu.com port 80 (#0)
* Trying 180.101.49.14...
* Connected to www.baidu.com (180.101.49.14) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.baidu.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Connection: keep-alive
< Content-Length: 2381
< Content-Type: text/html
< Date: Mon, 14 Nov 2022 06:11:11 GMT
< Etag: "588604c8-94d"
< Last-Modified: Mon, 23 Jan 2017 13:27:36 GMT
< Pragma: no-cache
< Server: bfe/1.0.8.18
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
<
<!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&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登錄</a> </noscript> <script>document.write('<a + 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;">更多產(chǎn)品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>關(guān)于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必讀</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意見反饋</a> 京ICP證030173號 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
* Connection #0 to host www.baidu.com left intact
如果覺得上面的信息還不夠,那么下面的命令可以查看更詳細的通信過程。
$ curl --trace output.txt www.baidu.com
$ cat output.txt
== Info: About to connect() to www.baidu.com port 80 (#0)
== Info: Trying 180.101.49.14...
== Info: Connected to www.baidu.com (180.101.49.14) port 80 (#0)
=> Send header, 77 bytes (0x4d)
0000: 47 45 54 20 2f 20 48 54 54 50 2f 31 2e 31 0d 0a GET / HTTP/1.1..
0010: 55 73 65 72 2d 41 67 65 6e 74 3a 20 63 75 72 6c User-Agent: curl
0020: 2f 37 2e 32 39 2e 30 0d 0a 48 6f 73 74 3a 20 77 /7.29.0..Host: w
0030: 77 77 2e 62 61 69 64 75 2e 63 6f 6d 0d 0a 41 63 ww.baidu.com..Ac
0040: 63 65 70 74 3a 20 2a 2f 2a 0d 0a 0d 0a cept: */*....
<= Recv header, 17 bytes (0x11)
0000: 48 54 54 50 2f 31 2e 31 20 32 30 30 20 4f 4b 0d HTTP/1.1 200 OK.
0010: 0a .
<= Recv header, 22 bytes (0x16)
0000: 41 63 63 65 70 74 2d 52 61 6e 67 65 73 3a 20 62 Accept-Ranges: b
0010: 79 74 65 73 0d 0a ytes..
<= Recv header, 76 bytes (0x4c)
0000: 43 61 63 68 65 2d 43 6f 6e 74 72 6f 6c 3a 20 70 Cache-Control: p
0010: 72 69 76 61 74 65 2c 20 6e 6f 2d 63 61 63 68 65 rivate, no-cache
0020: 2c 20 6e 6f 2d 73 74 6f 72 65 2c 20 70 72 6f 78 , no-store, prox
0030: 79 2d 72 65 76 61 6c 69 64 61 74 65 2c 20 6e 6f y-revalidate, no
0040: 2d 74 72 61 6e 73 66 6f 72 6d 0d 0a -transform..
<= Recv header, 24 bytes (0x18)
0000: 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 6b 65 65 70 Connection: keep
0010: 2d 61 6c 69 76 65 0d 0a -alive..
<= Recv header, 22 bytes (0x16)
0000: 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 Content-Length:
0010: 32 33 38 31 0d 0a 2381..
<= Recv header, 25 bytes (0x19)
0000: 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 74 65 Content-Type: te
0010: 78 74 2f 68 74 6d 6c 0d 0a xt/html..
<= Recv header, 37 bytes (0x25)
0000: 44 61 74 65 3a 20 4d 6f 6e 2c 20 31 34 20 4e 6f Date: Mon, 14 No
0010: 76 20 32 30 32 32 20 30 36 3a 31 32 3a 34 35 20 v 2022 06:12:45
0020: 47 4d 54 0d 0a GMT..
<= Recv header, 22 bytes (0x16)
0000: 45 74 61 67 3a 20 22 35 38 38 36 30 34 63 38 2d Etag: "588604c8-
0010: 39 34 64 22 0d 0a 94d"..
<= Recv header, 46 bytes (0x2e)
0000: 4c 61 73 74 2d 4d 6f 64 69 66 69 65 64 3a 20 4d Last-Modified: M
0010: 6f 6e 2c 20 32 33 20 4a 61 6e 20 32 30 31 37 20 on, 23 Jan 2017
0020: 31 33 3a 32 37 3a 33 36 20 47 4d 54 0d 0a 13:27:36 GMT..
<= Recv header, 18 bytes (0x12)
0000: 50 72 61 67 6d 61 3a 20 6e 6f 2d 63 61 63 68 65 Pragma: no-cache
0010: 0d 0a ..
<= Recv header, 22 bytes (0x16)
0000: 53 65 72 76 65 72 3a 20 62 66 65 2f 31 2e 30 2e Server: bfe/1.0.
0010: 38 2e 31 38 0d 0a 8.18..
<= Recv header, 67 bytes (0x43)
0000: 53 65 74 2d 43 6f 6f 6b 69 65 3a 20 42 44 4f 52 Set-Cookie: BDOR
0010: 5a 3d 32 37 33 31 35 3b 20 6d 61 78 2d 61 67 65 Z=27315; max-age
0020: 3d 38 36 34 30 30 3b 20 64 6f 6d 61 69 6e 3d 2e =86400; domain=.
0030: 62 61 69 64 75 2e 63 6f 6d 3b 20 70 61 74 68 3d baidu.com; path=
0040: 2f 0d 0a /..
<= Recv header, 2 bytes (0x2)
0000: 0d 0a ..
<= Recv data, 2381 bytes (0x94d)
0000: 3c 21 44 4f 43 54 59 50 45 20 68 74 6d 6c 3e 0d <!DOCTYPE html>.
0010: 0a 3c 21 2d 2d 53 54 41 54 55 53 20 4f 4b 2d 2d .<!--STATUS OK--
0020: 3e 3c 68 74 6d 6c 3e 20 3c 68 65 61 64 3e 3c 6d ><html> <head><m
0030: 65 74 61 20 68 74 74 70 2d 65 71 75 69 76 3d 63 eta http-equiv=c
0040: 6f 6e 74 65 6e 74 2d 74 79 70 65 20 63 6f 6e 74 ontent-type cont
0050: 65 6e 74 3d 74 65 78 74 2f 68 74 6d 6c 3b 63 68 ent=text/html;ch
0060: 61 72 73 65 74 3d 75 74 66 2d 38 3e 3c 6d 65 74 arset=utf-8><met
0070: 61 20 68 74 74 70 2d 65 71 75 69 76 3d 58 2d 55 a http-equiv=X-U
0080: 41 2d 43 6f 6d 70 61 74 69 62 6c 65 20 63 6f 6e A-Compatible con
0090: 74 65 6e 74 3d 49 45 3d 45 64 67 65 3e 3c 6d 65 tent=IE=Edge><me
00a0: 74 61 20 63 6f 6e 74 65 6e 74 3d 61 6c 77 61 79 ta content=alway
00b0: 73 20 6e 61 6d 65 3d 72 65 66 65 72 72 65 72 3e s name=referrer>
00c0: 3c 6c 69 6e 6b 20 72 65 6c 3d 73 74 79 6c 65 73 <link rel=styles
00d0: 68 65 65 74 20 74 79 70 65 3d 74 65 78 74 2f 63 heet type=text/c
00e0: 73 73 20 68 72 65 66 3d 68 74 74 70 3a 2f 2f 73 ss href=http://s
00f0: 31 2e 62 64 73 74 61 74 69 63 2e 63 6f 6d 2f 72 1.bdstatic.com/r
0100: 2f 77 77 77 2f 63 61 63 68 65 2f 62 64 6f 72 7a /www/cache/bdorz
0110: 2f 62 61 69 64 75 2e 6d 69 6e 2e 63 73 73 3e 3c /baidu.min.css><
0120: 74 69 74 6c 65 3e e7 99 be e5 ba a6 e4 b8 80 e4 title>..........
0130: b8 8b ef bc 8c e4 bd a0 e5 b0 b1 e7 9f a5 e9 81 ................
0140: 93 3c 2f 74 69 74 6c 65 3e 3c 2f 68 65 61 64 3e .</title></head>
0150: 20 3c 62 6f 64 79 20 6c 69 6e 6b 3d 23 30 30 30 <body link=#000
0160: 30 63 63 3e 20 3c 64 69 76 20 69 64 3d 77 72 61 0cc> <div id=wra
0170: 70 70 65 72 3e 20 3c 64 69 76 20 69 64 3d 68 65 pper> <div id=he
0180: 61 64 3e 20 3c 64 69 76 20 63 6c 61 73 73 3d 68 ad> <div class=h
0190: 65 61 64 5f 77 72 61 70 70 65 72 3e 20 3c 64 69 ead_wrapper> <di
01a0: 76 20 63 6c 61 73 73 3d 73 5f 66 6f 72 6d 3e 20 v class=s_form>
01b0: 3c 64 69 76 20 63 6c 61 73 73 3d 73 5f 66 6f 72 <div class=s_for
01c0: 6d 5f 77 72 61 70 70 65 72 3e 20 3c 64 69 76 20 m_wrapper> <div
01d0: 69 64 3d 6c 67 3e 20 3c 69 6d 67 20 68 69 64 65 id=lg> <img hide
01e0: 66 6f 63 75 73 3d 74 72 75 65 20 73 72 63 3d 2f focus=true src=/
01f0: 2f 77 77 77 2e 62 61 69 64 75 2e 63 6f 6d 2f 69 /www.baidu.com/i
0200: 6d 67 2f 62 64 5f 6c 6f 67 6f 31 2e 70 6e 67 20 mg/bd_logo1.png
0210: 77 69 64 74 68 3d 32 37 30 20 68 65 69 67 68 74 width=270 height
0220: 3d 31 32 39 3e 20 3c 2f 64 69 76 3e 20 3c 66 6f =129> </div> <fo
0230: 72 6d 20 69 64 3d 66 6f 72 6d 20 6e 61 6d 65 3d rm id=form name=
0240: 66 20 61 63 74 69 6f 6e 3d 2f 2f 77 77 77 2e 62 f action=//www.b
0250: 61 69 64 75 2e 63 6f 6d 2f 73 20 63 6c 61 73 73 aidu.com/s class
0260: 3d 66 6d 3e 20 3c 69 6e 70 75 74 20 74 79 70 65 =fm> <input type
0270: 3d 68 69 64 64 65 6e 20 6e 61 6d 65 3d 62 64 6f =hidden name=bdo
0280: 72 7a 5f 63 6f 6d 65 20 76 61 6c 75 65 3d 31 3e rz_come value=1>
0290: 20 3c 69 6e 70 75 74 20 74 79 70 65 3d 68 69 64 <input type=hid
02a0: 64 65 6e 20 6e 61 6d 65 3d 69 65 20 76 61 6c 75 den name=ie valu
02b0: 65 3d 75 74 66 2d 38 3e 20 3c 69 6e 70 75 74 20 e=utf-8> <input
02c0: 74 79 70 65 3d 68 69 64 64 65 6e 20 6e 61 6d 65 type=hidden name
02d0: 3d 66 20 76 61 6c 75 65 3d 38 3e 20 3c 69 6e 70 =f value=8> <inp
02e0: 75 74 20 74 79 70 65 3d 68 69 64 64 65 6e 20 6e ut type=hidden n
02f0: 61 6d 65 3d 72 73 76 5f 62 70 20 76 61 6c 75 65 ame=rsv_bp value
0300: 3d 31 3e 20 3c 69 6e 70 75 74 20 74 79 70 65 3d =1> <input type=
0310: 68 69 64 64 65 6e 20 6e 61 6d 65 3d 72 73 76 5f hidden name=rsv_
0320: 69 64 78 20 76 61 6c 75 65 3d 31 3e 20 3c 69 6e idx value=1> <in
0330: 70 75 74 20 74 79 70 65 3d 68 69 64 64 65 6e 20 put type=hidden
0340: 6e 61 6d 65 3d 74 6e 20 76 61 6c 75 65 3d 62 61 name=tn value=ba
0350: 69 64 75 3e 3c 73 70 61 6e 20 63 6c 61 73 73 3d idu><span class=
0360: 22 62 67 20 73 5f 69 70 74 5f 77 72 22 3e 3c 69 "bg s_ipt_wr"><i
0370: 6e 70 75 74 20 69 64 3d 6b 77 20 6e 61 6d 65 3d nput id=kw name=
0380: 77 64 20 63 6c 61 73 73 3d 73 5f 69 70 74 20 76 wd class=s_ipt v
0390: 61 6c 75 65 20 6d 61 78 6c 65 6e 67 74 68 3d 32 alue maxlength=2
03a0: 35 35 20 61 75 74 6f 63 6f 6d 70 6c 65 74 65 3d 55 autocomplete=
03b0: 6f 66 66 20 61 75 74 6f 66 6f 63 75 73 3e 3c 2f off autofocus></
03c0: 73 70 61 6e 3e 3c 73 70 61 6e 20 63 6c 61 73 73 span><span class
03d0: 3d 22 62 67 20 73 5f 62 74 6e 5f 77 72 22 3e 3c ="bg s_btn_wr"><
03e0: 69 6e 70 75 74 20 74 79 70 65 3d 73 75 62 6d 69 input type=submi
03f0: 74 20 69 64 3d 73 75 20 76 61 6c 75 65 3d e7 99 t id=su value=..
0400: be e5 ba a6 e4 b8 80 e4 b8 8b 20 63 6c 61 73 73 .......... class
0410: 3d 22 62 67 20 73 5f 62 74 6e 22 3e 3c 2f 73 70 ="bg s_btn"></sp
0420: 61 6e 3e 20 3c 2f 66 6f 72 6d 3e 20 3c 2f 64 69 an> </form> </di
0430: 76 3e 20 3c 2f 64 69 76 3e 20 3c 64 69 76 20 69 v> </div> <div i
0440: 64 3d 75 31 3e 20 3c 61 20 68 72 65 66 3d 68 74 d=u1> <a href=ht
0450: 74 70 3a 2f 2f 6e 65 77 73 2e 62 61 69 64 75 2e tp://news.baidu.
0460: 63 6f 6d 20 6e 61 6d 65 3d 74 6a 5f 74 72 6e 65 com name=tj_trne
0470: 77 73 20 63 6c 61 73 73 3d 6d 6e 61 76 3e e6 96 ws class=mnav>..
0480: b0 e9 97 bb 3c 2f 61 3e 20 3c 61 20 68 72 65 66 ....</a> <a href
0490: 3d 68 74 74 70 3a 2f 2f 77 77 77 2e 68 61 6f 31 =http://www.hao1
04a0: 32 33 2e 63 6f 6d 20 6e 61 6d 65 3d 74 6a 5f 74 23.com name=tj_t
04b0: 72 68 61 6f 31 32 33 20 63 6c 61 73 73 3d 6d 6e rhao123 class=mn
04c0: 61 76 3e 68 61 6f 31 32 33 3c 2f 61 3e 20 3c 61 av>hao123</a> <a
04d0: 20 68 72 65 66 3d 68 74 74 70 3a 2f 2f 6d 61 70 href=http://map
04e0: 2e 62 61 69 64 75 2e 63 6f 6d 20 6e 61 6d 65 3d .baidu.com name=
04f0: 74 6a 5f 74 72 6d 61 70 20 63 6c 61 73 73 3d 6d tj_trmap class=m
0500: 6e 61 76 3e e5 9c b0 e5 9b be 3c 2f 61 3e 20 3c nav>......</a> <
0510: 61 20 68 72 65 66 3d 68 74 74 70 3a 2f 2f 76 2e a href=http://v.
0520: 62 61 69 64 75 2e 63 6f 6d 20 6e 61 6d 65 3d 74 baidu.com name=t
0530: 6a 5f 74 72 76 69 64 65 6f 20 63 6c 61 73 73 3d j_trvideo class=
0540: 6d 6e 61 76 3e e8 a7 86 e9 a2 91 3c 2f 61 3e 20 mnav>......</a>
0550: 3c 61 20 68 72 65 66 3d 68 74 74 70 3a 2f 2f 74 <a href=http://t
0560: 69 65 62 61 2e 62 61 69 64 75 2e 63 6f 6d 20 6e ieba.baidu.com n
0570: 61 6d 65 3d 74 6a 5f 74 72 74 69 65 62 61 20 63 ame=tj_trtieba c
0580: 6c 61 73 73 3d 6d 6e 61 76 3e e8 b4 b4 e5 90 a7 lass=mnav>......
0590: 3c 2f 61 3e 20 3c 6e 6f 73 63 72 69 70 74 3e 20 </a> <noscript>
05a0: 3c 61 20 68 72 65 66 3d 68 74 74 70 3a 2f 2f 77 <a href=http://w
05b0: 77 77 2e 62 61 69 64 75 2e 63 6f 6d 2f 62 64 6f ww.baidu.com/bdo
05c0: 72 7a 2f 6c 6f 67 69 6e 2e 67 69 66 3f 6c 6f 67 rz/login.gif?log
05d0: 69 6e 26 61 6d 70 3b 74 70 6c 3d 6d 6e 26 61 6d in&tpl=mn&am
05e0: 70 3b 75 3d 68 74 74 70 25 33 41 25 32 46 25 32 p;u=http%3A%2F%2
05f0: 46 77 77 77 2e 62 61 69 64 75 2e 63 6f 6d 25 32 Fwww.baidu.com%2
0600: 66 25 33 66 62 64 6f 72 7a 5f 63 6f 6d 65 25 33 f%3fbdorz_come%3
0610: 64 31 20 6e 61 6d 65 3d 74 6a 5f 6c 6f 67 69 6e d1 name=tj_login
0620: 20 63 6c 61 73 73 3d 6c 62 3e e7 99 bb e5 bd 95 class=lb>......
0630: 3c 2f 61 3e 20 3c 2f 6e 6f 73 63 72 69 70 74 3e </a> </noscript>
0640: 20 3c 73 63 72 69 70 74 3e 64 6f 63 75 6d 65 6e <script>documen
0650: 74 2e 77 72 69 74 65 28 27 3c 61 20 68 72 65 66 t.write('<a href
0660: 3d 22 68 74 74 70 3a 2f 2f 77 77 77 2e 62 61 69 ="http://www.bai
0670: 64 75 2e 63 6f 6d 2f 62 64 6f 72 7a 2f 6c 6f 67 du.com/bdorz/log
0680: 69 6e 2e 67 69 66 3f 6c 6f 67 69 6e 26 74 70 6c in.gif?login&tpl
0690: 3d 6d 6e 26 75 3d 27 2b 20 65 6e 63 6f 64 65 55 =mn&u='+ encodeU
06a0: 52 49 43 6f 6d 70 6f 6e 65 6e 74 28 77 69 6e 64 RIComponent(wind
06b0: 6f 77 2e 6c 6f 63 61 74 69 6f 6e 2e 68 72 65 66 ow.location.href
06c0: 2b 20 28 77 69 6e 64 6f 77 2e 6c 6f 63 61 74 69 + (window.locati
06d0: 6f 6e 2e 73 65 61 72 63 68 20 3d 3d 3d 20 22 22 on.search === ""
06e0: 20 3f 20 22 3f 22 20 3a 20 22 26 22 29 2b 20 22 ? "?" : "&")+ "
06f0: 62 64 6f 72 7a 5f 63 6f 6d 65 3d 31 22 29 2b 20 bdorz_come=1")+
0700: 27 22 20 6e 61 6d 65 3d 22 74 6a 5f 6c 6f 67 69 '" name="tj_logi
0710: 6e 22 20 63 6c 61 73 73 3d 22 6c 62 22 3e e7 99 n" class="lb">..
0720: bb e5 bd 95 3c 2f 61 3e 27 29 3b 3c 2f 73 63 72 ....</a>');</scr
0730: 69 70 74 3e 20 3c 61 20 68 72 65 66 3d 2f 2f 77 ipt> <a href=//w
0740: 77 77 2e 62 61 69 64 75 2e 63 6f 6d 2f 6d 6f 72 ww.baidu.com/mor
0750: 65 2f 20 6e 61 6d 65 3d 74 6a 5f 62 72 69 69 63 e/ name=tj_briic
0760: 6f 6e 20 63 6c 61 73 73 3d 62 72 69 20 73 74 79 on class=bri sty
0770: 6c 65 3d 22 64 69 73 70 6c 61 79 3a 20 62 6c 6f le="display: blo
0780: 63 6b 3b 22 3e e6 9b b4 e5 a4 9a e4 ba a7 e5 93 ck;">...........
0790: 81 3c 2f 61 3e 20 3c 2f 64 69 76 3e 20 3c 2f 64 .</a> </div> </d
07a0: 69 76 3e 20 3c 2f 64 69 76 3e 20 3c 64 69 76 20 iv> </div> <div
07b0: 69 64 3d 66 74 43 6f 6e 3e 20 3c 64 69 76 20 69 id=ftCon> <div i
07c0: 64 3d 66 74 43 6f 6e 77 3e 20 3c 70 20 69 64 3d d=ftConw> <p id=
07d0: 6c 68 3e 20 3c 61 20 68 72 65 66 3d 68 74 74 70 lh> <a href=http
07e0: 3a 2f 2f 68 6f 6d 65 2e 62 61 69 64 75 2e 63 6f ://home.baidu.co
07f0: 6d 3e e5 85 b3 e4 ba 8e e7 99 be e5 ba a6 3c 2f m>............</
0800: 61 3e 20 3c 61 20 68 72 65 66 3d 68 74 74 70 3a a> <a href=http:
0810: 2f 2f 69 72 2e 62 61 69 64 75 2e 63 6f 6d 3e 41 //ir.baidu.com>A
0820: 62 6f 75 74 20 42 61 69 64 75 3c 2f 61 3e 20 3c bout Baidu</a> <
0830: 2f 70 3e 20 3c 70 20 69 64 3d 63 70 3e 26 63 6f /p> <p id=cp>&co
0840: 70 79 3b 32 30 31 37 26 6e 62 73 70 3b 42 61 69 py;2017 Bai
0850: 64 75 26 6e 62 73 70 3b 3c 61 20 68 72 65 66 3d du <a href=
0860: 68 74 74 70 3a 2f 2f 77 77 77 2e 62 61 69 64 75 http://www.baidu
0870: 2e 63 6f 6d 2f 64 75 74 79 2f 3e e4 bd bf e7 94 .com/duty/>.....
0880: a8 e7 99 be e5 ba a6 e5 89 8d e5 bf 85 e8 af bb ................
0890: 3c 2f 61 3e 26 6e 62 73 70 3b 20 3c 61 20 68 72 </a> <a hr
08a0: 65 66 3d 68 74 74 70 3a 2f 2f 6a 69 61 6e 79 69 ef=http://jianyi
08b0: 2e 62 61 69 64 75 2e 63 6f 6d 2f 20 63 6c 61 73 .baidu.com/ clas
08c0: 73 3d 63 70 2d 66 65 65 64 62 61 63 6b 3e e6 84 s=cp-feedback>..
08d0: 8f e8 a7 81 e5 8f 8d e9 a6 88 3c 2f 61 3e 26 6e ..........</a>&n
08e0: 62 73 70 3b e4 ba ac 49 43 50 e8 af 81 30 33 30 bsp;...ICP...030
08f0: 31 37 33 e5 8f b7 26 6e 62 73 70 3b 20 3c 69 6d 173... <im
0900: 67 20 73 72 63 3d 2f 2f 77 77 77 2e 62 61 69 64 g src=//www.baid
0910: 75 2e 63 6f 6d 2f 69 6d 67 2f 67 73 2e 67 69 66 u.com/img/gs.gif
0920: 3e 20 3c 2f 70 3e 20 3c 2f 64 69 76 3e 20 3c 2f > </p> </div> </
0930: 64 69 76 3e 20 3c 2f 64 69 76 3e 20 3c 2f 62 6f div> </div> </bo
0940: 64 79 3e 20 3c 2f 68 74 6d 6c 3e 0d 0a dy> </html>..
== Info: Connection #0 to host www.baidu.com left intact
或者
$ curl --trace-ascii output.txt www.sina.com
$ cat output1.txt
== Info: About to connect() to www.baidu.com port 80 (#0)
== Info: Trying 180.101.49.14...
== Info: Connected to www.baidu.com (180.101.49.14) port 80 (#0)
=> Send header, 77 bytes (0x4d)
0000: GET / HTTP/1.1
0010: User-Agent: curl/7.29.0
0029: Host: www.baidu.com
003e: Accept: */*
004b:
<= Recv header, 17 bytes (0x11)
0000: HTTP/1.1 200 OK
<= Recv header, 22 bytes (0x16)
0000: Accept-Ranges: bytes
<= Recv header, 76 bytes (0x4c)
0000: Cache-Control: private, no-cache, no-store, proxy-revalidate, no
0040: -transform
<= Recv header, 24 bytes (0x18)
0000: Connection: keep-alive
<= Recv header, 22 bytes (0x16)
0000: Content-Length: 2381
<= Recv header, 25 bytes (0x19)
0000: Content-Type: text/html
<= Recv header, 37 bytes (0x25)
0000: Date: Mon, 14 Nov 2022 06:14:28 GMT
<= Recv header, 22 bytes (0x16)
0000: Etag: "588604c8-94d"
<= Recv header, 46 bytes (0x2e)
0000: Last-Modified: Mon, 23 Jan 2017 13:27:36 GMT
<= Recv header, 18 bytes (0x12)
0000: Pragma: no-cache
<= Recv header, 22 bytes (0x16)
0000: Server: bfe/1.0.8.18
<= Recv header, 67 bytes (0x43)
0000: Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=
0040: /
<= Recv header, 2 bytes (0x2)
0000:
<= Recv data, 1040 bytes (0x410)
0000: <!DOCTYPE html>
0011: <!--STATUS OK--><html> <head><meta http-equiv=content-type conte
0051: nt=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible cont
0091: ent=IE=Edge><meta content=always name=referrer><link rel=stylesh
00d1: eet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/
0111: baidu.min.css><title>...........................</title></head>
0151: <body link=#0000cc> <div id=wrapper> <div id=head> <div class=he
0191: ad_wrapper> <div class=s_form> <div class=s_form_wrapper> <div i
01d1: d=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png w
0211: idth=270 height=129> </div> <form id=form name=f action=//www.ba
0251: idu.com/s class=fm> <input type=hidden name=bdorz_come value=1>
0291: <input type=hidden name=ie value=utf-8> <input type=hidden name=
02d1: f value=8> <input type=hidden name=rsv_bp value=1> <input type=h
0311: idden name=rsv_idx value=1> <input type=hidden name=tn value=bai
0351: du><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt va
0391: lue maxlength=255 autocomplete=off autofocus></span><span class=
03d1: "bg s_btn_wr"><input type=submit id=su value=............ class
<= Recv data, 1341 bytes (0x53d)
0000: ="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=ht
0040: tp://news.baidu.com name=tj_trnews class=mnav>......</a> <a href
0080: =http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a
00c0: href=http://map.baidu.com name=tj_trmap class=mnav>......</a> <
0100: a href=http://v.baidu.com name=tj_trvideo class=mnav>......</a>
0140: <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>......
0180: </a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?log
01c0: in&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3
0200: d1 name=tj_login class=lb>......</a> </noscript> <script>documen
0240: t.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl
0280: =mn&u='+ encodeURIComponent(window.location.href+ (window.locati
02c0: on.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_logi
0300: n" class="lb">......</a>');</script> <a href=//www.baidu.com/mor
0340: e/ name=tj_briicon class=bri style="display: block;">...........
0380: .</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=
03c0: lh> <a href=http://home.baidu.com>............</a> <a href=http:
0400: //ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Bai
0440: du <a href=http://www.baidu.com/duty/>.....................
0480: </a> <a href=http://jianyi.baidu.com/ class=cp-feedback>..
04c0: ..........</a> ...ICP...030173... <img src=//www.baid
0500: u.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
== Info: Connection #0 to host www.baidu.com left intact
2.5、發(fā)送表單信息
發(fā)送表單信息有 GET 和 POST 兩種方法。GET 方法相對簡單,只要把數(shù)據(jù)附在網(wǎng)址后面就行。
$ curl example.com/form.cgi?data=xxx
POST 方法必須把數(shù)據(jù)和網(wǎng)址分開,curl 就要用到 --data
或者 -d
參數(shù)。
$ curl -X POST --data "data=xxx" example.com/form.cgi
curl -H "Content-Type: application/json" -X POST -d "{\"abc\":123}" "https://httpbin.org/post"
json數(shù)據(jù)放在一個文件里
curl -H "Content-Type: application/json" -X POST -d @test.json URL
如果你的數(shù)據(jù)沒有經(jīng)過表單編碼,還可以讓 curl 為你編碼,參數(shù)是 --data-urlencode
。
$ curl -X POST--data-urlencode "date=April 1" example.com/form.cgi
2.6、HTTP動詞
curl 默認的 HTTP 動詞是 GET,使用 -X
參數(shù)可以支持其他動詞。
$ curl -X POST www.example.com
$ curl -X DELETE www.example.com
2.7、User Agent字段
這個字段是用來表示客戶端的設(shè)備信息。服務(wù)器有時會根據(jù)這個字段,針對不同設(shè)備,返回不同格式的網(wǎng)頁,比如手機版和桌面版。
瀏覽器的 User Agent 是:
Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36
curl 可以這樣模擬:
$ curl --user-agent "[User Agent]" [URL]
2.8、cookie
使用 --cookie
參數(shù),可以讓 curl 發(fā)送 cookie。
$ curl --cookie "name=xxx" www.example.com
至于具體的 cookie 的值,可以從 http response 頭信息的 Set-Cookie
字段中得到。
2.9、增加頭信息
有時需要在 http request 之中,自行增加一個頭信息。--header
參數(shù)就可以起到這個作用。
$ curl --header "Content-Type:application/json" http://example.com
2.10、HTTP認證
有些網(wǎng)域需要 HTTP 認證,這時 curl 需要用到 --user
或者 -u
參數(shù)。
$ curl --user name:password example.com
2.11、上傳文件(option:-T)
curl -T dodo1.JPG -u 用戶名:密碼 ftp://www.linux.com/img/
2.12、偽造來源頁面|偽造referer|盜鏈 (option:-e)
很多服務(wù)器會檢查http訪問的referer從而來控制訪問。比如:你是先訪問首頁,然后再訪問首頁中的郵箱頁面,這里訪問郵箱的referer地址就是訪問首頁成功后的頁面地址,如果服務(wù)器發(fā)現(xiàn)對郵箱頁面訪問的referer地址不是首頁的地址,就斷定那是個盜鏈了,這樣就會讓服務(wù)器以為你是從www.linux.com點擊某個鏈接過來的
curl -e "www.linux.com" http://mail.linux.com
#告訴愛E族,我是從百度來的
curl -e http://baidu.com http://aiezu.com
2.13、偽造代理設(shè)備(模仿瀏覽器)
有些網(wǎng)站需要使用特定的瀏覽器去訪問他們,有些還需要使用某些特定的版本。curl內(nèi)置option:-A可以讓我們指定瀏覽器去訪問網(wǎng)站
curl -A "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.0)" http://www.linux.com
#告訴愛E族,我是GOOGLE爬蟲蜘蛛(其實我是curl命令)
curl -A " Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" http://aiezu.com
#告訴愛E族,我用的是微信內(nèi)置瀏覽器
curl -A "Mozilla/5.0 AppleWebKit/600 Mobile MicroMessenger/6.0" http://aiezu.com
2.14、設(shè)置http請求指定proxy服務(wù)器以及其端口(option::-x)
很多時候上網(wǎng)需要用到代理服務(wù)器(比如是使用代理服務(wù)器上網(wǎng)或者因為使用curl別人網(wǎng)站而被別人屏蔽IP地址的時候),幸運的是curl通過使用內(nèi)置option:-x來支持設(shè)置代理
curl -x 192.168.100.100:1080 http://www.linux.com
2.15、保存http的response里面的header信息(option:-D)
curl -D cookied.txt http://www.linux.com
執(zhí)行后cookie信息就被存到了cookied.txt里面了
注意:-c(小寫)產(chǎn)生的cookie和-D里面的cookie是不一樣的。
2.16、顯示抓取錯誤
curl -f http://www.linux.com/error
三、curl命令參數(shù)詳解
由于linux curl功能十分強大,所以命令參數(shù)十分多,下表只是篩選出來的部分參數(shù),更多參數(shù)請運行“man curl”命令查看。
參數(shù)組 | 參數(shù) | 描述 |
---|---|---|
url | url | 需要抓取的一到多個URLs; 多個下面通配符的方式: 1、http://{www,ftp,mail}.aiezu.com; 2、http://aiezu.com/images/[001-999].jpg; 3、http://aiezu.com/images/[1-999].html; 4、ftp://aiezu.com/file[a-z].txt |
請 求 頭 | -H “name: value” --header “name: value” | (HTTP)添加一個http header(http請求頭); |
-H “name:” --header “name:” | (HTTP)移除一個http header(http請求頭); | |
-A “string” --user-agent “string” 【參考】 | (HTTP)設(shè)置Http請求頭“User-Agent”,服務(wù)器通過“User-Agent”可以判斷客戶端使用的瀏覽器名稱和操作系統(tǒng)類型,偽造此參數(shù)能導致服務(wù)器做出錯誤判斷。 也可以使用“-H”, “–header option”設(shè)置此選項; | |
-e --referer 【參考】 | (HTTP)設(shè)置訪問時的來源頁面,告訴http服務(wù)從哪個頁面進入到此頁面; -e "aiezu.com"相當于“-H “Referer: www.qq.com””; | |
響 應(yīng) 頭 | -I --head | (HTTP)只輸出HTTP-header,不獲取內(nèi)容(HTTP/FTP/FILE)。 用于HTTP服務(wù)時,獲取頁面的http頭; (如:curl -I http://aiezu.com) 用于FTP/FILE時,將會獲取文件大小、最后修改時間; (如:curl -I file://test.txt) |
-i --include | (HTTP)輸出HTTP頭和返回內(nèi)容; | |
-D --dump-header | (HTTP)轉(zhuǎn)儲http響應(yīng)頭到指定文件; | |
cookie | -b name=data --cookie name=data 【參考】 | (HTTP)發(fā)送cookie數(shù)據(jù)到HTTP服務(wù)器,數(shù)據(jù)格式為:“NAME1=VALUE1; NAME2=VALUE2”; 如果行中沒有“=”,將把參數(shù)值當作cookie文件名; 這個cookie數(shù)據(jù)可以是由服務(wù)器的http響應(yīng)頭“Set-Cookie:”行發(fā)送過來的; |
-c filename --cookie-jar file name 【參考】 | (HTTP)完成操作后將服務(wù)器返回的cookies保存到指定的文件; 指定參數(shù)值為“-”將定向到標準輸出“如控制臺”; | |
-j --junk-session-cookies | (HTTP)告訴curl放棄所有的"session cookies"; 相當于重啟瀏覽器; | |
代理 | -x host:port -x [protocol://[user:pwd@]host[:port] --proxy [protocol://[user:pwd@]host[:port] 【參考】 | 使用HTTP代理訪問;如果未指定端口,默認使用8080端口; protocol默認為http_proxy,其他可能的值包括: http_proxy、HTTPS_PROXY、socks4、socks4a、socks5; 如: --proxy 8.8.8.8:8080; -x “http_proxy://aiezu:123@aiezu.com:80” |
-p --proxytunnel | 將“-x”參數(shù)的代理,作為通道的方式去代理非HTTP協(xié)議,如ftp; | |
–socks4 <host[:port]> --socks4a <host[:port]> --socks5 <host[:port]> 【參考】 | 使用SOCKS4代理; 使用SOCKS4A代理; 使用SOCKS5代理; 此參數(shù)會覆蓋“-x”參數(shù); | |
–proxy-anyauth --proxy-basic --proxy-diges --proxy-negotiate --proxy-ntlm | http代理認證方式,參考: --anyauth --basic --diges --negotiate --ntlm | |
-U user:password --proxy-user user:password | 設(shè)置代理的用戶名和密碼; | |
數(shù)據(jù) 傳輸 | -G --get 【參考】 | 如果使用了此參數(shù),“-d/”、“–data”、“–data-binary”參數(shù)設(shè)置的數(shù)據(jù),講附加在url上,以GET的方式請求; |
-d @file -d “string” --data “string” --data-ascii “string” --data-binary “string” --data-urlencode “string” 【參考】 | (HTTP)使用HTTP POST方式發(fā)送“key/value對”數(shù)據(jù),相當于瀏覽器表單屬性(method=“POST”,enctype=“application/x-www-form-urlencoded”) -d,–data:HTTP方式POST數(shù)據(jù); --data-ascii:HTTP方式POST ascii數(shù)據(jù); --data-binary:HTTP方式POST二進制數(shù)據(jù); --data-urlencode:HTTP方式POST數(shù)據(jù)(進行urlencode); 如果數(shù)據(jù)以“@”開頭,后緊跟一個文件,將post文件內(nèi)的內(nèi)容; | |
-F name=@file -F name=<file -F name=content --form name=content 【參考】 | (HTTP)使用HTTP POST方式發(fā)送類似“表單字段”的多類型數(shù)據(jù),相當于同時設(shè)置瀏覽器表單屬性(method=“POST”,enctype=“multipart/form-data”),可以使用此參數(shù)上傳二進制文件。 如果字段內(nèi)容以“@”開頭,剩下的部分應(yīng)該是文件名,curl將會上傳此文件,如: curl -F “pic=@pic.jpg” http://aiezu.com; curl -F “page=@a.html;type=text/html” http://aiezu.com curl -F “page=@/tmp/a;filename=a.txt” http://aiezu.com 如果字段內(nèi)容以“<”開頭,剩下的部分應(yīng)該是文件名,curl將從文件中獲取作為此字段的值,如:curl -F “text=<text.txt” http://aiezu.com; | |
–form-string <key=value> | (HTTP)類似于“–form”,但是“@”、“<”無特殊含義; | |
-T file --upload-file file | 通過“put”的方式將文件傳輸?shù)竭h程網(wǎng)址; 選項參數(shù)只使用字符"-",將通過stdin讀入文件內(nèi)容; 如: cat test.txtcurl “http://aiezu.com/a.php” -T - curl “http://aiezu.com/a.php” -T - <test.txt 此參數(shù)也可以使用通配符: curl -T “{file1,file2}” http://aiezu.com curl -T “img[1-1000].png” http://aiezu.com | |
斷點 續(xù)傳 | -C --continue-at | 斷點續(xù)轉(zhuǎn),從文件頭的指定位置開始繼續(xù)下載/上傳; offset續(xù)傳開始的位置,如果offset值為“-”,curl會自動從文件中識別起始位置開始傳輸; |
-r --range | (HTTP/FTP/SFTP/FILE) 只傳輸內(nèi)容的指定部分: 0-499:最前面500字節(jié); -500:最后面500字節(jié); 9500-:最前面9500字節(jié); 0-0,-1:最前面和最后面的1字節(jié); 100-199,500-599:兩個100字節(jié); | |
認證 | –basic | (HTTP)告訴curl使用HTTP Basic authentication(HTTP協(xié)議時),這是默認認證方式; |
–ntlm | (HTTP)使用NTLM身份驗證方式,用于HTTP協(xié)議; 一般用于IIS使用NTLM的網(wǎng)站; | |
–digest | (HTTP)使用HTTP Digest authentication加密,用于HTTP協(xié)議; 配合“-u/–user”選項,防止密碼使用明文方式發(fā)送; | |
–negotiate | (HTTP)使用GSS-Negotiate authentication方式,用于HTTP協(xié)議; 它主要目的是為它的主要目的是為kerberos5認證提供支持支持; | |
–anyauth | –anyauth | (HTTP)告訴curl自動選擇合適的身份認證方法,并選用最安全的方式; |
-u user:password --user user:password | 使用用戶名、密碼認證,此參數(shù)會覆蓋“-n”、“–netrc”和“–netrc-optional”選項; 如果你只提供用戶名,curl將要求你輸入密碼; 如果你使用“SSPI”開啟的curl庫做“NTLM”認證,可以使用不含用戶名密碼的“-u:”選項,強制curl使用當前登錄的用戶名密碼進行認證; 此參數(shù)相當于設(shè)置http頭“Authorization:”; | |
證書 | -E <證書[:密碼]> --cert <證書[:密碼]> | (SSL)指定“PEM”格式的證書文件和證書密碼; |
–cert-type | (SSL)告訴curl所提供證書的類型:PEM、DER、ENG等; 默認為“PEM”; | |
–cacert <CA證書> | (SSL)告訴curl所以指定的CA證書文件,必須是“PEM”格式; | |
–capath <CA證書路徑> | (SSL)告訴curl所以指定目錄下的CA證書用來驗證; 這些證書必須是“PEM”格式; | |
–crlfile | (HTTPS/FTPS)提供一個PEM格式的文件,用于指定被吊銷的證書列表; | |
-k --insecure | (SSL)設(shè)置此選項將允許使用無證書的不安全SSL進行連接和傳輸。 | |
SSL 其他 | –ciphers | (SSL)指定SSL要使用的加密方式;如:“aes_256_sha_256”; |
–engine | 設(shè)置一個OpenSSL加密引擎用于加密操作; 使用“curl --engine list”查看支持的加密引擎列表; | |
–random-file | (SSL)指定包含隨機數(shù)據(jù)的文件路徑名;數(shù)據(jù)是用來為SSL連接產(chǎn)生隨機種子為; | |
–egd-file | (SSL)為隨機種子生成器EGD(Entropy Gathering Daemon socket)指定的路徑名; | |
-1/–tlsv1 --tlsv1.0 --tlsv1.1 --tlsv1.2 -2/–sslv2 -3/–sslv3 | (SSL)使用TLS版本2與遠程服務(wù)器通訊; (SSL)使用TLS 1.0版本與遠程服務(wù)器通訊; (SSL)使用TLS 1.1版本與遠程服務(wù)器通訊; (SSL)使用TLS 1.2版本與遠程服務(wù)器通訊; (SSL)使用SSL版本2與遠程服務(wù)器通訊; (SSL)使用SSL版本3與遠程服務(wù)器通訊; | |
私鑰 公鑰 | –key | (SSL/SSH)指定一個私鑰文件名;為指定時自動嘗試使用下面文件:“/.ssh/id_rsa”、“/.ssh/id_dsa”、“./id_rsa’”、 “./id_dsa”; |
–key-type | (SSL)指定私鑰文件類型,支持:DER、PEM、ENG,默認是PEM; | |
–pass | (SSL/SSH)指定私鑰文件的密碼; | |
–pubkey | (SSH)使用指定文件提供的您公鑰; | |
FTP | -P --ftp-port <接口> | (FTP)FTP主動模式時,設(shè)置一個地址等待服務(wù)器的連接,如: 網(wǎng)卡:eth1 IP:8.8.8.8 主機名:aiezu.com 可以加端口號:eth1:20000-21000; |
–crlf | (FTP)上傳時將換行符(LF)轉(zhuǎn)換為回車換行(CRLF); | |
–ftp-account [data] | (FTP)ftp帳號信息; | |
–ftp-method [method] | (FTP)可選值:multicwd/nocwd/singlecwd; | |
–ftp-pasv | (FTP)使用使用PASV(被動)/EPSV模式; | |
–ftp-skip-pasv-ip | (FTP)使用PASV的時,跳過指定IP; | |
–ftp-create-dirs | (FTP)上傳時自動創(chuàng)建遠程目錄; | |
-l --list-only | (FTP)列出ftp文件列表; | |
-B --use-ascii | (FTP/LDAP)使用Ascii傳輸模式,用于FTP、LDAP;在ftp中相當與使用了“type=A;”模式。 | |
–disable-epsv | (FTP)告訴curl在PASV(被動模式)時不要使用EPSV; | |
–disable-eprt | (FTP)告訴curl在主動模式時禁用EPRT和LPRT; | |
限速 | –limit-rate | 限制curl使用的最大帶寬;如果未指定單位,默認單位為“bytes/秒”,你也可以指定單位為“K”、“M”、“G”等單位,如:“–limit-rate 1m”為限制最大使用帶寬為“1m字節(jié)/秒”; |
-y --speed-time | If a download is slower than speed-limit bytes per second during a speed-time period, the download gets aborted. If speed-time is used, the default speed-limit will be 1 unless set with -Y. This option controls transfers and thus will not affect slow connects etc. If this is a concern for you, try the --connect-timeout option. | |
-Y --speed-limit | If a download is slower than this given speed (in bytes per second) for speed-time seconds it gets aborted. speed-time is set with -y and is 30 if not set. | |
其他 選項 | -0/–http1.0 | (HTTP) 強制curl使用HTTP 1.0而不是使用默認的HTTP 1.1; |
–interface | 使用指定的網(wǎng)卡接口訪問; curl --interface eth0 http://aiezu.com curl --interface 10.0.0.101 http://aiezu.com | |
-X --request | (HTTP)指定與服務(wù)器通信使用的請求方法,如:GET、PUT、POST、DELETE等,默認GET; | |
–keepalive-time | 設(shè)置keepalive時間 | |
–no-keepalive | 關(guān)閉keepalive功能; | |
–no-buffer | 禁用對輸出流緩沖; | |
–buffer | 啟用輸出流緩沖; | |
-L --location | (HTTP/HTTPS)追隨http響應(yīng)頭“Location:”定向到跳轉(zhuǎn)后的頁面; (在http響應(yīng)碼為3XX時使用,如301跳轉(zhuǎn)、302跳轉(zhuǎn)) | |
–location-trusted | (HTTP/HTTPS)同“–location”,但跳轉(zhuǎn)后會發(fā)送跳轉(zhuǎn)前的用戶名和密碼; | |
–compressed | (HTTP)請求對返回內(nèi)容使用壓縮算法進行壓縮;curl支持對gzip壓縮進行解壓; | |
–connect-timeout | 指定最大連接超時,單位“秒”; | |
-m seconds --max-time seconds | 限制整個curl操作的最長時間,單位為秒; | |
-s --silent | 安靜模式。不要顯示進度表或錯誤消息; | |
-# --progress-bar | 顯示進度條; | |
錯誤 選項 | -f --fail | (HTTP)連接失敗時(400以上錯誤)不返回默認錯誤頁面,而是返回一個curl錯誤碼“22”; |
–retry --retry-delay --retry-max-time | 失敗重試次數(shù); 重試間隔時間; 最大重試時間; | |
-S --show-error | 安靜模式下顯示錯誤信息; | |
–stderr | 錯誤信息保存文件; | |
輸出 | -o file --output file | 將返回內(nèi)容輸出到文件。 如果是用過通配符獲取多個url,可以使用“#”后跟“數(shù)字序號”,curl會自動將它替換對應(yīng)的關(guān)鍵詞,如: curl “http://aiezu.com/{a,b}.txt” -o “#1.txt”; 將保存為:“a.txt”,“b.txt”; curl “http://aiezu.com/{a,b}_[1-3].txt” -o “#1#2.txt”; 將保存為:a1.txt、a2.txt、a3.txt、b1.txt、b2.txt、b3.txt 如果要根據(jù)規(guī)則創(chuàng)建保存目錄,參考:“–create-dirs” 指定“-”將定向到標準輸出“如控制臺”; |
-O --remote-name | 將返回內(nèi)容輸出到當前目錄下,和url中文件名相同的文件中(不含目錄); | |
–create-dirs | 與“-o”參數(shù)配合使用,創(chuàng)建必要的本地目錄層次結(jié)構(gòu) | |
-w --write-out format | 操作完成后在返回信息尾部追加指定的內(nèi)容;要追加的內(nèi)容可以是一個字符串“string”、從文件中獲取“@filename”、從標準輸入中獲取“@-” 格式參數(shù)中可以用%{variable_name} 方式使用響應(yīng)信息的相關(guān)變量,如:%{content_type}、%{http_code}、%{local_ip}…,更多變量參考“man curl”獲取; 格式參數(shù)可以使用“\n”、“\r”、“\t”等轉(zhuǎn)義字符; | |
調(diào)試 | –trace | 轉(zhuǎn)儲所有傳入和傳出的數(shù)據(jù)到文件,包括描述信息; 使用“-”作為文件名將輸出發(fā)送到標準輸出。 |
–trace-ascii file | 轉(zhuǎn)儲所有傳入和傳出的數(shù)據(jù)到文件,包括描述信息,只轉(zhuǎn)儲ASCII部分,更容易閱讀; 使用“-”作為文件名將輸出發(fā)送到標準輸出。 這個選項會覆蓋之前使用的-v、 --verbose、 --trace-ascii選項; | |
–trace-time | 轉(zhuǎn)儲文件中添加時間信息; | |
-K --config | 從配置文件中讀取參數(shù),參考:http://curl.haxx.se/docs/ | |
-v --verbose | 顯示更詳細的信息,調(diào)試時使用; | |
幫助 | -M --manual | 顯示完整的幫助手冊; |
-h --help | linux curl用法幫助; |
curl -h 的完整參數(shù)解釋:
-a/–append | 上傳文件時,附加到目標文件 |
---|---|
–anyauth | 可以使用“任何”身份驗證方法 |
–basic | 使用HTTP基本驗證 |
-B/–use-ascii | 使用ASCII文本傳輸 |
-d/–data | HTTP POST方式傳送數(shù)據(jù) |
–data-ascii | 以ascii的方式post數(shù)據(jù) |
–data-binary | 以二進制的方式post數(shù)據(jù) |
–negotiate | 使用HTTP身份驗證 |
–digest | 使用數(shù)字身份驗證 |
–disable-eprt | 禁止使用EPRT或LPRT |
–disable-epsv | 禁止使用EPSV |
–egd-file | 為隨機數(shù)據(jù)(SSL)設(shè)置EGD socket路徑 |
–tcp-nodelay | 使用TCP_NODELAY選項 |
-E/–cert <cert[:passwd]> | 客戶端證書文件和密碼 (SSL) |
–cert-type | 證書文件類型 (DER/PEM/ENG) (SSL) |
–key | 私鑰文件名 (SSL) |
–key-type | 私鑰文件類型 (DER/PEM/ENG) (SSL) |
–pass | 私鑰密碼 (SSL) |
–engine | 加密引擎使用 (SSL). “–engine list” for list |
–cacert | CA證書 (SSL) |
–capath | CA目 (made using c_rehash) to verify peer against (SSL) |
–ciphers | SSL密碼 |
–compressed | 要求返回是壓縮的形勢 (using deflate or gzip) |
–connect-timeout | 設(shè)置最大請求時間 |
–create-dirs | 建立本地目錄的目錄層次結(jié)構(gòu) |
–crlf | 上傳是把LF轉(zhuǎn)變成CRLF |
–ftp-create-dirs | 如果遠程目錄不存在,創(chuàng)建遠程目錄 |
–ftp-method [multicwd/nocwd/singlecwd] | 控制CWD的使用 |
–ftp-pasv | 使用 PASV/EPSV 代替端口 |
–ftp-skip-pasv-ip | 使用PASV的時候,忽略該IP地址 |
–ftp-ssl | 嘗試用 SSL/TLS 來進行ftp數(shù)據(jù)傳輸 |
–ftp-ssl-reqd | 要求用 SSL/TLS 來進行ftp數(shù)據(jù)傳輸 |
-F/–form <name=content> | 模擬http表單提交數(shù)據(jù) |
-form-string <name=string> | 模擬http表單提交數(shù)據(jù) |
-g/–globoff | 禁用網(wǎng)址序列和范圍使用{}和[] |
-G/–get | 以get的方式來發(fā)送數(shù)據(jù) |
-h/–help | 幫助 |
-H/–header
|
自定義頭信息傳遞給服務(wù)器 |
–ignore-content-length | 忽略的HTTP頭信息的長度 |
-i/–include | 輸出時包括protocol頭信息 |
-I/–head | 只顯示文檔信息 |
-j/–junk-session-cookies | 讀取文件時忽略session cookie |
–interface | 使用指定網(wǎng)絡(luò)接口/地址 |
–krb4 | 使用指定安全級別的krb4 |
-k/–insecure | 允許不使用證書到SSL站點 |
-K/–config | 指定的配置文件讀取 |
-l/–list-only | 列出ftp目錄下的文件名稱 |
–limit-rate | 設(shè)置傳輸速度 |
–local-port | 強制使用本地端口號 |
-m/–max-time | 設(shè)置最大傳輸時間 |
–max-redirs | 設(shè)置最大讀取的目錄數(shù) |
–max-filesize | 設(shè)置最大下載的文件總量 |
-M/–manual | 顯示全手動 |
-n/–netrc | 從netrc文件中讀取用戶名和密碼 |
–netrc-optional | 使用 .netrc 或者 URL來覆蓋-n |
–ntlm | 使用 HTTP NTLM 身份驗證 |
-N/–no-buffer | 禁用緩沖輸出 |
-p/–proxytunnel | 使用HTTP代理 |
–proxy-anyauth | 選擇任一代理身份驗證方法 |
–proxy-basic | 在代理上使用基本身份驗證 |
–proxy-digest | 在代理上使用數(shù)字身份驗證 |
–proxy-ntlm | 在代理上使用ntlm身份驗證 |
-P/–ftp-port | 使用端口地址,而不是使用PASV |
-Q/–quote | 文件傳輸前,發(fā)送命令到服務(wù)器 |
–range-file | 讀取(SSL)的隨機文件 |
-R/–remote-time | 在本地生成文件時,保留遠程文件時間 |
–retry | 傳輸出現(xiàn)問題時,重試的次數(shù) |
–retry-delay | 傳輸出現(xiàn)問題時,設(shè)置重試間隔時間 |
–retry-max-time | 傳輸出現(xiàn)問題時,設(shè)置最大重試時間 |
-S/–show-error | 顯示錯誤 |
–socks4 <host[:port]> | 用socks4代理給定主機和端口 |
–socks5 <host[:port]> | 用socks5代理給定主機和端口 |
-t/–telnet-option <OPT=val> | Telnet選項設(shè)置 |
–trace | 對指定文件進行debug |
–trace-ascii | Like --跟蹤但沒有hex輸出 |
–trace-time | 跟蹤/詳細輸出時,添加時間戳 |
–url | Spet URL to work with |
-U/–proxy-user <user[:password]> | 設(shè)置代理用戶名和密碼 |
-V/–version | 顯示版本信息 |
-X/–request | 指定什么命令 |
-y/–speed-time | 放棄限速所要的時間。默認為30 |
-Y/–speed-limit | 停止傳輸速度的限制,速度時間’秒 |
-z/–time-cond | 傳送時間設(shè)置 |
-0/–http1.0 | 使用HTTP 1.0 |
-1/–tlsv1 | 使用TLSv1(SSL) |
-2/–sslv2 | 使用SSLv2的(SSL) |
-3/–sslv3 | 使用的SSLv3(SSL) |
–3p-quote | like -Q for the source URL for 3rd party transfer |
–3p-url | 使用url,進行第三方傳送 |
–3p-user | 使用用戶名和密碼,進行第三方傳送 |
-4/–ipv4 | 使用IP4 |
-6/–ipv6 | 使用IP6 |
四、Linux curl命令退出碼
下面是linux curl命令的錯誤代碼和她們的相應(yīng)的錯誤消息,可能會出現(xiàn)在惡劣的環(huán)境。
退出碼 | 錯誤描述 |
---|---|
1 | Unsupported protocol. This build of curl has no support for this protocol. |
2 | Failed to initialize. |
3 | URL malformed. The syntax was not correct. |
5 | Couldn’t resolve proxy. The given proxy host could not be resolved. |
6 | Couldn’t resolve host. The given remote host was not resolved. |
7 | Failed to connect to host. |
8 | FTP weird server reply. The server sent data curl couldn’t parse. |
9 | FTP access denied. The server denied login or denied access to the particular resource or directory you wanted to reach. Most often you tried to change to a directory that doesn’t exist on the server. |
11 | FTP weird PASS reply. Curl couldn’t parse the reply sent to the PASS request. |
13 | FTP weird PASV reply, Curl couldn’t parse the reply sent to the PASV request. |
14 | FTP weird 227 format. Curl couldn’t parse the 227-line the server sent. |
15 | FTP can’t get host. Couldn’t resolve the host IP we got in the 227-line. |
17 | FTP couldn’t set binary. Couldn’t change transfer method to binary. |
18 | Partial file. Only a part of the file was transferred. |
19 | FTP couldn’t download/access the given file, the RETR (or similar) command failed. |
21 | FTP quote error. A quote command returned error from the server. |
22 | HTTP page not retrieved. The requested url was not found or returned another error with the HTTP error code being 400 or above. This return code only appears if -f/–fail is used. |
23 | Write error. Curl couldn’t write data to a local filesystem or similar. |
25 | FTP couldn’t STOR file. The server denied the STOR operation, used for FTP uploading. |
26 | Read error. Various reading problems. |
27 | Out of memory. A memory allocation request failed. |
28 | Operation timeout. The specified time-out period was reached according to the conditions. |
30 | FTP PORT failed. The PORT command failed. Not all FTP servers support the PORT command, try doing a transfer using PASV instead! |
31 | FTP couldn’t use REST. The REST command failed. This command is used for resumed FTP transfers. |
33 | HTTP range error. The range “command” didn’t work. |
34 | HTTP post error. Internal post-request generation error. |
35 | SSL connect error. The SSL handshaking failed. |
36 | FTP bad download resume. Couldn’t continue an earlier aborted download. |
37 | FILE couldn’t read file. Failed to open the file. Permissions? |
38 | LDAP cannot bind. LDAP bind operation failed. |
39 | LDAP search failed. |
41 | Function not found. A required LDAP function was not found. |
42 | Aborted by callback. An application told curl to abort the operation. |
43 | Internal error. A function was called with a bad parameter. |
45 | Interface error. A specified outgoing interface could not be used. |
47 | Too many redirects. When following redirects, curl hit the maximum amount. |
48 | Unknown TELNET option specified. |
49 | Malformed telnet option. |
51 | The peer’s SSL certificate or SSH MD5 fingerprint was not ok. |
52 | The server didn’t reply anything, which here is considered an error. |
53 | SSL crypto engine not found. |
54 | Cannot set SSL crypto engine as default. |
55 | Failed sending network data. |
56 | Failure in receiving network data. |
58 | Problem with the local certificate. |
59 | Couldn’t use specified SSL cipher. |
60 | Peer certificate cannot be authenticated with known CA certificates. |
61 | Unrecognized transfer encoding. |
62 | Invalid LDAP URL. |
63 | Maximum file size exceeded. |
64 | Requested FTP SSL level failed. |
65 | Sending the data requires a rewind that failed. |
66 | Failed to initialize SSL Engine. |
67 | The user name, password, or similar was not accepted and curl failed to log in. |
68 | File not found on TFTP server. |
69 | Permission problem on TFTP server. |
70 | Out of disk space on TFTP server. |
71 | Illegal TFTP operation. |
72 | Unknown TFTP transfer ID. |
73 | File already exists (TFTP). |
74 | No such user (TFTP). |
75 | Character conversion failed. |
76 | Character conversion functions required. |
77 | Problem with reading the SSL CA cert (path? access rights?). |
78 | The resource referenced in the URL does not exist. |
79 | An unspecified error occurred during the SSH session. |
80 | Failed to shut down the SSL connection. |
82 | Could not load CRL file, missing or wrong format (added in 7.19.0). |
83 | Issuer check failed (added in 7.19.0). |
XX | More error codes will appear here in future releases. The existing ones are meant to never change. |
五、curl 下載功能
5.1、下載頁面:
$ curl -o dodo1.jpg http:www.linux.com/dodo1.JPG
#要注意-O這里后面的url要具體到某個文件,不然抓不下來
$ curl -O http://www.linux.com/dodo1.JPG
5.2、循環(huán)下載
有時候下載圖片可以能是前面的部分名稱是一樣的,就最后的尾椎名不一樣。這樣就會把dodo1,dodo2,dodo3,dodo4,dodo5全部保存下來
$ curl -O http://www.linux.com/dodo[1-5].JPG
5.3、下載重命名
在hello/dodo1.JPG的文件下載下來就會變成hello_dodo1.JPG,其他文件依此類推,從而有效的避免了文件被覆蓋
$ curl -o #1_#2.JPG http://www.linux.com/{hello,bb}/dodo[1-5].JPG
由于下載的hello與bb中的文件名都是dodo1,dodo2,dodo3,dodo4,dodo5。因此第二次下載的會把第一次下載的覆蓋,這樣就需要對文件進行重命名。
$ curl -O http://www.linux.com/{hello,bb}/dodo[1-5].JPG
5.4、分塊下載(option:-r)
curl -r 0-100 -o dodo1_part1.JPG http://www.linux.com/dodo1.JPG
curl -r 100-200 -o dodo1_part2.JPG http://www.linux.com/dodo1.JPG
curl -r 200- -o dodo1_part3.JPG http://www.linux.com/dodo1.JPG
cat dodo1_part* > dodo1.JPG #這樣就可以查看dodo1.JPG的內(nèi)容了
5.5、通過ftp下載文件(option:-u)
curl 可以通過ftp下載文件,curl提供兩種從ftp中下載的語法
curl 可以通過ftp下載文件,curl提供兩種從ftp中下載的語法
curl -O -u 用戶名:密碼 ftp://www.linux.com/dodo1.JPG
curl -O ftp://用戶名:密碼@www.linux.com/dodo1.JPG
5.6、下載,顯示進度條(option:-#)或不顯示進度條(option:-s)
curl -# -O http://www.linux.com/dodo1.JPG
curl -s -O http://www.linux.com/dodo1.JPG
5.7、下載,斷點續(xù)傳(-C )
斷點續(xù)轉(zhuǎn),從文件頭的指定位置開始繼續(xù)下載/上傳;offset續(xù)傳開始的位置,如果offset值為“-”,curl會自動從文件中識別起始位置開始傳輸;
curl -# -o centos6.8.iso -C - http://mirrors.aliyun.com/centos/6.8/isos/x86_64/CentOS-6.8-x86_64-minimal.iso
curl -C -O http://www.linux.com/dodo1.JPG
六、參考文檔
1、https://docs.gitlab.com/ee/api/jobs.html
2、https://www.cnblogs.com/deny/p/15778324.html
3、https://blog.csdn.net/ths512/article/details/110917388
4、http://www.ruanyifeng.com/blog/2019/09/curl-reference.html文章來源:http://www.zghlxwxcb.cn/news/detail-441281.html
5、https://www.ruanyifeng.com/blog/2011/09/curl.html文章來源地址http://www.zghlxwxcb.cn/news/detail-441281.html
到了這里,關(guān)于linux 命令 curl 詳解的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!