如何在Linux上安装curl?需要遵循哪些步骤?

命令简介

curl是一个广为使用的命令,可以帮助用户将数据传送或从一个远程服务器。 它支持许多协议包括HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT, TELNET, LDAP,以及 FILE。

curl提供了很多特性,包括文件断点续传,支持代理,FTP上传,用户认证,POST方法,带宽限制,等等。curl的底层采用libcurl 库。

在新版的Linux 中,curl已经内嵌安装,不需要额外安装。如果运行时候发现提示命令不存在,可以用系统自带的管理工具安装。
CentOS, RHEL, Fedora等系统的安装命令:

sudo dnf install curl

Ubuntu, Debian等系统的安装命令:

sudo apt install curl

实例

curl命令格式:

$ curl [option] url

1 显示url的内容

可以使用curl查看URL对应的页面源码。
示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl http://www.baidu.com
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css>...</html>

2 只接收header

通过参数-I 或者 –head,让curl只获取header信息。
示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl -I http://www.baidu.com
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 277
Content-Type: text/html
Date: Tue, 29 Nov 2022 03:21:15 GMT
Etag: "575e1f59-115"
Last-Modified: Mon, 13 Jun 2016 02:50:01 GMT
Pragma: no-cache
Server: bfe/1.0.8.18

3 下载内容并保存到文件

curl通过参数-o 或者 –output 来保存内容。用户需要指定文件名称。
命令格式:

curl -o filename URL
或者
curl --output filename URL

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl -o baidu www.baidu.com
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2381  100  2381    0     0  12801      0 --:--:-- --:--:-- --:--:-- 12732
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ ls
baidu

如上例,curl会展示下载过程中的数据,例如,传输速度,文件大小,耗时等。

4 通过curl下载文件

如果url代表的是一个文件,可以通过-o 或者 –output保存文件。

yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl -o baidu.png https://b.bdstatic.com/searchbox/icms/searchbox/img/cheng_girl.png
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  9337  100  9337    0     0  21172      0 --:--:-- --:--:-- --:--:-- 21172
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ ll
total 16
-rw-r--r-- 1 yunzhong yunzhong 9337 Nov 29 12:19 baidu.png

5 下载文件并保存为URL相同名字

上面的例子,我们用-o 或者 –output指定下载文件的名称。为了简化操作,我们可以使用参数-O 或者 –remote-name, 用URL中的文件名作为下载的文件名。

yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl -O  https://b.bdstatic.com/searchbox/icms/searchbox/img/cheng_girl.png
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  9337  100  9337    0     0  23459      0 --:--:-- --:--:-- --:--:-- 23401
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ ll
total 28
-rw-r--r-- 1 yunzhong yunzhong 9337 Nov 29 12:23 cheng_girl.png

6 同时下载多个文件

curl支持同时下载多个文件,每个文件设置参数-o 或者 -O
命令格式:

$ curl -O url1 -O url2

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl -O  https://b.bdstatic.com/searchbox/icms/searchbox/img/cheng_girl.png -O https://b.bdstatic.com/searchbox/icms/searchbox/img/young_boy.png
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  9337  100  9337    0     0  22498      0 --:--:-- --:--:-- --:--:-- 22498
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  7730  100  7730    0     0  75048      0 --:--:-- --:--:-- --:--:-- 75048
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ ll
total 36
-rw-r--r-- 1 yunzhong yunzhong 9337 Nov 29 21:51 cheng_girl.png
-rw-r--r-- 1 yunzhong yunzhong 7730 Nov 29 21:51 young_boy.png

7 恢复下载

如果一个下载终止或者被打断,可以使用-C 或者 –continue-at参数 恢复下载。当设置了参数,curl命令将会找出断点位置并重新执行。
示例:


yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl -C - -O https://releases.ubuntu.com/22.04.1/ubuntu-22.04.1-desktop-amd64.iso
** Resuming transfer from byte position 311296
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0 3649M    0  240k    0     0  54649      0 19:26:59  0:00:04 19:26:55 54637^C

8 使用curl跟随重定向

默认情况下,curl并不会跟踪重定向。如果目标页被移动到另外一个地址,我们可以通过-L 或者 –location参数来跟踪。
首先明确下,怎么知道是重定向。http标准状态码可以明确告知,下面是状态码的基本列表:

编码

子编码

意义

100+

代表请求已被接受,需要继续处理。

200+

请求已成功被服务器接收、理解、并接受。

300+

重定向

400+

请求错误

403

服务器已经理解请求,但是拒绝执行。

404

请求失败,服务器没有对应的资源。

500+

服务器内部出现错误。

