curl命令的使用

curl是利用URL语法在命令行方式下工作的开源文件传输工具。

1.CURL + URL 获取当个网页源码,默认将输出打印到标准输出中。

curl www.baidu.com

2.CURL -o/-O选项保存下载的文件到指定的文件中。
-o: 将文件保存为命令中指定的文件名的文件。
-O: 使用URL中默认的文件名保存文件到本地。

curl -o baidu.com www.baidu.com            #将文件下载到本地并且保存为baidu.com
curl -O www.baidu.com/index.php            #使用url中默认的文件名将文件保存到本地    

3.CURL -L 选项进行重定向
默认情况下,CURL不会发送HTTP Location headers,当一个被请求页面移动到另一个站点时,会发送一个HTTP Location header作为请求,然后重定向到新的地址上,例如,访问google时,会自动地将地址重定向到google.com.hk上:

curl -L http://www.google.com

使用-L强制重定向,此时会查询google.com.hk站点。

4.CURL -x 选项可以为curl添加代理

curl -x proxyserver.test.com:3182 http://www.baidu.com

5.CURL --data 选项使用POST请求
CURL默认使用GET方式请求数据,可以通过–data/-d方式指明使用POST方式传递数据。

GET:
curl -u username https://api.github.com/user?access_token=xxxxx
POST:
curl -u username --data "param1=value1&param2=value" http://api.github.com

6.CURL 通过字典查询单词
查询单词的含义:

curl dict://dict.org/d:bash
250 ok
150 3 definitions retrieved
151 "Bash" gcide "The Collaborative International Dictionary of English v.0.48"
Bash \Bash\, v. t. [imp. & p. p. {Bashed}; p. pr. & vb. n.
   {Bashing}.] [Perh. of imitative origin; or cf. Dan. baske to
   strike, bask a blow, Sw. basa to beat, bas a beating.]
   To strike heavily; to beat; to crush. [Prov. Eng. & Scot.]
   --Hall Caine.
   [1913 Webster]

         Bash her open with a rock.               --Kipling.
   [Webster 1913 Suppl.]
.
151 "Bash" gcide "The Collaborative International Dictionary of English v.0.48"
Bash \Bash\, n.
   1. a forceful blow, especially one that does damage to its
      target.
      [PJC]

   2. a elaborate or lively social gathering or party.
      [PJC]
.
151 "Bash" gcide "The Collaborative International Dictionary of English v.0.48"
Bash \Bash\, v. t. & i. [OE. baschen, baissen. See {Abash}.]
   To abash; to disconcert or be disconcerted or put out of
   countenance. [Obs.]
   [1913 Webster]

         His countenance was bold and bashed not. --Spenser.
   [1913 Webster]
.
250 ok [d/m/c = 3/0/19; 0.000r 0.000u 0.000s]
221 bye [d/m/c = 0/0/0; 0.000r 0.000u 0.000s]

列出所有可用字典:

curl dict://dict.org/show:db
250 ok
110 72 databases present
gcide "The Collaborative International Dictionary of English v.0.48"
wn "WordNet (r) 3.0 (2006)"
moby-thesaurus "Moby Thesaurus II by Grady Ward, 1.0"
elements "The Elements (07Nov00)"
vera "V.E.R.A. -- Virtual Entity of Relevant Acronyms (September 2014)"
jargon "The Jargon File (version 4.4.7, 29 Dec 2003)"
foldoc "The Free On-line Dictionary of Computing (18 March 2015)"
easton "Easton's 1897 Bible Dictionary"
hitchcock "Hitchcock's Bible Names Dictionary (late 1800's)"
bouvier "Bouvier's Law Dictionary, Revised 6th Ed (1856)"
devil "The Devil's Dictionary (1881-1906)"
world02 "CIA World Factbook 2002"
gaz2k-counties "U.S. Gazetteer Counties (2000)"
gaz2k-places "U.S. Gazetteer Places (2000)"
gaz2k-zips "U.S. Gazetteer Zip Code Tabulation Areas (2000)"
fd-tur-eng "Turkish-English FreeDict Dictionary ver. 0.2.1"
fd-por-deu "Portuguese-German FreeDict Dictionary ver. 0.1.1"
fd-nld-eng "Dutch-English Freedict Dictionary ver. 0.1.3"
fd-eng-ara "English-Arabic FreeDict Dictionary ver. 0.6.2"
fd-spa-eng "Spanish-English FreeDict Dictionary ver. 0.1.1"
fd-eng-hun "English-Hungarian FreeDict Dictionary ver. 0.1"
fd-ita-eng "Italian-English FreeDict Dictionary ver. 0.1.1"
...

7.CURL 授权
在访问需要授权的页面时,可通过使用-u选项提供用户名和密码进行授权。

curl -u username:password URL
通常的做法是在命令行中只输入用户名,之后会提示输入密码,这样可以保证在历史记录时不会将密码泄露。
curl -u username URL
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值