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

news2024/9/20 8:14:28

0 前言

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

Linux和Windows都提供了curl命令。

D:\>curl --help
Usage: curl [options...] <url>
 -d, --data <data>          HTTP POST data
 -f, --fail                 Fail fast with no output on HTTP errors
 -h, --help <category>      Get help for commands
 -i, --include              Include protocol response headers in the output
 -o, --output <file>        Write to file instead of stdout
 -O, --remote-name          Write output to a file named as the remote file
 -s, --silent               Silent mode
 -T, --upload-file <file>   Transfer local FILE to destination
 -u, --user <user:password> Server user and password
 -A, --user-agent <name>    Send User-Agent <name> to server
 -v, --verbose              Make the operation more talkative
 -V, --version              Show version number and quit

This is not the full help, this menu is stripped into categories.
Use "--help category" to get an overview of all categories.
For all options use the manual or "--help all".

D:\>curl --help category
Usage: curl [options...] <url>
 auth        Different types of authentication methods
 connection  Low level networking operations
 curl        The command line tool itself
 dns         General DNS options
 file        FILE protocol options
 ftp         FTP protocol options
 http        HTTP and HTTPS protocol options
 imap        IMAP protocol options
 misc        Options that don't fit into any other category
 output      Filesystem output
 pop3        POP3 protocol options
 post        HTTP Post specific options
 proxy       All options related to proxies
 scp         SCP protocol options
 sftp        SFTP protocol options
 smtp        SMTP protocol options
 ssh         SSH protocol options
 telnet      TELNET protocol options
 tftp        TFTP protocol options
 tls         All TLS/SSL related options
 upload      All options for uploads
 verbose     Options related to any kind of command line output of curl

D:\>

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

1 curl命令应用实例

1.1  只查看将把header(头部信息)写入指定文件:curl -I 统一资源定位符

我们可以使用命令 curl -i 网址 来显示header信息和资源内容,如果我们只想想看header信息,可以使用-I选项。

1.1.1 查看g.cn的header信息

[purpleendurer @ bash ~ ]  curl -I g.cn
HTTP/1.1 301 Moved Permanently
Location: https://google.cn/
X-Content-Type-Options: nosniff
Server: sffe
Content-Length: 215
X-XSS-Protection: 0
Date: Mon, 12 Aug 2024 14:10:09 GMT
Expires: Mon, 12 Aug 2024 14:40:09 GMT
Cache-Control: public, max-age=1800
Content-Type: text/html; charset=UTF-8
Age: 1673

[purpleendurer @ bash ~ ]  

 

1.1.2 查看网络文件 https://i-blog.csdnimg.cn/direct/f2734cc485764a9fa75c46b697f40081.png 的header信息

[purpleendurer @ bash ~ ]  curl -I  https://i-blog.csdnimg.cn/direct/f2734cc485764a9fa75c46b697f40081.png
HTTP/1.1 200 OK
Content-Type: image/png
Content-Length: 46323
Connection: keep-alive
Server: OBS
ETag: "1da8da89c801747e0d8427b9ac0461e1"
Date: Fri, 09 Aug 2024 11:23:54 GMT
Last-Modified: Thu, 01 Aug 2024 05:07:57 GMT
Expires: Sun, 08 Sep 2024 11:23:54 GMT
Accept-Ranges: bytes
x-obs-request-id: 0000019136E184B5EBE8705BEFA1430C
Content-Disposition: inline
x-obs-tagging-count: 0
x-obs-id-2: 32AAAQAAEAABAAAQAAEAABAAAQAAEAABCS3bAzJG6eJ1F/gKjkvPPj8zfb3tQv+l
x-link-via: bjct01:443;xianymp09:80;
X-Cache-Status: HIT from KS-CLOUD-XIANY-MP-09-04
X-Cache-Status: HIT from KS-CLOUD-BJ-CT-01-02
X-Cdn-Request-ID: 4edfdd9d8d4ff2cebf5e274fa0ae5a4b

1.2  将把header(头部信息)写入指定文件:curl -D 文件名 统一资源定位符

我们可以使用命令 curl -I 网址 来显示header信息,如果我们想把header信息保存到文件中,可以使用-D选项。

例如,我们要把http://g.cn的头部信息写入当前目录下的文件 h.txt中。

[purpleendurer @ bash ~ ]  curl -D h.txt http://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 ~ ]  ls
Code  h.txt
[purpleendurer @ bash ~ ]  cat h.txt
HTTP/1.1 301 Moved Permanently
Location: https://google.cn/
X-Content-Type-Options: nosniff
Server: sffe
Content-Length: 215
X-XSS-Protection: 0
Date: Mon, 12 Aug 2024 14:21:12 GMT
Expires: Mon, 12 Aug 2024 14:51:12 GMT
Cache-Control: public, max-age=1800
Content-Type: text/html; charset=UTF-8
Age: 530