因此,如果服务器返回的状态码为300+,则说明进行了重定向。

示例:
我们以必应为例,当访问www.bing.com时,会自动重定向到www.cn.bing.com。因此直接使用curl并不能获得结果,添加-L参数后才能获得页面信息。

yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl www.bing.com
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl -L www.bing.com
<!doctype html><html lang="zh" dir="ltr"><head>...

9 curl的静默模式

curl可以使用参数-s 或者 –silent 隐藏参数信息或者错误信息。

yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl -O https://b.bdstatic.com/searchbox/icms/searchbox/img/young_girl.png
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  8647  100  8647    0     0  37271      0 --:--:-- --:--:-- --:--:-- 37271
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl -O -s https://b.bdstatic.com/searchbox/icms/searchbox/img/young_girl.png
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$

10 查看请求返回码

配合grep,可以过滤出请求结果的状态码。
命令格式:

$ curl -IsL URL | grep HTTP 

如果想单纯的获得状态码,可以配合awk命令。

$ curl -IsL URL  | grep HTTP | awk '{print $2}'

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl -IsL www.baidu.com | grep HTTP
HTTP/1.1 200 OK
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl -IsL www.baidu.com | grep HTTP | awk '{print $2}'
200

11 设置最大传输速率

–limit-rate参数可以设定最大传输速率。如果未设置后缀,则参数值的单位是bytes/second 。可用的后缀为k,m,g。
示例:
Average Dload可以看出,速度限制生效。也可以看出,我的网速有多么慢。

yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl --limit-rate 2 -O https://releases.ubuntu.com/22.04.1/ubuntu-22.04.1-desktop-amd64.iso
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0 3649M    0 16384    0     0   1352      0  32d 18h  0:00:12  32d 18h     0^C
yunzhong@DESKTOP-9VB7LN7:/tmp/curltest$ curl --limit-rate 2k -O https://releases.ubuntu.com/22.04.1/ubuntu-22.04.1-desktop-amd64.iso
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0 3649M    0 49152    0     0   2890      0  15d 07h  0:00:17  15d 07h  3278^

12 从有认证的FTP服务器下载文件

curl命令可以使用-u 或者 –user 参数设置访问服务器的用户名密码。
命令格式:

$ curl -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/file.txt
或者
$ curl --user FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/file.txt

13 上传文件到ftp服务器

可以使用-T 或者 –upload-file 参数指定上传ftp服务器的文件。一般的服务器都需要认证,因此可以与-u 或者 –user参数一起使用,指定认证信息。
命令格式:

$ curl -T filename -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/file.txt
或者
$ curl --upload-file filename -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/file.txt

14 使用curl发送POST请求

使用参数-d 或者 –data,可以为POST请求设定数据。默认情况下,采用表单(form)的形式发送数据,因此数据必须是key=value形式,且多个key之间采用&分割。
示例:

$ curl -d "fname=John&lname=Doe" https://www.example.com/post
或者
$ curl --data "fname=John&lname=Doe" https://www.example.com/post

其实,很多时候,我们都采用了json数据格式,而并非是form表单形式。
可以使用如下命令:

curl -H "Content-Type: application/json" -X POST -d '{"json":"value", "jsonKey": "value2"}' https://www.example.com/post

其中,-H命令设定了请求header参数。
-X参数,设定了请求的http方法。
-d参数,设定了请求的发送数据。

和POST类似,可以使用-x和-d命令,实现http其他方法的支持,例如PUT等。

15 指定proxy

可以使用参数-x 或者 –proxy 指定代理,如果未设定端口则默认使用1080端口。
命令格式:

$ curl -x proxy_host:port url
或者
$ curl --proxy proxy_host:port url

16 为proxy指定用户名密码

如果proxy服务需要认证信息,我们可以使用参数-U 或者 –proxy-user 传递用户名密码信息。
命令格式:

$ curl -U username:password -x proxy_host:port url
或者
$ curl --proxy-user username:password -x proxy_host:port url

17 DICT协议支持

curl 支持DICT(dictionary network protocol)协议,对于发现名词定义有很好的帮助。
不需要额外参数,可以直接调用。

$ curl dict://dict.org/d:machine

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至2705686032@qq.com 举报,一经查实,本站将立刻删除。原文转载: 原文出处:

(0)
云计算的头像云计算
上一篇 2024 年 9 月 7 日
下一篇 2024 年 9 月 7 日

相关推荐

发表回复

登录后才能评论

联系我们

400-900-3935

在线咨询: QQ交谈

邮件:cong@zun.com

工作时间:365天无休服务 24小时在线

添加微信