0


Linux shell编程学习笔记69: curl 命令行网络数据传输工具 选项数量雷人(中)

0 前言

curl是Linux中的一款综合性网络传输工具,既可以上传也可以下载,支持HTTP、HTTPS、FTP等30余种常见协‍议。

该命令选项超多,在学习笔记68中,我们列举了该命令的部分实例,今天继续通过实例来研究curl命令的功能和用法。

1 curl命令应用实例

1.1 跟随重定向:curl -L 统一资源定位符

在访问一个网页时,如果这个网页已经移动到另一个站点时,会发送一个HTTP Loaction header作为请求,然后将请求重定向到新的地址上。

然而curl在默认情况下不会发送HTTP Location headers(重定向),这时我们可以指定 -L 选项来跟随重定向。

例如,我们打开g.cn时会自动重定向跳转到 google.cn。

当我们使用命令 curl g.cn时,看到的是g.cn的代码。

要想看到google.cn的代码,可以在命令中指定-L选项,即 curl -L g.cn

purpleEndurer @ bash ~] curl g.cn
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://google.cn/">here</A>.
</BODY></HTML>
[purpleEndurer @ bash ~] curl -L g.cn
<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8">
    <title>Google</title>
    <style>
      html { background: #fff; margin: 0 1em; }
      body { font: .8125em/1.5 arial, sans-serif; text-align: center; }
      h1 { font-size: 1.5em; font-weight: normal; margin: 1em 0 0; }
      p#footer { color: #767676; font-size: .77em; }
      p#footer a { background: url(//www.google.cn/intl/zh-CN_cn/images/cn_icp.gif) top right no-repeat; padding: 5px 20px 5px 0; }
      ul { margin: 2em; padding: 0; }
      li { display: inline; padding: 0 2em; }
      div { -moz-border-radius: 20px; -webkit-border-radius: 20px; border: 1px solid #ccc; border-radius: 20px; margin: 2em auto 1em; max-width: 650px; min-width: 544px; }
      div:hover, div:hover * { cursor: pointer; }
      div:hover { border-color: #999; }
      div p { margin: .5em 0 1.5em; }
      img { border: 0; }
    </style>
  </head>
  <body>
    <div>
      <a href="https://www.google.com.hk/webhp?hl=zh-CN&amp;sourceid=cnhp">
        <img src="//www.google.cn/intl/zh-CN_cn/landing/cnexp/google-search.png" alt="Google" width="586" height="257">
      </a>
      <h1><a href="https://www.google.com.hk/webhp?hl=zh-CN&amp;sourceid=cnhp"><strong id="target">google.com.hk</strong></a></h1>
      <p>请收藏我们的网址
    </div>
    <p id="footer">
      <span>ICP证合字B2-20070004号</span>
    </p>
  </body>
</html>
[purpleEndurer @ bash ~] 

1.2 断点续传:curl -C - -O 统一资源定位符

在Windows中,我们可以使用迅雷等的软件进行断点续传。

在Linux中,curl可以通过-C选项同样可以达到相同的断点续传效果。

我们以下载 QQ Linux版 文件https://dldir1.qq.com/qqfile/qq/QQNT/Linux/QQ_3.2.12_240808_amd64_01.deb 为例。

[purpleEndurer @ bash ~] ls
Code
[purpleEndurer @ bash ~] curl -O https://dldir1.qq.com/qqfile/qq/QQNT/Linux/QQ_3.2.12_240808_amd64_01.deb
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
 13  145M   13 19.0M    0     0  6154k      0  0:00:24  0:00:03  0:00:21 6153k^C
[purpleEndurer @ bash ~] ls
Code  QQ_3.2.12_240808_amd64_01.deb
[purpleEndurer @ bash ~] du QQ_3.2.12_240808_amd64_01.deb
22528   QQ_3.2.12_240808_amd64_01.deb
[purpleEndurer @ bash ~] curl -C - -O https://dldir1.qq.com/qqfile/qq/QQNT/Linux/QQ_3.2.12_240808_amd64_01.deb
** Resuming transfer from byte position 23068672
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  123M  100  123M    0     0  26.0M      0  0:00:04  0:00:04 --:--:-- 28.1M
[purpleEndurer @ bash ~] ls
Code  QQ_3.2.12_240808_amd64_01.deb
[purpleEndurer @ bash ~] du QQ_3.2.12_240808_amd64_01.deb
148708  QQ_3.2.12_240808_amd64_01.deb
[purpleEndurer @ bash ~] 

说明:

1.使用ls命令查看当前目录内容,只有一个Code目录。

2.使用 curl -C https://dldir1.qq.com/qqfile/qq/QQNT/Linux/QQ_3.2.12_240808_amd64_01.deb 命令开始下载文件,文件大小是145MB。

3.在下载进度达到19%时,按Ctrl + c 终止下载

  1. 使用ls命令查看当前目录内容,除了之前已经存在的Code目录,还多了一个文件QQ_3.2.12_240808_amd64_01.deb。

5.使用du命令查看文件QQ_3.2.12_240808_amd64_01.deb的大小,只有22528KB(19M)。

6.使用命令curl -C - -O https://dldir1.qq.com/qqfile/qq/QQNT/Linux/QQ_3.2.12_240808_amd64_01.deb开始断点续传

7.使用使用ls命令查看当前目录内容,仍然只有Code目录和文件QQ_3.2.12_240808_amd64_01.deb。

8.使用du命令查看文件QQ_3.2.12_240808_amd64_01.deb的大小,有148708KB,即145MB。

1.3 使用代理:curl -x 代理服务器地址:端口 统一资源定位符

在很多时候,上网需要用到代理服务器(比如是使用代理服务器上网或者因为使用curl别人网站而被别人屏蔽IP地址的时候),幸运的是curl提供了-x选项来支持我们设置代理

[purpleendurer @ bash ~ ] curl -x 127.0.0.1:1080 http://g.cn
curl: (7) Failed connect to 127.0.0.1:1080; Connection refused
[purpleendurer @ bash ~ ] curl -x 127.0.0.1:80 http://g.cn
curl: (7) Failed connect to 127.0.0.1:80; Connection refused
[purpleendurer @ bash ~ ] curl -x 47.92.194.235:4000 http://g.cn
<html><head><title>Easy Restaurant Report System</title></head><body></body></html>
[purpleendurer @ bash ~ ] curl -x 47.92.194.235:4000 http://blog.csdn.net/purpleendurer
<html><head><title>My Foods</title></head><body></body></html>
[purpleendurer @ bash ~ ] curl -x 8.130.74.114:80 http://g.cn
<html><head><title>D-LINK</title></head><body></body></html>
[purpleendurer @ bash ~ ] curl -x 8.130.74.114:80 http://blog.csdn.net/purpleendurer
<html><head><title>PbxHosting | Servicios de Telefonia IP</title></head><body></body></html>
[purpleendurer @ bash ~ ] 

1.4 指定浏览器类型和版本:curl -A "浏览器User-Agent" 统一资源定位符

有些网站需要使用特定的浏览器去访问,有些还需要使用某些特定的版本。curl提供的-A选项可以让我们指定浏览器类型和版本去访问网站。

我们以访问csdn.net为例。

1.4.1 正常访问:curl https://www.csdn.net | more

[purpleendurer @ bash ~ ] curl https://www.csdn.net | more
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0<!doctype html><html lang="zh" data-server-rendered="true"><head><title>CSDN - 专业开发者社区</title> <meta name="keywords" content="CSDN博客,CSDN学院,CSDN论坛,CSDN直播"> <meta name="descriptio
n" content="CSDN是全球知名中文IT技术交流平台,创建于1999年,包含原创博客、精品问答、职业培训、技术论坛、资源下载等产品服务,提供原创、优质、完整内容的专业IT技术开发社区."> <meta http-equiv="conten
t-type" content="text/html;charset=utf-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui"> <meta name="referrer" content="always"> <!----> <!----
> <!----> 
        <script src="https://g.csdnimg.cn/tingyun/tingyun.js"></script>
       <!----> <!----> <!----> <link rel="shortcut icon" href="https://g.csdnimg.cn/static/logo/favicon32.ico" type="image/x-icon"> <link rel="canonical" href="https://www.csdn.net"> <!----> 
          <meta name="toolbar" content={"type":"0","fixModel":"1","model":"normal"} />
       
          <meta name="report" content={"spm":"1000.2115"} />
       <script src="https://g.csdnimg.cn/??lib/jquery/1.12.4/jquery.min.js,user-tooltip/2.7/user-tooltip.js,lib/qrcode/1.0.0/qrcode.min.js"></script> <script src='//g.csdnimg.cn/common/csdn-rep
ort/report.js' type='text/javascript'></script> 
         <script src="https://g.csdnimg.cn/user-ordercart/2.0.1/user-ordercart.js?ts=2.0.1"></script>
       <!----> 
          <script src="https://g.csdnimg.cn/common/csdn-login-box/csdn-login-box.js" defer></script>
       
         <script src="https://g.csdnimg.cn/user-ordertip/3.0.2/user-ordertip.js?t=3.0.2"></script>
       <!----> <!----> <!----> <!----> 
      <script>
        window.TINGYUN && window.TINGYUN.init && window.TINGYUN.init(function (ty_rum) {
          ty_rum.server = {   "event_timeout": 60000,   "dr_threshold": 4000,   "opt_custom_param_rule": [],   "cross_page_delay": 3000,   "router_enable": true,   "fp_threshold": 2000,   "toke
n": "568934913a6343de840a781ca5eaba4b",   "beacon": "wkbrs1.tingyun.com",   "trace_threshold": 7000,   "x_server_switch": true,   "ignore_err": false,   "id": "hWg-u0rE5b8",   "key": "Z1Tu5hoKb
Gw",   "fs_threshold": 4000 };
        });
      </script>
       <!----> 
         <script src="https://g.csdnimg.cn/common/csdn-toolbar/csdn-toolbar.js?v=1716884627340" defer></script>
       <!----><link rel="stylesheet" href="https://csdnimg.cn/release/cmsfe/public/css/common.902bcda2.css"><link rel="stylesheet" href="https://csdnimg.cn/release/cmsfe/public/css/tpl/www-inde
x-new/index.1d6c87f8.css"></head> <body><div id="toolbarBox" style="min-height: 48px;"></div> <div id="app"><div><div class="main"><div class="page-container page-component"><div><div class="ho
me_wrap"><div class="content_wrap"><div id="floor-nav_557" floor-index="0"><div comp-data="[object Object]" floor-data="[object Object]" class="blog-nav-tag" data-v-f8e9e086><div class="blog-na
v " data-v-f8e9e086><img src="https://img-home.csdnimg.cn/images/20220107105619.png" alt class="blog-nav-down " data-v-f8e9e086> <div class="blog-nav-box" data-v-f8e9e086><ul class="def" data-v
-f8e9e086><!----> <!----> <!----> <!----> <!----> <!----> <li class="navigation-right " data-v-f8e9e086><a href="https://blog.csdn.net/nav/back-end" data-report-click="{&quot;spm&quot;:&quot;10
01.2100.3001.7366&quot;,&quot;extend1&quot;:&quot;back-end&quot;}" data-report-view="{&quot;spm&quot;:&quot;1001.2100.3001.7366&quot;,&quot;extend1&quot;:&quot;back-end&quot;}" data-v-f8e9e086>
后端</a></li><li class="navigation-right " data-v-f8e9e086><a href="https://blog.csdn.net/nav/web" data-report-click="{&quot;spm&quot;:&quot;1001.2100.3001.7366&quot;,&quot;extend1&quot;:&quot;
web&quot;}" data-report-view="{&quot;spm&quot;:&quot;1001.2100.3001.7366&quot;,&quot;extend1&quot;:&quot;web&quot;}" data-v-f8e9e086>前端</a></li><li class="navigation-right " data-v-f8e9e086><
a href="https://blog.csdn.net/nav/mobile" data-report-click="{&quot;spm&quot;:&quot;1001.2100.3001.7366&quot;,&quot;extend1&quot;:&quot;mobile&quot;}" data-report-view="{&quot;spm&quot;:&quot;1
001.2100.3001.7366&quot;,&quot;extend1&quot;:&quot;mobile&quot;}" data-v-f8e9e086>移动开发</a></li><li class="navigation-right " data-v-f8e9e086><a href="https://blog.csdn.net/nav/lang" data-re
port-click="{&quot;spm&quot;:&quot;1001.2100.3001.7366&quot;,&quot;extend1&quot;:&quot;lang&quot;}" data-report-view="{&quot;spm&quot;:&quot;1001.2100.3001.7366&quot;,&quot;extend1&quot;:&quot;
lang&quot;}" data-v-f8e9e086>编程语言</a></li><li class="navigation-right " data-v-f8e9e086><a href="https://blog.csdn.net/nav/java" data-report-click="{&quot;spm&quot;:&quot;1001.2100.3001.736
--More--

