首页 > 系统相关 >Linux学习笔记:curl命令

Linux学习笔记:curl命令

时间:2023-01-12 23:44:07浏览次数:54  
标签:baidu www HTTP -- 笔记 Linux curl com

一、介绍

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 命令用法详解

标签:baidu,www,HTTP,--,笔记,Linux,curl,com
From: https://www.cnblogs.com/hider/p/17048281.html

相关文章

  • C#设计模式学习笔记:设计原则
    原文网址:https://www.cnblogs.com/atomy/p/12144242.html   本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/8287784.html,记录一下学习过程以备后续查用。  ......
  • Linux学习笔记:shell sleep睡眠
    一、介绍在Linux的bash中,利用sleep和usleep命令可以控制睡眠时长,进行延时操作。sleep:默认以秒为单位usleep:默认以微秒为单位(1s=1000ms=1000000us)具体例......
  • 数论笔记
    目录数论模运算相关龟速乘快速幂矩阵快速幂整除整除的定义与性质素数素数的定义与性质素数判定试除法\(kn+i\)法预处理法Miller-Rabin素性测试素数筛法埃氏筛欧拉筛(线性筛......
  • linux 中下载 和安装curl
     001、下载curl:官网[root@PC1software]#wgethttps://curl.se/download/curl-7.61.0.tar.gz--no-check-certificate[root@PC1software]#lscurl-7.61.0.tar.gz......
  • linux 中非root用户安装R:configure: error: libcurl >= 7.28.0 library and headers a
     001、问题:configure:error:libcurl>=7.28.0libraryandheadersarerequiredwithsupportforhttps  002、解决方法:切回root,安装curl[root@PC1softw......
  • linux中普通用户安装R:configure: error: --with-readline=yes (default) and headers/
     001、问题configure:error:--with-readline=yes(default)andheaders/libsarenotavailable  002、解决方法a、切换回root用户[[email protected].......
  • 学习笔记——Mybatis中缓存机制
    2023-01-12一、Mybatis中缓存机制1、一级缓存(1)概述:一级缓存(即本地缓存或SqlSession级别缓存)(2)特点:①一级缓存默认开启②不能关闭③可以清空(3)缓存原理①当第一次获......
  • linux 安装简洁的 zsh
    为什么要安装简洁的zshzsh是shell中的佼佼者,但是网上配置zsh的方案,千篇一律的都是配置的oh-my-zsh,个人感觉非常臃肿,配置低的话,用起来还会非常卡。安装zsh本文以......
  • FHQ-treap 学习笔记
    FHQ-Treap学习这种平衡树不需要了解treap,据说treap和splay能干的事情他也能干。update:2023.1.12以前写的博客看起来太仓促了,修改了一下。前置芝士二叉搜索树的性......
  • 【英语六级笔记】翻译部分
    准备六级考试的时候收集的一些词语、搭配1、历史文化发源于/起源于originatefromsthisthebirthplaceofsthisthecradleofcardle:摇篮,发源地startin兴起于...,兴......