[實用]送你一個短小精悍的curl命令

curl是linux下實用的工具,發送http請求,構造各種頭部,用好curl命令是基礎技術人員的必修課。

一、我們經常會用到(以下拿頭條域名來實驗):

僅獲取響應頭部:curl http://toutiao.com/ -I

獲取詳細的信息:curl -v http://www.baidu.com

alen@alen:~/GX6628/install$ curl -I www.baidu.com
HTTP/1.1 200 OK
Server: bfe/1.0.8.14
Date: Mon, 01 Aug 2016 06:51:21 GMT
Content-Type: text/html
Content-Length: 277
Last-Modified: Mon, 13 Jun 2016 02:50:28 GMT
Connection: Keep-Alive
ETag: "575e1f74-115"
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Pragma: no-cache
Accept-Ranges: bytes

獲取請求響應的頭部:

curl http://toutiao.com/ -vosa

dig下百度獲取到百度解析IP:60.29.248.109然後用curl命令,指定到這個IP去請求:

curl http://toutiao.com/ -x 60.29.248.109:80 -vosa

alen@alen:~/GX6628/install$ curl  www.baidu.com -vosa
* Rebuilt URL to: www.baidu.com/
* Hostname was NOT found in DNS cache
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 112.80.248.74...
* Connected to www.baidu.com (112.80.248.74) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.35.0
> Host: www.baidu.com
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Mon, 01 Aug 2016 06:44:15 GMT
< Content-Type: text/html; charset=utf-8
< Transfer-Encoding: chunked
< Connection: Keep-Alive
< Vary: Accept-Encoding
< Set-Cookie: BAIDUID=FF23E9AD0737E7C0F4AEA9353BF0412B:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
< Set-Cookie: BIDUPSID=FF23E9AD0737E7C0F4AEA9353BF0412B; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
< Set-Cookie: PSTM=1470033855; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
< Set-Cookie: BDSVRTM=0; path=/
< Set-Cookie: BD_HOME=0; path=/
< Set-Cookie: H_PS_PSSID=1464_18281_17946_19861_11621_20744_20705; path=/; domain=.baidu.com
< P3P: CP=" OTI DSP COR IVA OUR IND COM "
< Cache-Control: private
< Cxy_all: baidu+e52379bcbd8d9c6d4d9974b5fda6ebe3
< Expires: Mon, 01 Aug 2016 06:43:35 GMT
< X-Powered-By: HPHP
* Server BWS/1.1 is not blacklisted
< Server: BWS/1.1
< X-UA-Compatible: IE=Edge,chrome=1
< BDPAGETYPE: 1
< BDQID: 0x9e9b170f000054fd
< BDUSERID: 0
< 
{ [data not shown]
100 99485    0 99485    0     0   646k      0 --:--:-- --:--:-- --:--:--  647k
* Connection #0 to host www.baidu.com left intact

二、來點基礎的:

-o (小寫字母o) 默認情況下,curl會把整個內容顯示在終端(也就是屏幕)上,可以通過-o將內容保存在某個文件下,比較常見的是指定到/dev/null ,類似一個垃圾箱;
-O (大寫的O)會根據請求的文件名在本地保存;
-I 只輸出響應頭部;
-e 用來設定Referer;
-A 用來設定UA頭部;
-H 可以用來設定請求的頭部(其他模擬頭部記不住別的頭部就記憶這個,好用!)這個來個例子:

[實用]送你一個短小精悍的curl命令
-H頭部使用時候和其他不一樣的是需要添加相關的頭部名稱,比如這裏的”Referer:”,但是直接使用-e則不需要;
-r 在發送請求的時候,可以用來指定發送range請求;

三、來點不一樣的吧:

curl -s -o /dev/null -w %{time_namelookup}:%{time_connect}:%{time_starttransfer}:%{time_total}-%{http_code}-%{size_download}-%{speed_download}- http://toutiao.com/; date
對應獲取到本次curl的dns時間、建連時間、首包時間、總時間、http狀態碼、下載大小、下載速度;

[實用]送你一個短小精悍的curl命令

單次測試可能存在誤差,那我們就多測試幾次:

[實用]送你一個短小精悍的curl命令

本次講的是curl 的-w參數,好的話就轉發吧!

轉載:http://www.58maisui.com/2016/08/01/a/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章