Curl Basic I

在这里插入图片描述

curl is a command-line utility for transferring data from or to a server designed to work without user interaction. With curl, you can download or upload data using one of the supported protocols including HTTP, HTTPS, SCP , SFTP , and FTP . curl provides a number of options allowing you to resume transfers, limit the bandwidth, proxy support, user authentication, and much more.

curl是一个命令行实用程序,用于从服务器传输数据或将数据传输到设计为无需用户交互即可工作的服务器。使用curl,您可以使用受支持的协议之一下载或上传数据,包括HTTP,HTTPS,SCP,SFTP和FTP。curl提供了许多选项,使您可以恢复传输,限制带宽,代理支持,用户身份验证等等

In this tutorial, we will show you how to use the curl tool through practical examples and detailed explanations of the most common curl options.

Installing Curl

The curl package is pre-installed on most Linux distributions today.

To check whether the Curl package is installed on your system, open up your console, type curl, and press enter. If you have curl installed, the system will print curl: try 'curl --help' or 'curl --manual' for more information. Otherwise, you will see something like curl command not found.

要检查系统上是否安装了Curl软件包,请打开控制台,键入curl,然后按Enter。如果已安装curl,则系统将打印curl:尝试使用“ curl --help”或“ curl --manual”以获取更多信息。否则,您将看到找不到curl命令之类的东西

If curl is not installed you can easily install it using the package manager of your distribution.

Install Curl on Ubuntu and Debian

sudo apt update

sudo apt install curl -y

Install Curl on CentOS and Fedora

sudo yum install curl -y

How to Use Curl

The syntax for the curl command is as follows:

curl [options] [URL...]

In its simplest form, when invoked without any option, curl displays the specified resource to the standard output.

在没有任何选项的情况下,curl以其最简单的形式将指定的资源显示到标准输出中

For example, to retrieve the example.com homepage you would run:

例如,要检索example.com主页,请运行:

curl example.com

The command will print the source code of the example.com homepage in your terminal window.

该命令将在终端窗口中显示example.com主页的源代码

If no protocol is specified, curl tries to guess the protocol you want to use, and it will default to HTTP.

如果未指定协议,curl将尝试猜测要使用的协议,它将默认为HTTP

Save the Output to a File

To save the result of the curl command, use either the -o or -O option.

要保存curl命令的结果,请使用-o或-O选项

Lowercase -o saves the file with a predefined filename, which in the example below is vue-v2.6.10.js:

小写-o使用预定义的文件名保存文件,在下面的示例中为vue-v2.6.10.js

curl -o vue-v2.6.10.js https://cdn.jsdelivr.net/npm/vue/dist/vue.js

Uppercase -O saves the file with its original filename:

大写字母-O使用原始文件名保存文件:

curl -O https://cdn.jsdelivr.net/npm/vue/dist/vue.js

Download Multiple files

To download multiple files at once, use multiple -O options, followed by the URL to the file you want to download.

要一次下载多个文件,请使用多个-O选项,然后使用要下载文件的URL

In the following example we are downloading the Arch Linux and Debian iso files:

在以下示例中,我们正在下载Arch Linux和Debian iso文件:

$ curl -O http://mirrors.edge.kernel.org/archlinux/iso/2018.06.01/archlinux-2018.06.01-x86_64.iso \
$      -O https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-9.4.0-amd64-netinst.iso

Resume a Download

恢复文件

You can resume a download by using the -C - option. This is useful if your connection drops during the download of a large file, and instead of starting the download from scratch, you can continue the previous one.

您可以使用-C-选项恢复下载。如果在下载大文件期间断开连接,这很有用,并且您可以继续上一个,而不是从头开始下载

For example, if you are downloading the Ubuntu 18.04 iso file using the following command

例如,如果要使用以下命令下载Ubuntu 18.04 iso文件

curl -O http://releases.ubuntu.com/18.04/ubuntu-18.04-live-server-amd64.iso

and suddenly your connection drops you can resume the download with:

突然之间,您的连接断开了,您可以通过以下方式继续下载:

curl -C - -O http://releases.ubuntu.com/18.04/ubuntu-18.04-live-server-amd64.iso

Get the HTTP Headers of a URL

获取URL的HTTP头

HTTP headers are colon-separated key-value pairs containing information such as user agent, content type, and encoding. Headers are passed between the client and the server with the request or the response.

HTTP标头是用冒号分隔的键/值对,其中包含诸如用户代理,内容类型和编码之类的信息。标头通过请求或响应在客户端和服务器之间传递

Use the -I option to fetch only the HTTP headers of the specified resource:

使用-I选项仅获取指定资源的HTTP标头:

curl -I --http2 https://www.ubuntu.com/

在这里插入图片描述

Test if a Website Supports HTTP/2

测试网站是否支持HTTP/2

To check whether a particular URL supports the new HTTP/2 protocol , fetch the HTTP Headers with -I along with the --http2 option:

要检查特定的URL是否支持新的HTTP/2协议,请使用-I以及–http2选项来获取HTTP标头:

curl -I --http2 -s https://linuxize.com/ | grep HTTP

The -s option tells curl to run in a silent (quiet) and hide the progress meter and error messages.

-s选项告诉curl以静默方式(安静)运行,并隐藏进度表和错误消息

If the remote server supports HTTP/2, curl prints HTTP/2.0 200:

如果远程服务器支持HTTP/2,则curl打印HTTP/2.0 200:

Output
HTTP/2 200

