linux命令之curl,wget

 

  curl因爲可自定義各類請求參數因此在模擬web請求調試網頁方面更擅長;wget因爲支持ftp和Recursive因此在下載文件方面更擅長。類比的話curl是瀏覽器,而wget是迅雷9php

  在下載上,也各有所長,wget能夠遞歸,支持斷點;而curl支持URL中加入變量,所以能夠批量下載。html

  我的用途上,我常常用wget來下載文件,加 -c選項不怕斷網;使用curl 來跟網站的API 交互,簡便清晰。linux

 

1.下載文件nginx

curl -O http://man.linuxde.net/text.iso                    #O大寫,不用O只是打印內容不會下載
wget http://www.linuxde.net/text.iso #不用參數,直接下載文件

 

2.下載文件並重命名web

curl -o rename.iso http://man.linuxde.net/text.iso         #o小寫
wget -O rename.zip http://www.linuxde.net/text.iso #O大寫

 

3.斷點續傳瀏覽器

curl -O -C -URL http://man.linuxde.net/text.iso            #C大寫
wget -c http://www.linuxde.net/text.iso #c小寫

 

4.限速下載服務器

curl --limit-rate 50k -O http://man.linuxde.net/text.iso
wget --limit-rate=50k http://www.linuxde.net/text.iso

 

5.顯示響應頭部信息cookie

curl -I http://man.linuxde.net/text.iso
wget --server-response http://www.linuxde.net/test.iso

 

6.wget利器--打包下載網站網絡

wget --mirror -p --convert-links -P /var/www/html http://man.linuxde.net/

 

curl(文件傳輸工具)

  1. 1

    經常使用參數以下:app

    -c,–cookie-jar:將cookie寫入到文件

    -b,–cookie:從文件中讀取cookie

    -C,–continue-at:斷點續傳

    -d,–data:http post方式傳送數據

    -D,–dump-header:把header信息寫入到文件

    -F,–from:模擬http表達提交數據

    -s,–slient:減小輸出信息

    -o,–output:將信息輸出到文件

    -O,–remote-name:按照服務器上的文件名,存在本地

    –l,–head:僅返回頭部信息

    -u,–user[user:pass]:設置http認證用戶和密碼

    -T,–upload-file:上傳文件

    -e,–referer:指定引用地址

    -x,–proxy:指定代理服務器地址和端口

    -w,–write-out:輸出指定格式內容

    –retry:重試次數

    –connect-timeout:指定嘗試鏈接的最大時間/s

  2. 2

    使用示例:

    例1:抓取頁面到指定文件,若是有亂碼可使用iconv轉碼 

    # curl -o baidu.html www.baidu.com

    # curl –s –o baidu.html www.baidu.com |iconv -f utf-8 #減小輸出信息

      

    例2:模擬瀏覽器頭(user-agent) 

    # curl -A 「Mozilla/4.0 (compatible;MSIE 6.0; Windows NT 5.0)」 www.baidu.com

     

    例3:處理重定向頁面 

    # curl –L http://192.168.1.100/301.php #默認curl是不處理重定向

      

    例4:模擬用戶登錄,保存cookie信息到cookies.txt文件,再使用cookie登錄 

    # curl -c ./cookies.txt -F NAME=user -F PWD=***URL 本文全部的概念均可以從《Linux就該這麼學》中找到詳細的解釋。#NAME和PWD是表單屬性不一樣,每一個網站基本都不一樣

    # curl -b ./cookies.txt –o URL

      

    例5:獲取HTTP響應頭headers 

    # curl -I http://www.baidu.com

    # curl -D ./header.txt http://www.baidu.com #將headers保存到文件中

      

    例6:訪問HTTP認證頁面 

    # curl –u user:pass URL

      

    例7:經過ftp上傳和下載文件 

    # curl -T filename ftp://user:pass@ip/docs #上傳

    # curl -O ftp://user:pass@ip/filename #下載

     