[purpleendurer @ bash ~ ]  

 首先,我们使用命令 curl -D h.txt http://g.cn 来将header信息保存到当前目录下的文件h.txt中

然后,我们使用ls命令查看当前目录下的内容,可以看到在当前目录下,有一个目录code,还有一个文件h.txt。

接着,我们使用 cat命令查看文件h.txt的内容,正是g.cn的header信息。

1.3 下载文件时保存原始时间:curl -R -o 文件名 统一资源定位符

通常我们将网络中的文件下载保存到本地,那么本地的文件的时间会是实际保存时间,如果我们希望下载到本地的文件仍然保存网络中远程文件的原始时间,那么可以使用-R选项。

我们以下载1.1.2 中的网络文件 https://i-blog.csdnimg.cn/direct/f2734cc485764a9fa75c46b697f40081.png为例。

[purpleendurer @ bash ~ ]   curl -o 1.png https://i-blog.csdnimg.cn/direct/f2734cc485764a9fa75c46b697f40081.png
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 46323  100 46323    0     0   180k      0 --:--:-- --:--:-- --:--:--  180k
[purpleendurer @ bash ~ ]   ls -l --full-time 1.png
-rw-rw-r-- 1 csdn csdn 46323 2024-08-12 23:01:08.213754613 +0800 1.png
[purpleendurer @ bash ~ ]    curl -R -o 2.png https://i-blog.csdnimg.cn/direct/f2734cc485764a9fa75c46b697f40081.png
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 46323  100 46323    0     0   204k      0 --:--:-- --:--:-- --:--:--  204k
[purpleendurer @ bash ~ ]   ls -l --full-time 2.png
-rw-rw-r-- 1 csdn csdn 46323 2024-08-01 13:07:57.000000000 +0800 2.png
[purpleendurer @ bash ~ ]  

首先,我们使用命令curl -o 1.png https://i-blog.csdnimg.cn/direct/f2734cc485764a9fa75c46b697f40081.png 将 远程文件https://i-blog.csdnimg.cn/direct/f2734cc485764a9fa75c46b697f40081.png 下载为本地文件1.png

然后我们使用命令 ls -l --full-time 1.png 查看文件1.png的时间信息,即:

2024-08-12 23:01:08.213754613 +0800

在 1.1.2 中,我们可以看到网络文件 https://i-blog.csdnimg.cn/direct/f2734cc485764a9fa75c46b697f40081.png 的时间信息,即:

Thu, 01 Aug 2024 05:07:57 GMT

可以看到, 本地文件1.png的时间信息与 网络文件 https://i-blog.csdnimg.cn/direct/f2734cc485764a9fa75c46b697f40081.png是不同的。

接着,我们使用命令  curl -R -o 2.png https://i-blog.csdnimg.cn/direct/f2734cc485764a9fa75c46b697f40081.png 将 远程文件https://i-blog.csdnimg.cn/direct/f2734cc485764a9fa75c46b697f40081.png 下载为本地文件2.png

随后,我们使用命令 ls -l --full-time 2.png 查看文件2.png的时间信息,即:

2024-08-01 13:07:57.000000000 +0800

可以看到,这显然不是文件写入磁盘的时间。

这是由于我们在命令curl -R -o 2.png https://i-blog.csdnimg.cn/direct/f2734cc485764a9fa75c46b697f40081.png 中一了 -R选项,所以本地文件2.png的与 网络文件 https://i-blog.csdnimg.cn/direct/f2734cc485764a9fa75c46b697f40081.png 的时间信息都是2024-08-01 。

1.4  查看邮件:curl -u 用户名:密码 imap服务器url

 我们以imap://mail.163.com为例。

在命令行上直接输入密码可能并不安全,所以我们也可以只在命令行上输入用户名,然后再根据提示输入密码。

[purpleendurer @ bash ~ ]  curl -u purpleendurer imap://mail.163.com
Enter host password for user 'purpleendurer':
curl: (7) Failed connect to mail.163.com:143; Connection timed out
[purpleendurer @ bash ~ ]  

 

1.5 查阅使用手册:curl -M | more

curl命令的帮助信息还是比较简略的,我们还可以使用 -M 选项查阅使用手册,获取更详细的说明。

