命令行curl上传文件

Client URL, or simple cURL is a library and command-line utility for transferring data between systems. It supports a myriad of different protocols and tends to be installed by default on many Unix-like operating systems. Because of it’s general availability, it is a great choice for when you need to quickly download a file to your local system.

客户端URL或简单的cURL是用于在系统之间传输数据的库和命令行实用程序。 它支持多种不同的协议,并且倾向于默认安装在许多类Unix操作系统上。 由于它具有一般可用性,因此当您需要快速将文件下载到本地系统时,它是一个不错的选择。

入门 (Getting started)

To follow along at home, you will need to have the curl utility installed. As mentioned, it’s pretty standard issue on Unix-like operating systems such as Linux and macOS.

要在家中继续学习,您需要安装curl工具。 如前所述,这是类似Unix的操作系统(例如Linux和macOS)上的非常标准的问题。

If you don’t have the curl command available, please consult your favorite package manager. Even if it’s not installed, your package manager more than likely has it available to install.

如果没有可用的curl命令,请咨询您喜欢的软件包管理器。 即使未安装,程序包管理器也很有可能安装它。

The commands we will be issuing will be pretty safe as they will be downloading files from the Internet and non-destructive in nature. Obviously, downloading files off of the Internet can be sketchy, so be sure you are downloading from reputable sources.

我们将发布的命令将非常安全,因为它们将从Internet下载文件,并且本质上是非破坏性的。 显然,从Internet上下载文件可能是粗略的,因此请确保从信誉良好的来源下载文件。

Also, if you plan to run any scripts you have downloaded, it’s good practice to check their contents before making them executable and running them. A quick cat and looking over the code is often sufficient depending on the size of the file and your knowledge of the code you’re reviewing.

另外,如果您打算运行已下载的任何脚本,则最好在使其可执行并运行之前检查其内容。 快速cat和看在代码根据文件的大小和你的代码,你正在评论的知识往往是足够的。

取得远端档案 (Fetching remote files)

Out of the box, without any command-line arguments, the curl command will fetch a file and display it’s contents to the standard output.

开箱即用,没有任何命令行参数, curl命令将获取文件并将其内容显示到标准输出中。

Let’s give it a try by downloading the robots.txt file from your favorite development blog:

通过从您喜欢的开发博客下载robots.txt文件来进行尝试:

$ curl https://alligator.io/robots.txt
User-agent: *
Sitemap: https://alligator.io/sitemap.xml

Not much to it! Give curl a URL and it will fetch the resource and display it’s contents.

没什么大不了的! 给curl一个URL,它将获取资源并显示其内容。

保存远程文件 (Saving remote files)

Fetching a file and display it’s contents is all well and good, but what if you want to actually save the file to your system?

提取文件并显示其内容很好,但是如果要将文件实际保存到系统中怎么办?

To save the remote file to your local system, with the same filename as the server you’re downloading from, add the --remote-name argument, or simply, -O:

要将远程文件保存到本地系统中,并使用与要下载的服务器相同的文件名,请添加--remote-name参数,或简单地使用-O

$ curl -O https://alligator.io/robots.txt% Total    % Received % Xferd  Average Speed   Time    Time     Time  CurrentDload  Upload   Total   Spent    Left  Speed
100    56  100    56    0     0    251      0 --:--:-- --:--:-- --:--:--   251

Instead of displaying the contents of the file, curl displays a nice little text-based progress meter and saves the file to the same name as the remote file’s name. You can check on things with the cat command:

curl不会显示文件的内容,而是显示一个不错的,基于文本的小进度条,并将文件保存为与远程文件名相同的名称。 您可以使用cat命令检查内容:

$ cat robots.txt
User-agent: *
Sitemap: https://alligator.io/sitemap.xml

将远程文件保存到特定文件名 (Saving remote files to a specific filename)

What if you already had a local file with the same name as the file on the remote server?

如果您已经拥有一个与远程服务器上的文件同名的本地文件,该怎么办?

Unless you are okay with overwriting your local file of the same name, you will want to add the -o or --output argument followed by the name of the local file you’d like to save the contents to:

