curl 实战用法

2,139 阅读5分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第35天,点击查看活动详情

curl 是 1998 年推出的基于 URL 来传输数据的命令行工具。平常我们用的最多的还是 http 请求,但事实上 curl 支持的协议是非常多的,包括 DICT, FILE, FTP, FTPS, HTTP, HTTPS 等。

截止发稿前,目前最新的 curl 版本已经到 7.84.0,如果你的机器上目前没有,可以到这里安装

今天回顾一下 curl 最常用的几个选项。完整的使用手册可以参考 manpage

curl 常用选项

  • -a/--append 上传文件时,附加到目标文件

  • -A:随意指定自己这次访问所宣称的自己的浏览器信息

  • -b/--cookie <name=string/file> cookie字符串或文件读取位置,使用option来把上次的cookie信息追加到http request里面去。

  • -c/--cookie-jar 操作结束后把cookie写入到这个文件中

  • -C/--continue-at 断点续转

  • -d/--data HTTP POST方式传送数据

    --data-ascii 以ascii的方式post数据 --data-binary 以二进制的方式post数据 --negotiate 使用HTTP身份验证 --digest 使用数字身份验证 --disable-eprt 禁止使用EPRT或LPRT --disable-epsv 禁止使用EPSV

  • -D/--dump-header 把header信息写入到该文件中

    --egd-file 为随机数据(SSL)设置EGD socket路径

    --tcp-nodelay 使用TCP_NODELAY选项

  • -e/--referer 指定引用地址

  • -F/--form <name=content> 模拟http表单提交数据

    --form-string <name=string> 模拟http表单提交数据

  • -G/--get 以get的方式来发送数据

  • -H/--header

    指定请求头参数

    --ignore-content-length 忽略的HTTP头信息的长度

  • -i/--include 输出时包括protocol头信息

  • -I/--head 仅返回头部信息,使用HEAD请求

  • -k/--insecure 允许不使用证书到SSL站点

  • -K/--config 指定的配置文件读取

  • -l/--list-only 列出ftp目录下的文件名称

    --limit-rate 设置传输速度

    --local-port 强制使用本地端口号

  • -m/--max-time 指定处理的最大时长

    --max-redirs 设置最大读取的目录数

    --max-filesize 设置最大下载的文件总量

  • -o/--output 指定输出文件名称

  • -O/--remote-name 把输出写到该文件中,保留远程文件的文件名

  • -v/--verbose 小写的v参数,用于打印更多信息,包括发送的请求信息,这在调试脚本是特别有用。

  • -s/--slient 减少输出的信息,比如进度

  • --connect-timeout 指定尝试连接的最大时长

  • -x/--proxy <proxyhost[:port]> 指定代理服务器地址和端口,端口默认为1080

  • -u/--user <user[:password]>设置服务器的用户和密码

  • -r/--range 检索来自HTTP/1.1或FTP服务器字节范围

    --range-file 读取(SSL)的随机文件

  • -R/--remote-time 在本地生成文件时,保留远程文件时间

    --retry 指定重试次数

    --retry-delay 传输出现问题时,设置重试间隔时间

    --retry-max-time 传输出现问题时,设置最大重试时间

  • -s/--silent 静默模式。不输出任何东西

  • -S/--show-error 显示错误

    --socks4 <host[:port]> 用socks4代理给定主机和端口

    --socks5 <host[:port]> 用socks5代理给定主机和端口

    --stderr

  • -x/--proxy <host[:port]> 在给定的端口上使用HTTP代理

  • -X/--request 指定什么命令。curl默认的HTTP动词是GET,使用-X参数可以支持其他动词。

  • -T/--upload-file 指定上传文件路径

请求网页内容

curl https://www.baidu.com/

这样就能拿到百度首页的 html,响应如下:

<!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=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');
                </script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

这里我们也可以指定端口请求:

curl http://www.weirdserver.com:8000/

需要额外说明的是,有些时候我们希望能看到 response header,比如请求报了 400 怎么办呢?这时候我们可以启用 verbose 选项。

还是上面的请求,加上 -v 看看效果:

$ curl -v https://www.baidu.com/


*   Trying 110.242.68.3...
* TCP_NODELAY set
* Connected to www.baidu.com (110.242.68.3) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/cert.pem
  CApath: none
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server accepted to use http/1.1
* Server certificate:
*  subject: C=CN; ST=beijing; L=beijing; OU=service operation department; O=Beijing Baidu Netcom Science Technology Co., Ltd; CN=baidu.com
*  start date: Jul  5 05:16:02 2022 GMT
*  expire date: Aug  6 05:16:01 2023 GMT
*  subjectAltName: host "www.baidu.com" matched cert's "*.baidu.com"
*  issuer: C=BE; O=GlobalSign nv-sa; CN=GlobalSign RSA OV SSL CA 2018
*  SSL certificate verify ok.
> GET / HTTP/1.1
> Host: www.baidu.com
> User-Agent: curl/7.64.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: 2443
< Content-Type: text/html
< Date: Tue, 30 Aug 2022 14:22:43 GMT
< Etag: "588603e2-98b"
< Last-Modified: Mon, 23 Jan 2017 13:23:46 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><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cachxxxxxxxxxxxxxxxx
......

后面详细的 html 数据我们就省略了,关键是前面的部分,可以看到协议头,host,UA,Cookie 等信息。

此外,我们还可以用 -d <data> 选项来提交数据。post 数据需要经过 url 编码。

curl -d "name=Rafael%20Sagula&phone=3320780" http://www.where.com/guest.cgi

这里也可以看到,多个 form data 之间用 & 来连接:

<variable1>=<data1>&<variable2>=<data2>&...

下载文件

下载网页,自定义命名

curl -o thatpage.html http://www.example.com/

这里 thatpage.html 就是本地文件名,加上 -o 选项即可。

下载网页,保持原名

curl -O http://www.example.com/index.html

这里我们没有再指定本地文件名,直接拿到的就是远程的文件名。

如果我们需要下载多个文件,可以直接在后面追加:

curl -O www.haxx.se/index.html -O curl.se/download.html

参考资料