wget(文件下載工具)

  1.  經常使用參數以下:

     2.1 啓動參數

    -V,–version:顯示版本號

    -h,–help:查看幫助

    -b,–background:啓動後轉入後臺執行

    2.2 日誌記錄和輸入文件參數

    -o,–output-file=file:把記錄寫到file文件中

    -a,–append-output=file:把記錄追加到file文件中

    -i,–input-file=file:從file讀取url來下載

    2.3 下載參數

    -bind-address=address:指定本地使用地址

    -t,-tries=number:設置最大嘗試鏈接次數

    -c,-continue:接着下載沒有下載完的文件

    -O,-output-document=file:將下載內容寫入到file文件中

    -spider:不下載文件

    -T,-timeout=sec:設置響應超時時間

    -w,-wait=sec:兩次嘗試之間間隔時間

    –limit-rate=rate:限制下載速率

    -progress=type:設置進度條

    2.4 目錄參數

    -P,-directory-prefix=prefix:將文件保存到指定目錄

    2.5 HTTP參數

    -http-user=user:設置http用戶名

    -http-passwd=pass:設置http密碼

    -U,–user-agent=agent:假裝代理

    -no-http-keep-alive:關閉http活動連接,變成永久連接

    -cookies=off:不使用cookies

    -load-cookies=file:在開始會話前從file文件加載cookies

    -save-cookies=file:在會話結束將cookies保存到file文件

    2.6 FTP參數

    -passive-ftp:默認值,使用被動模式

    -active-ftp:使用主動模式

    2.7 遞歸下載排除參數

    -A,–accept=list:分號分割被下載擴展名的列表

    -R,–reject=list:分號分割不被下載擴展名的列表

    -D,–domains=list:分號分割被下載域的列表

    –exclude-domains=list:分號分割不被下載域的列表

     

     

  2.  使用示例:例1:下載單個文件到當前目錄下,也能夠-P指定下載目錄 

    # wgethttp://nginx.org/download/nginx-1.8.0.tar.gz

      

    例2:對於網絡不穩定的用戶可使用-c和–tries參數,保證下載完成

    # wget –tries=20 -c http://nginx.org/download/nginx-1.8.0.tar.gz

     

    例3:下載大的文件時,咱們能夠放到後臺去下載,這時會生成wget-log文件來保存下載進度

    # wget -b http://nginx.org/download/nginx-1.8.0.tar.gz

     

    例4:能夠利用—spider參數判斷網址是否有效 

    # wget –spider http://nginx.org/download/nginx-1.8.0.tar.gz

     

    例5:自動從多個連接下載文件

    # cat url_list.txt #先建立一個URL文件

    http://nginx.org/download/nginx-1.8.0.tar.gz

    http://nginx.org/download/nginx-1.6.3.tar.gz

    # wget -i url_list.txt

     

    例6:限制下載速度

    # wget –limit-rate=1m http://nginx.org/download/nginx-1.8.0.tar.gz

     

    例7:登錄ftp下載文件

    # wget –ftp-user=user –ftp-password=pass ftp://ip/filenam

     

    區別

    它們的區別,我看到的有以下幾點:
    1.curl是libcurl這個庫支持的,wget是一個純粹的命令行命令。
    2.curl支持更多的協議。curl supports FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, LDAP, LDAPS, FILE, POP3, IMAP, SMTP and RTSP at the time of this writing. Wget supports HTTP, HTTPS and FTP.
    3.curl 默認支持HTTP1.1(也支持1.0),而wget僅僅支持HTTP1.0規範。引用wget的man page中的一段話吧,Please be aware that Wget needs to know the size of the POST data in advance. It's not quite clear how to work around this limitation inherent in HTTP/1.0. Although HTTP/1.1 introduces chunked transfer that doesn't require knowing the request length in advance, a client can't use chunked unless it knows it's talking to an HTTP/1.1 server.  And it can't know that until it receives a response, which in turn requires the request to have been completed -- a chicken-and-egg problem.
    4.curl在指定要下載的連接時可以支持URL的序列或集合,而wget則不能這樣;

    5.wget支持遞歸下載,而curl則沒有這個功能。

     

參考博文:

https://jingyan.baidu.com/article/eb9f7b6d63f092869264e856.html

https://blog.csdn.net/xiaoliuliu2050/article/details/53410298 

相關文章
相關標籤/搜索