除非可以覆盖相同名称的本地文件,否则,您将需要添加-o--output参数,后跟想要将内容保存到的本地文件的名称:

$ curl -o gator-bots.txt https://alligator.io/robots.txt% Total    % Received % Xferd  Average Speed   Time    Time     Time  CurrentDload  Upload   Total   Spent    Left  Speed
100    56  100    56    0     0    221      0 --:--:-- --:--:-- --:--:--   221

Which downloads the remote robots.txt file to the locally named gator-bots.txt file:

gator-bots.txt远程robots.txt文件下载到本地命名的gator-bots.txt文件:

$ cat gator-bots.txt
User-agent: *
Sitemap: https://alligator.io/sitemap.xml

跟随重定向 (Following redirects)

Thus far all of the examples have included fully qualified URLs that include the https:// protocol. If you happened to try to fetch the robots.txt file and only specified alligator.io, you would be presented with an error about a redirect as we redirect requests from http:// to https:// (as one should):

到目前为止,所有示例都包括包含https://协议的标准URL。 如果您碰巧尝试获取robots.txt文件,并且仅指定了alligator.io ,那么当我们将请求从http://重定向到https:// (如应有的那样),您将看到关于重定向的错误:

$ curl alligator.io/robots.txt
Redirecting to https://alligator.io/robots.txt

No big deal though. curl has a flag you can pass in. The --location or -L argument tells curl to redo the request to the new location when a 3xx response code is encountered:

不过没什么大不了的。 curl有一个可以传递的标志--location-L参数告诉curl在遇到3xx响应代码时将请求重做到新位置:

$ curl -L alligator.io/robots.txt
User-agent: *
Sitemap: https://alligator.io/sitemap.xml

Of course if so desired, you can combine the -L argument with some of the aforementioned arguments to download the file to your local system.

当然,如果需要的话,可以将-L参数与前面提到的一些参数结合使用,以将文件下载到本地系统。

结论 (Conclusion)

curl is a great utility for quickly and easily downloading files from a remote system. While it’s similar to rsync in functionality, I find it to be a bit easier to work with since there’s less to remember in terms of arguments for some of the more common / basic tasks.

curl是一个很棒的实用程序,可用于快速轻松地从远程系统下载文件。 尽管它在功能上类似于rsync ,但我发现它的使用要容易一些,因为在一些较常见/基本任务的参数方面,您无需记住。

Like most of the simple yet powerful command-line utilities we discuss, this post really only covers the tip of the iceberg. With support for many different protocols and the added upload capabilities, curl has a ton to offer.

就像我们讨论的大多数简单但功能强大的命令行实用程序一样,本文实际上仅涵盖了冰山一角。 凭借对许多不同协议的支持以及附加的上传功能, curl可以提供大量功能。

Ready to learn more? Check out the manual page for curl by running man curl.

准备了解更多? 检查出的手册页curl通过运行man curl

翻译自: https://www.digitalocean.com/community/tutorials/workflow-downloading-files-curl

命令行curl上传文件

