Linux学习笔记:curl命令

一、介绍

cURL,全称 Command Line URL viewer,是一个利用 URL 规则在命令行下工作的文件传输工具。

其主要作用是通过 http、ftp 等方式下载文件,也能够上传文件,作为一个功能强大的网络工具,curl 命令同时支持 HTTPS 等众多协议,还支持 POSTcookies、认证、从指定偏移处下载部分文件、用户代理字符串、限速、文件大小、进度条等特征。

获取数据,显示在"标准输出"(stdout)上。

按传统,习惯称 cURL 为下载工具。

cURL 支持的通信协议有 FTP、FTPS、HTTP、HTTPS、TFTP、SFTP、Gopher、SCP、Telnet、DICT、FILE、LDAP、LDAPS、IMAP、POP3、SMTP和RTSP

二、查看网页源码

直接 curl + 网址 即可查看网页源码。

$ curl 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......

使用 -o 参数可以保存文件。

$ curl -o baidu2.txt www.baidu.com
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2381  100  2381    0     0  38403      0 --:--:-- --:--:-- --:--:-- 50659

三、自动跳转

有些网址是自动跳转的。

使用 -L 参数,curl 会跳转到新的网址。

$ curl -L www.baidu.com

四、显示头信息

-i 参数可以显示 http response 的头信息,与网页代码一起。

-I 则只显示 http response 的头信息。

$ curl -i www.baidu.com

$ curl -I 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: Thu, 12 Jan 2023 15:21:25 GMT
Etag: "575e1f65-115"
Last-Modified: Mon, 13 Jun 2016 02:50:13 GMT
Pragma: no-cache
Server: bfe/1.0.8.18

五、显示通信过程

-v 参数可以显示一次 http 通信的整个过程,包括端口连接和 http request 的头信息。

$ curl -v www.baidu.com
* Rebuilt URL to: www.baidu.com/
* timeout on name lookup is not supported
*   Trying 14.215.177.39...
* Connected to www.baidu.com (14.215.177.39) port 80 (#0)
> GET / HTTP/1.1
> Host: www.baidu.com
> User-Agent: curl/7.49.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Connection: keep-alive
< Content-Length: 2381
< Content-Type: text/html
< Date: Thu, 12 Jan 2023 15:24:18 GMT
< Etag: "588604dc-94d"
< Last-Modified: Mon, 23 Jan 2017 13:27:56 GMT
< Pragma: no-cache
< Server: bfe/1.0.8.18
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
<
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8>......

下面的命令可以查看更详细的过程。

# 输出 DEBUG 跟踪信息到文件
$ curl --trace output.txt www.baidu.com
<!DOCTYPE html>
<!--STATUS OK--><html> <head>......

# 或者(不输出10进制结果)
curl --trace-ascii output.txt www.baidu.com

六、HTTP动词

curl 默认的 HTTP 动词是 GET,使用 -X 参数可以支持其他动词。

$ curl -X POST www.baidu.com

$ curl -X DELETE www.baidu.com

七、其他

其他类似 User AgentCookie、头信息、HTTP认证等信息可具体查看参数说明。

$ curl --help
Usage: curl [options...] <url>
Options: (H) means HTTP/HTTPS only, (F) means FTP only
     --anyauth       Pick "any" authentication method (H)
 -a, --append        Append to target file when uploading (F/SFTP)
     --basic         Use HTTP Basic Authentication (H)
     --cacert FILE   CA certificate to verify peer against (SSL)
     --capath DIR    CA directory to verify peer against (SSL)
 -E, --cert CERT[:PASSWD]  Client certificate file and password (SSL)
 .......
 -V, --version       Show version number and quit
 -w, --write-out FORMAT  Use output FORMAT after completion
     --xattr         Store metadata in extended file attributes
 -q, --disable       Disable .curlrc (must be first parameter)

参考链接:curl 的使用

参考链接:学习笔记之 curl 命令用法详解

posted @   Hider1214  阅读(187)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2022-01-12 Python学习笔记:pip使用技巧
点击右上角即可分享
微信分享提示