Otherwise, the response is HTTP/1.1 200:

否则,响应为HTTP/1.1 200:

Output
HTTP/1.1 200 OK

If you have curl version 7.47.0 or newer, you do not need to use the --http2 option because HTTP/2 is enabled by default for all HTTPS connections.

如果您使用的是curl版本7.47.0或更高版本,则无需使用–http2选项,因为默认情况下,所有HTTPS连接都启用了HTTP/2

Follow Redirects

跟随重定向

By default, curl doesn’t follow the HTTP Location headers.

默认情况下,curl不遵循HTTP Location标头

If you try to retrieve the non-www version of google.com, you will notice that instead of getting the source of the page you’ll be redirected to the www version:

如果您尝试检索google.com的非www版本,则会注意到,除了获取页面来源之外,您还将重定向到www版本:

curl google.com

在这里插入图片描述
The -L option instructs curl to follow any redirect until it reaches the final destination:

-L选项指示curl遵循任何重定向,直到到达最终目的地:

curl -L google.com

Change the User-Agent

更改用户代理

Sometimes when downloading a file, the remote server may be set to block the Curl User-Agent or to return different contents depending on the visitor device and browser.

有时,在下载文件时,可能会根据访问者设备和浏览器将远程服务器设置为阻止Curl User-Agent或返回不同的内容

In situations like this to emulate a different browser, use the -A option.

在类似这样的情况下模拟其它浏览器时,请使用-A选项

For example to emulates Firefox 60 you would use:

例如,要模拟Firefox 60,可以使用:

curl -A "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0" https://getfedora.org/

Specify a Maximum Transfer Rate

指定最大传输速率

The --limit-rate option allows you to limit the data transfer rate. The value can be expressed in bytes, kilobytes with the k suffix, megabytes with the m suffix, and gigabytes with the g suffix.

使用–limit-rate选项可以限制数据传输速率。该值可以表示为字节,后缀为k的千字节,后缀m的兆字节,以及后缀g的千兆字节

In the following example curl will download the Go binary and limit the download speed to 1 mb:

在以下示例中,curl将下载Go二进制文件并将下载速度限制为1 mb:

curl --limit-rate 1m -O https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz

This option is useful to prevent curl consuming all the available bandwidth.

这个选项对于防止curl消耗所有可用带宽很有用

Transfer Files via FTP

通过FTP传输文件

To access a protected FTP server with curl, use the -u option and specify the username and password as shown below:

要使用curl访问受保护的FTP服务器,请使用-u选项并指定用户名和密码,如下所示:

curl -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/

Once logged in, the command lists all files and directories in the user’s home directory.

登录后,该命令将列出用户主目录中的所有文件和目录

You can download a single file from the FTP server using the following syntax:

您可以使用以下语法从FTP服务器下载单个文件:

curl -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/file.tar.gz

To upload a file to the FTP server, use the -T followed by the name of the file you want to upload:

要将文件上传到FTP服务器,请使用-T,后跟要上传的文件名:

curl -T newfile.tar.gz -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/

Send Cookies

Sometimes you may need to make an HTTP request with specific cookies to access a remote resource or to debug an issue.

有时您可能需要使用特定的Cookie发出HTTP请求,以访问远程资源或调试问题

By default, when requesting a resource with curl, no cookies are sent or stored.

默认情况下,当请求带有curl的资源时,不发送或存储任何cookie

To send cookies to the server, use the -b switch followed by a filename containing the cookies or a string.

要将cookie发送到服务器,请使用-b开关,后跟包含cookie或字符串的文件名

For example, to download the Oracle Java JDK rpm file jdk-14.0.2_linux-x64_bin.rpm you’ll need to pass a cookie named oraclelicense with value a:

例如,要下载Oracle Java JDK rpm文件jdk-14.0.2_linux-x64_bin.rpm,您需要传递一个名为oraclelicense的cookie,其值为a:

curl -L -b "oraclelicense=a" -O http://download.oracle.com/otn-pub/java/jdk/14.0.2+13/19aef61b38124481863b1413dce1855f/jdk-14.0.2_linux-x64_bin.rpm

Using Proxies

使用代理

curl supports different types of proxies, including HTTP, HTTPS and SOCKS. To transfer data through a proxy server, use the -x (--proxy) option, followed by the proxy URL.

curl支持不同类型的代理,包括HTTP,HTTPS和SOCKS。要通过代理服务器传输数据,请使用-x(–proxy)选项,后跟代理URL

The following command downloads the specified resource using a proxy on 192.168.44.1 port 8888:

以下命令使用192.168.44.1端口8888上的代理下载指定的资源:

curl -x 192.168.44.1:8888 http://linux.com/

If the proxy server requires authentication, use the -U (--proxy-user) option followed by the user name and password separated by a colon (user:password):

如果代理服务器需要身份验证,请使用-U(–proxy-user)选项,后跟用冒号(user:password)分隔的用户名和密码:

curl -U username:password -x 192.168.44.1:8888 http://linux.com/

Conclusion

curl is a command-line tool that allows you to transfer data from or to a remote host. It is useful for troubleshooting issues, downloading files, and more.

curl是一个命令行工具,可让您从远程主机或向远程主机传输数据。对于解决问题,下载文件等有用

The examples shown in this tutorial are simple, but demonstrate the most used curl options and are meant to help you understand how the curl command work.

you to transfer data from or to a remote host. It is useful for troubleshooting issues, downloading files, and more.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值