[purpleendurer @ bash ~ ]  curl -M | more
                                  _   _ ____  _
  Project                     ___| | | |  _ \| |
                             / __| | | | |_) | |
                            | (__| |_| |  _ <| |___
                             \___|\___/|_| \_\_____|

NAME
       curl - transfer a URL

SYNOPSIS
       curl [options] [URL...]

DESCRIPTION
       curl  is  a tool to transfer data from or to a server, using one of the
       supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS,  IMAP,
       IMAPS,  LDAP,  LDAPS,  POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS,
       TELNET and TFTP).  The command is designed to work without user  inter-
       action.

       curl offers a busload of useful tricks like proxy support, user authen-
       tication, FTP upload, HTTP post, SSL connections, cookies, file  trans-
       fer  resume,  Metalink,  and more. As you will see below, the number of
       features will make your head spin!

       curl is powered by  libcurl  for  all  transfer-related  features.  See
       libcurl(3) for details.

URL
       The  URL  syntax is protocol-dependent. You'll find a detailed descrip-
       tion in RFC 3986.

       You can specify multiple URLs or parts of URLs  by  writing  part  sets
       within braces as in:

        http://site.{one,two,three}.com
--More--

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2036636.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

sql实战

这里写自定义目录标题 sql实战cmseasy daiqile全局污染 RCE限制16字符传入参数限制传入字符7个限制35字符&#xff0c;并过滤所有英文数字 sql实战 cmseasy 1、/lib/admin/admin.php和/lib/admin/tool/front_class.php源代码中发现&#xff0c;可以伪造IP并且传入ishtml1&…

Leetcode JAVA刷刷站(26)删除有序数组中的重复项

一、题目概述 二、思路方向 为了原地删除重复出现的元素&#xff0c;并保持元素的相对顺序一致&#xff0c;我们可以使用双指针的方法来解决这个问题。这种方法通常被称为“快慢指针”法。在这个问题中&#xff0c;快指针&#xff08;fast&#xff09;用于遍历数组&#xff0…

计算机的错误计算(六十一)

摘要 解释计算机的错误计算&#xff08;六十&#xff09;中的错误计算原因。 计算机的错误计算&#xff08;六十&#xff09;中的计算可以归纳为 因此&#xff0c;我们只需要分析该算式。 例1. 已知 分析如何计算 首先&#xff0c;一个数乘以一个2&#xff0c;一般不会…

[Megagon Labs] Annotating Columns with Pre-trained Language Models

Annotating Columns with Pre-trained Language Models 任务定义 输入&#xff1a;一张数据表&#xff0c;但没有表头&#xff0c;只有表中的数据。 输出&#xff1a;每一列数据的数据类型&#xff0c;以及两列数据之间的关系。 数据类型和数据关系都是由训练数据决定的固定…

docker部署Prometheus、Grafana

docker部署Prometheus 1、 拉取prometheus镜像 docler pull prom/prometheus 遇到问题&#xff1a;注意下科学上网。 2、将prometheus配置文件放在外面管理 prometheus.yml global:scrape_interval: 15sevaluation_interval: 15salerting:alertmanagers:- static_configs:-…

聚合平台项目之数据抓取

首先先记录一下我自己对这个数据抓取的一些心得&#xff1a; 数据抓取也就是常说的爬虫。 在我没真正去做的时候&#xff0c;我还想爬虫好高大上。 现在学完之后也就怯魅了 其实本质就是在自己的代码中模拟浏览器给后端发请求&#xff0c;后端收到响应之后&#xff0c;返回…

Redis知识进阶-私人定制组

Redis 目录 RedisRedis 简介关键特征Redis不同操作系统安装在Linux上的安装&#xff1a;在macOS上的安装&#xff1a;在Windows上的安装&#xff1a; Redis 数据结构及特点常用5种及示例&#xff1a;其他结构 主要功能总结 Redis 简介 Redis是一个开源的高性能键值对数据库&am…

酶促4+2和2+2环加成反应(有机合成与生物合成)-文献精读38

酶促42和22环加成反应&#xff1a;区域与立体选择性的理解与应用 01 有机合成 类似有机化学&#xff1a;狄尔斯–阿尔德反应 狄尔斯–阿尔德反应是[42]环加成反应中最具代表的&#xff0c;由共轭双烯与亲双烯体构建环己烯骨架的经典反应。反应有良好的立体、位置选择性。 该…

3.类和对象(中)

1. 类的默认成员函数 默认成员函数就是用户没有显式实现&#xff0c;编译器会自动生成的成员函数称为默认成员函数&#xff08;就是我们不写&#xff0c;编译器会默认生成一份&#xff09;。一个类&#xff0c;我们不写的情况下编译器会默认生成以下6个默认成员函数&#xff0…

