curl簡介
curl [options / URLs]
curl是一種利用URL語法用於服務器傳輸數據的工具,支持如下協議:
- DICT
- FILE
- FTP \ FTPS
- GOPHER
- HTTP \ HTTPS
- IMAP \ IMAPS
- LDAP \ LDAPS
- POP3 \ POP3S
- RTMP \ RTSP
- SCP
- SFTP
- SMB \ SMBS
- SMTP \ SMTPS
- TELNET
- TFTP
還支持POST、cookies、認證、從指定偏移處下載部分文件、用戶代理字符串、限速、文件大小、進度條等特徵。
1 用法1:curl查看或下載網頁源碼
#### 1 查看網頁
curl http://www.baidu.com
#### 2 下載頁面到文件
curl http://www.baidu.com -o baidu
#### 3 下載頁面到文件 --silent 不顯示進度
curl http://www.baidu.com -o baidu --silent
#### 4 下載頁面到文件 --progress 顯示進度條
curl http://www.baidu.com -o baidu --progress
2 用法2:curl設置參照頁字符串
參照頁是位於HTTP頭部中的一個字符串,用來表示用戶是從哪個頁面到達當前頁面的,如果用戶點擊網頁A中的某個連接,那麼用戶就會跳轉到B網頁,網頁B頭部的參照頁字符串就包含網頁A的URL。
使用--referer
選項指定參照頁字符串:
curl --referer http://www.google.com http://man.linuxde.net
3 用法3:curl設置cookies
--cokkie "COKKIES"
選項來指定cookie,多個cookie使用分號分隔;
curl http://man.linuxde.net --cookie "user=root;pass=123456"
curl --cookie "name=zhangsan" http://localhost:8080/login
--cookie-jar
或者-c
將cookie 另存爲一個文件
curl www.baidu.com --cookie-jar baidu_cookie
4 用法4:curl設置帶寬
--limit-rate
限制下載速度(單位 k千字節 m兆字節)
curl URL --limit-rate 50k
--max-filesize
指定可下載的最大文件大小
curl URL --max-filesize bytes
5 用法5: curl認證
使用curl選項 -u 可以完成HTTP或者FTP的認證,可以指定密碼,也可以不指定密碼在後續操作中輸入密碼:
$ curl -u name:password [URL]
curl -u user:pwd http://man.linuxde.net
curl -u user http://man.linuxde.net
6 用法6:打印頭部信息
-I
或者--head
選項只打印頭部http信息(不顯示內容)-i顯示內容
fwd@fwd:~/00-RUN/02-test$ 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: Sun, 11 Aug 2019 05:27:28 GMT
Etag: "575e1f7c-115"
Last-Modified: Mon, 13 Jun 2016 02:50:36 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
7 用法7 :curl完成http動作
-X
或者--reques
: 發送http請求,默認情況爲GET
$ curl -X POST [URL] //eg: curl -X POST blog.xiaobnema.cn
$ curl -X DELETE [URL] //eg: curl -X DELETE blog.xiaobenma.cn
8 用法8:curl發送json
curl -H 'Content-Type:application/json' -d '[JSON string]' [URL]
curl -H "Content-Type: application/json" -X POST -d '{"name": "yuzhan", "age" : 18}' http://172.24.145.95:8080
9 用法9:curl監控網頁響應時間
#### 1 監控網頁響應時間
curl -o /dev/null -s -w "time_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n" "http://www.baidu.com"
#### 2-監控站點可用性
curl -o /dev/null -s -w %{http_code} "http://www.baidu.com"
10 用法10:ftp下載、上傳
#### 1-下載
curl -u name:passwd ftp://ip:port/path/file
curl ftp://name:passwd@ip:port/path/file
#### 2-ftp上傳
curl -T localfile -u name:passwd ftp://upload_site:port/path/
#### 3-http上傳
curl -T localfile http://cgi2.tky.3web.ne.jp/~zzh/abc.cgi