命令行curl上传文件_命令行基础知识:使用cURL下载文件相关推荐

  1. python从服务器下载文件_如何用Python从本地服务器下载文件

    下面的示例演示如何通过tcp从服务器下载文件. 客户代码:import socket import os HOST = 'localhost' PORT = 1024 downloadDir = &q ...

  2. crt上传数据_用SecureCRT来上传和下载数据

    我使用的是SecureCRT5.5 SecureCR下的文件传输协议有ASCII.Xmodem.Zmodem 文件传输协议 文件传输是数据交换的主要形式.在进行文件传输时,为使文件能被正确识别和传送, ...

  3. curl上传文件的命令

    curl是开源的http上传和下载工具,通过命令行实现http操作,也可以使用其源码进行http编程,就不用重新实现http协议的接口了. 网上有很多curl使用的命令行示例和基于其接口开发的示例,这 ...

  4. npm、pnpm和yarn使用(官网、安装、命令行、上传自己的包、包版本号详解、更新卸载包、查看所有版本、同等依赖peer、可选依赖optional)

    目录 npm 官网 npm install packageName npm i packagename@版本号 安装git上发布的包 npm ci 注意(对比npm i) npm outdated n ...

  5. 使用curl上传带有文件的POST数据

    本文翻译自:Using curl to upload POST data with files I would like to use cURL to not only send data param ...

  6. php curl上传文件返回false,php curl上传文件$_FILES为空的问题

    PHP 5.0~5.6 各版本兼容的cURL文件上传 最近做的一个需求,使用PHP cURL上传文件.踩坑若干,整理如下. 不同版本PHP之间cURL的区别 PHP的cURL支持通过给CURL_POS ...

  7. 检查文件上传完成_“我的数据上传NCBI又报错了...” “攻略拿去!”

    在上一期的内容中,我们分享了NCBI测序数据上传的主要步骤和资料填写的注意事项.今天跟大家分享最后一步:原始测序数据的上传以及上传后项目编号的相关类型和含义. 图1 NCBI测序数据上传步骤 | 原始 ...

  8. java 文件上传漏洞_文件上传漏洞(绕过姿势)

    文件上传漏洞可以说是日常渗透测试用得最多的一个漏洞,因为用它获得服务器权限最快最直接.但是想真正把这个漏洞利用好却不那么容易,其中有很多技巧,也有很多需要掌握的知识.俗话说,知己知彼方能百战不殆,因此 ...

  9. curl跨服务器请求文件,Curl上传文件的服务器API POST调用

    我想将一系列POSTMAN调用翻译成bash以创建脚本.超级简单到现在,我要发布与形式,data.I角色的XLSX文件中使用这个脚本:Curl上传文件的服务器API POST调用 curl -i -X ...

最新文章

  1. mysql thread入门分析
  2. Python list合并(列表合并),dict合并(字典合并)
  3. KNN算法实验-采用UCI的Iris数据集和DryBean数据集
  4. 服务器如何从bios修改mac,BIOS维修网站www.biosrepair.com-用编程器修改网卡MAC地址
  5. vm服务器虚拟机如何导出报表,教程:浏览 VM 中的 Power BI 报表服务器 - Power BI | Microsoft Docs...
  6. d3.js 旋转图形_视觉效果和动态图形软件After Effects 2019 v16.1.3.5 Win/Mac 中文/英文/多语言破解版...
  7. 国信长天蓝桥杯嵌入式类——stm32——使用keil4建立工程文件过程
  8. 如何判断一个多边形的环是逆时针还是顺时针
  9. mysql内嵌插入语句_MySQL中添加或插入语句(Insert)的几种使用方式
  10. Java版1-50内素数(质数)和
  11. Oracle 10g的闪回机制
  12. Greenplum 实时数据仓库实践(6)——实时数据装载
  13. 开源verilog仿真工具iverilog的安装与使用
  14. CTYZ的树论赛(P5557 旅行/P5558 心上秋/P5559 失昼城的守星使)
  15. 在xp中tc环境下实现的 销售管理系统(可切换繁体和简体,可设密码,可通过修改代码修改字体颜色)
  16. ‘XXX‘ is missing the class attribute ‘ExtensionOfNativeClass‘!
  17. 网站架构设计发展路径学习
  18. 音视频技术开发周刊 | 289
  19. 虚拟机安装mac无法在更新服务器失败,VMware虚拟机装Mac系统时出现不可恢夏错误怎么办 简单两步解决错误问题...
  20. Android实现奇怪的大冒险游戏菜单切换界面

热门文章

  1. php 怎么获取中文首字母排序,利用PHP怎么获取第一个中文首字母并进行排序
  2. excel计算日期时间差 8位数字转化成日期 excel输入天数计算日期 excel输入天数生成日期
  3. 【中国地质大学】初试复试资料分享(附考研群)
  4. ajax全套 增删改附代码
  5. 服务器升级暂时不能修改名称,原神怎么不能改名字了
  6. 高新技术企业科技人员比例要求及认定标准
  7. SPSS编程在Ridit分析中的应用
  8. App审核指南丨AppStore被拒理由大全
  9. js:写入/获取/清除缓存
  10. 上帝视角来看 2022 年前端趋势