江协科技STM32学习笔记(第09章 I2C通信)

第09章 I2C通信 9.1 I2C通信协议 9.1.1 I2C通信 串口通信没有时钟线的异步全双工的协议。 案例&#xff1a;通信协议设计&#xff1a; 某个公司开发了一款芯片&#xff0c;可以干很多事情,比如AD转换、温湿度测量、姿态测量等等。这个芯片里的众多外设也是通过读写寄存器来…

InCDE论文翻译

InCDE论文翻译 Towards Continual Knowledge Graph Embedding via Incremental Distillation 通过增量蒸馏实现持续知识图嵌入 Abstract 传统的知识图嵌入(KGE)方法通常需要在新知识出现时保留整个知识图(KG)&#xff0c;这会带来巨大的训练成本。为了解决这个问题&#xf…

掌握网络数据的钥匙:Python Requests-HTML库深度解析

文章目录 掌握网络数据的钥匙&#xff1a;Python Requests-HTML库深度解析背景&#xff1a;为何选择Requests-HTML&#xff1f;什么是Requests-HTML&#xff1f;如何安装Requests-HTML&#xff1f;5个简单库函数的使用方法3个场景下库的使用示例常见Bug及解决方案总结 掌握网络…

[C++][opencv]基于opencv实现photoshop算法可选颜色调整

【测试环境】 vs2019 opencv4.8.0 【效果演示】 【核心实现代码】 SelectiveColor.hpp #ifndef OPENCV2_PS_SELECTIVECOLOR_HPP_ #define OPENCV2_PS_SELECTIVECOLOR_HPP_#include "opencv2/core.hpp" #include "opencv2/imgproc.hpp" #include "…

笔记:在WPF中OverridesDefaultStyle属性如何使用

一、目的&#xff1a;介绍下在WPF中OverridesDefaultStyle属性如何使用 OverridesDefaultStyle 属性在 WPF 中用于控制控件是否使用默认的主题样式。将其设置为 True 时&#xff0c;控件将不会应用默认的主题样式&#xff0c;而是完全依赖于你在 Style 中定义的样式。以下是如何…

代码随想录算法训练营day39||动态规划07:多重背包+打家劫舍

多重背包理论 描述&#xff1a; 有N种物品和一个容量为V 的背包。 第i种物品最多有Mi件可用&#xff0c;每件耗费的空间是Ci &#xff0c;价值是Wi 。 求解将哪些物品装入背包可使这些物品的耗费的空间 总和不超过背包容量&#xff0c;且价值总和最大。 本质&#xff1a; …

图论------迪杰斯特拉(Dijkstra)算法求单源最短路径。

编程要求 在图的应用中&#xff0c;有一个很重要的需求&#xff1a;我们需要知道从某一个点开始&#xff0c;到其他所有点的最短路径。这其中&#xff0c;Dijkstra 算法是典型的最短路径算法。 本关的编程任务是补全右侧代码片段中 Begin 至 End 中间的代码&#xff0c;实现 …

543 二叉树的直径

解题思路&#xff1a; \qquad 如果某一个点&#xff08;非叶子节点&#xff09;在最长路径上&#xff0c;那么应该有两种情况&#xff1a; \qquad 情况一&#xff1a;该节点为非转折点&#xff0c;最长路径经过其一个子节点 父节点&#xff1b; \qquad 情况二&#xff1a;该…

Rancher 使用 Minio 备份 Longhorn 数据卷

0. 概述 Longhorn 支持备份到 NFS 或者 S3, 而 MinIO 就是符合 S3 的对象存储服务。通过 docker 部署 minio 服务&#xff0c;然后在 Longhorn UI 中配置备份服务即可。 1. MinIO 部署 1.1 创建备份目录 mkdir -p /home/longhorn-backup/minio/data mkdir -p /home/longhor…

24经济师报名照上传技巧,无需下载照片工具

24经济师报名照上传技巧&#xff0c;无需下载照片工具 #中级经济师 #经济师 #高级经济师 #经济师报名照片 #中级经济师报名照片 #经济师考试

SPI通讯协议示例

目录 0x01 SPI通讯特点0x01 硬件SPI示例0x02 软件SPI示例 0x01 SPI通讯特点 SPI在接线方面会拥有片选引脚、时钟引脚、数据引脚&#xff0c;其中数据引脚又分为 MISO和MOSI&#xff0c;分别对应的是 “Master IN Slave Out”(主机输入从机输出) 和 “Master Out Slave IN”(主…