从反馈的网页代码来看,网页是可以正常浏览的。

1.4.2 指定 使用微软IE 6.0 访问:curl -A "Mozilla/4.0 (Windows; MSIE 6.0; Windows NT 5.2)" https://www.csdn.net

[purpleendurer @ bash ~ ] curl -A "Mozilla/4.0 (Windows; MSIE 6.0; Windows NT 5.2)" https://www.csdn.net
<html><body><script language="javascript"> window.onload=setTimeout("cy(173)", 200); function cy(YL) {var qo, mo="", no="", oo = [0xaa,0x07,0x93,0xba,0xa8,0x89,0xb2,0x9a,0x23,0xf0,0x79,0xa8,0x64,0x0d,0xb5,0xdd,0x46,0x2e,0xf5,0xe4,0x94,0xdc,0xa2,0x70,0x8f,0xd4,0xf9,0xa4,0x0b,0xd2,0x36,0x5d,0x26,0x0e,0x75,0x7a,0x6b,0xd2,0x5b,0x82,0x6a,0x59,0x80,0xad,0x3b,0xa9,0xbe,0xac,0xeb,0x79,0xe8,0xf5,0xdd,0x6b,0x8e,0x7c,0xe3,0xcb,0xb9,0x21,0x8f,0x5d,0xe4,0x12,0xe7,0x56,0xa3,0xb8,0xe5,0x6d,0x94,0x85,0xa7,0x16,0x84,0x32,0xa7,0x98,0x32,0x08,0x0f,0x96,0x05,0xf5,0xff,0x07,0x34,0x61,0xd6,0xc7,0xdc,0x0a,0x31,0xa0,0x28,0x2f,0x22,0x77,0xc4,0xd1,0xb9,0x6f,0xa9,0x91,0x00,0xe9,0x5f,0x0d,0x8e,0x9b,0xd0,0x9e,0x6c,0x3a,0xe0,0x2e,0x1c,0x6a,0xa0,0xef,0x6a,0x52,0xa0,0xe5,0x4e,0x53,0x28,0x10,0x35,0x24,0x09,0xb1,0xc5,0x4e,0x1f,0x6e,0xdb,0xf0,0x6b,0x5c,0xc7,0xad,0x55,0x46,0x9b,0xb0,0x7e,0xcd,0x1b,0xb0,0xef,0xfd,0x0b,0xc0,0xce,0xfb,0xc1,0x18,0x67,0x0b,0xf7,0xb9,0x5c,0x8a,0xd9,0xa1,0x4a,0x33,0x15,0x7c,0xe4,0x6d,0x5c,0xec,0x73,0x9a,0xc7,0x8f,0x18,0xa8,0x79,0x02,0xe9,0xb7,0xe5,0x35,0x85,0xca,0x32,0xff,0x48,0x37,0xea,0x9b,0xc9,0x19,0x42,0x2a,0xda,0x02,0xef,0xf4,0xdc,0x8c,0x8d,0xbb,0x61,0xc9,0x52,0x1a,0x48,0xf0,0x96,0x27,0xee,0x97,0xfb,0x23,0x09,0xf1,0xd9,0x62,0x14,0xd9,0x6a,0x33,0xdb,0x03,0x53,0xba,0x88,0xf0,0x91,0x1a,0x3a,0x29,0xe0,0x89,0x9e,0x3b];qo = "qo=238; do{oo[qo]=(-oo[qo])&0xff; oo[qo]=(((oo[qo]>>3)|((oo[qo]<<5)&0xff))-194)&0xff;} while(--qo>=2);"; eval(qo);qo = 237; do { oo[qo] = (oo[qo] - oo[qo - 1]) & 0xff; } while (-- qo >= 3 );qo = 1; for (;;) { if (qo > 237) break; oo[qo] = ((((((oo[qo] + 40) & 0xff) + 248) & 0xff) << 6) & 0xff) | (((((oo[qo] + 40) & 0xff) + 248) & 0xff) >> 2); qo++;}po = ""; for (qo = 1; qo < oo.length - 1; qo++) if (qo % 6) po += String.fromCharCode(oo[qo] ^ YL);eval("qo=eval;qo(po);");} </script> </body></html>[purpleendurer @ bash ~ ] 

2001年,微软公司推出了IE浏览器最“经典”的6.0版本,并在2003年走上巅峰,IE浏览器各个版本占据全球市场95%的份额。那是IE浏览器的黄金时代。

时过境迁,微软IE 6.0 是很古老的浏览器了,csdn.net应该不再支持它了,所以返回的代码跟1.4.1不同。


本文转载自: https://blog.csdn.net/Purpleendurer/article/details/140939813
版权归原作者 紫郢剑侠 所有, 如有侵权,请联系我们删除。

“Linux shell编程学习笔记69: curl 命令行网络数据传输工具 选项数量雷人(中)”的评论:

还没有评论