curl是一个利用URL语法在命令行下工作的文件传输工具,作用是发出网络请求,然后获取数据;它支持文件的上传和下载;支持多种通信协议。

一、查看网页源码

直接在 curl 命令后加上网址,默认会发送 GET 请求来获取链接内容到标准输出

  1. curl www.jollypay.com

二、显示http头信息

-i 参数可以显示 http response 的头信息,连同网页源码一起。-I 参数则只显示 http response 的头信息。

  1. curl -i http://www.codebelief.com
  2.  
  3. HTTP/1.1 200 OK
  4. Server: nginx/1.10.3
  5. Date: Thu, 11 May 2017 08:25:46 GMT
  6. Content-Type: text/html; charset=utf-8
  7. Content-Length: 24206
  8. Connection: keep-alive
  9. X-Powered-By: Express
  10. Cache-Control: public, max-age=0
  11. ETag: W/"5e8e-Yw5ZdnVVly9/aEnMX7fVXQ"
  12. Vary: Accept-Encoding
  13.  
  14. <!DOCTYPE html>
  15. <html lang="en">
  16. ......
  17. </html>
  1. curl -I http://www.codebelief.com
  2.  
  3. HTTP/1.1 200 OK
  4. Server: nginx/1.10.3
  5. Date: Thu, 11 May 2017 08:24:45 GMT
  6. Content-Type: text/html; charset=utf-8
  7. Content-Length: 24206
  8. Connection: keep-alive
  9. X-Powered-By: Express
  10. Cache-Control: public, max-age=0
  11. ETag: W/"5e8e-Yw5ZdnVVly9/aEnMX7fVXQ"
  12. Vary: Accept-Encoding

三、显示通信过程

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

四、保存下载网页

1、使用linux的重定向功能保存

  1. # curl http://www.linux.com >> linux.html

2、可以使用curl的内置option:-o(小写)保存网页,文件名为自定义

  1. curl -o home.html http://www.sina.com.cn

3、用-O(大写的),后面的url要具体到某个真实存在的文件

  1. curl -O http://www.linux.com/hello.sh

4、通过ftp下载文件

  1. curl -O -u 用户名:密码 ftp://www.linux.com/dodo1.JPG

5、下载多个文件

  1. curl -O http://www.codebelief.com/page/2/ -O http://www.codebelief.com/page/3/
  1. curl -O http://www.linux.com/dodo[1-5].JPG

五、上传文件

curl不仅仅可以下载文件,还可以上传文件。通过内置option:-T来实现

  1. curl -T dodo1.JPG -u 用户名:密码 ftp://www.linux.com/img/

六、使用-L跟随链接重定向

如果直接使用 curl 打开某些被重定向后的链接,这种情况下就无法获取我们想要的网页内容。例如:

  1. curl www.jollypay.com
  2.  
  3. <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
  4. <html>
  5. <head><title>301 Moved Permanently</title></head>
  6. <body bgcolor="white">
  7. <h1>301 Moved Permanently</h1>
  8. <p>The requested resource has been assigned a new permanent URI.</p>
  9. <hr/>Powered by Tengine</body>
  10. </html>

301即为重定向,此时我们想要 curl 做的,就是像浏览器一样跟随链接的跳转,获取最终的网页内容。我们可以在命令中添加 -L 选项来跟随链接重定向:

  1. curl -L www.jollypay.com

七、使用 -A 自定义 User-Agent

我们可以使用 -A 来自定义用户代理,例如下面的命令将伪装成安卓火狐浏览器对网页进行请求:

  1. curl -A Mozilla/5.0 (Android; Mobile; rv:35.0) Gecko/35.0 Firefox/35.0 http://www.baidu.com

这个例子,我们使用 -H 可以实现同样的目的。

八、使用 -H 自定义 header

当我们需要传递特定的 header 的时候,可以仿照以下命令来写: 
curl -H “Referer: www.example.com” -H “User-Agent: Custom-User-Agent” http://www.baidu.com 
可以看到,当我们使用 -H 来自定义 User-Agent 时,需要使用 “User-Agent: xxx” 的格式。

我们能够直接在 header 中传递 Cookie,格式与上面的例子一样: 
curl -H “Cookie: JSESSIONID=D0112A5063D938586B659EF8F939BE24” http://www.example.com

九、 使用 -c 保存 Cookie

当我们使用 cURL 访问页面的时候,默认是不会保存 Cookie 的。有些情况下我们希望保存 Cookie 以便下次访问时使用。例如登陆了某个网站,我们希望再次访问该网站时保持登陆的状态,这时就可以现将登陆时的 Cookie 保存起来,下次访问时再读取。

-c 后面跟上要保存的文件名。

  1. curl -c cookie-example http://www.example.com

十、使用 -b 读取 Cookie

前面讲到了使用 -H 来发送 Cookie 的方法,这种方式是直接将 Cookie 字符串写在命令中。如果使用 -b 来自定义 Cookie,命令如下:

  1. curl -b JSESSIONID=D0112A5063D938586B659EF8F939BE24 http://www.example.com

如果要从文件中读取 Cookie,-H 就无能为力了,此时可以使用 -b 来达到这一目的:

  1. curl -b cookie-example http://www.example.com

即 -b 后面既可以是 Cookie 字符串,也可以是保存了 Cookie 的文件名。

十一、 使用 -d 发送 POST 请求

curl默认使用get请求的方式。

我们以登陆网页为例来进行说明使用 curl 发送 POST 请求的方法。假设有一个登录页面 www.example.com/login,只需要提交用户名和密码便可登录。我们可以使用 curl来完成这一 POST 请求,-d 用于指定发送的数据,-X 用于指定发送数据的方式:

  1. curl -d userName=tom&passwd=123456 -X POST http://www.example.com/login

在使用 -d 的情况下,如果省略 -X,则默认为 POST 方式: 与上面等效

  1. curl -d userName=tom&passwd=123456 http://www.example.com/login

使用-d发送数据时,如果想强制使用 GET 方式:

  1. curl -d somedata -X GET http://www.example.com/api

或者使用 -G 选项:

  1. curl -d somedata -G http://www.example.com/api

从文件中读取要发送的数据:

  1. curl -d @data.txt http://www.example.com/login

带 Cookie 登录 
当然,如果我们再次访问该网站,仍然会变成未登录的状态。我们可以用之前提到的方法保存 Cookie,在每次访问网站时都带上该 Cookie 以保持登录状态。

  1. curl -c cookie-login -d userName=tom&passwd=123456 http://www.example.com/login

再次访问该网站时,使用以下命令:

  1. curl -b cookie-login http://www.example.com/login

这样,就能保持访问的是登录后的页面了。

十二、测试网页返回值

-w,–write-out参数:用于在一次完整且成功的操作后输出指定格式的内容到标准输出。

输出格式由普通字符串和任意数量的变量组成,输出变量需要按照%{variable_name}的格式,如果需要输出%,double一下即可,即%%,同时,\n是换行,\r是回车,\t是TAB。curl会用合适的值来替代输出格式中的变量,所有可用变量如下:

  1. url_effective 最终获取的url地址,尤其是当你指定给curl的地址存在301跳转,且通过-L继续追踪的情形。
  2.  
  3. http_code http状态码,如200成功,301转向,404未找到,500服务器错误等。(The numerical response code that was found in the last retrieved HTTP(S) or FTP(s) transfer. In 7.18.2 the alias response_code was added to show the same info.)
  4.  
  5. http_connect The numerical code that was found in the last response (from a proxy) to a curl CONNECT request. (Added in 7.12.4)
  6.  
  7. time_total 总时间,按秒计。精确到小数点后三位。 The total time, in seconds, that the full operation lasted. The time will be displayed with millisecond resolution.)
  8.  
  9. time_namelookup DNS解析时间,从请求开始到DNS解析完毕所用时间。(The time, in seconds, it took from the start until the name resolving was completed.)
  10.  
  11. time_connect 连接时间,从开始到建立TCP连接完成所用时间,包括前边DNS解析时间,如果需要单纯的得到连接时间,用这个time_connect时间减去前边time_namelookup时间。以下同理,不再赘述。(The time, in seconds, it took from the start until the TCP connect to the remote host (or proxy) was completed.)
  12.  
  13. time_appconnect 连接建立完成时间,如SSL/SSH等建立连接或者完成三次握手时间。(The time, in seconds, it took from the start until the SSL/SSH/etc connect/handshake to the remote host was completed. (Added in 7.19.0))
  14.  
  15. time_pretransfer 从开始到准备传输的时间。(The time, in seconds, it took from the start until the file transfer was just about to begin. This includes all pre-transfer commands and negotiations that are specific to the particular protocol(s) involved.)
  16.  
  17. time_redirect 重定向时间,包括到最后一次传输前的几次重定向的DNS解析,连接,预传输,传输时间。(The time, in seconds, it took for all redirection steps include name lookup, connect, pretransfer and transfer before the final transaction was started. time_redirect shows the complete execution time for multiple redirections. (Added in 7.12.3))
  18.  
  19. time_starttransfer 开始传输时间。在发出请求之后,Web 服务器返回数据的第一个字节所用的时间(The time, in seconds, it took from the start until the first byte was just about to be transferred. This includes time_pretransfer and also the time the server needed to calculate the result.)
  20.  
  21. size_download 下载大小。(The total amount of bytes that were downloaded.)
  22.  
  23. size_upload 上传大小。(The total amount of bytes that were uploaded.)
  24.  
  25. size_header 下载的header的大小(The total amount of bytes of the downloaded headers.)
  26.  
  27. size_request 请求的大小。(The total amount of bytes that were sent in the HTTP request.)
  28.  
  29. speed_download 下载速度,单位-字节每秒。(The average download speed that curl measured for the complete download. Bytes per second.)
  30.  
  31. speed_upload 上传速度,单位-字节每秒。(The average upload speed that curl measured for the complete upload. Bytes per second.)
  32.  
  33. content_type 就是content-Type,不用多说了,这是一个访问我博客首页返回的结果示例(text/html; charset=UTF-8);(The Content-Type of the requested document, if there was any.)
  34.  
  35. num_connects Number of new connects made in the recent transfer. (Added in 7.12.3)
  36.  
  37. num_redirects Number of redirects that were followed in the request. (Added in 7.12.3)
  38.  
  39. redirect_url When a HTTP request was made without -L to follow redirects, this variable will show the actual URL a redirect would take you to. (Added in 7.18.2)
  40.  
  41. ftp_entry_path The initial path libcurl ended up in when logging on to the remote FTP server. (Added in 7.15.4)
  42.  
  43. ssl_verify_result ssl认证结果,返回0表示认证成功。( The result of the SSL peer certificate verification that was requested. 0 means the verification was successful. (Added in 7.19.0))

以下是诊断服务器到sts渠道的网络访问情况

  1. curl -w " time_namelookup: %{time_namelookup}s\n time_connect: %{time_connect}s\n time_appconnect: %{time_appconnect}s\n time_pretransfer: %{tme_pretransfer}s\n tme_redirect: %{time_redirect}s\n time_starttransfer: %{time_starttransfer}s\n time_total: %{time_total}s\n " -o /dev/null -s https://srtest.stspayone.com
  1. -/dev/null:把curl 返回的内容写到垃圾回收站即/dev/null
    -s 不显示统计信息(不加的话会有一个百分比的下载进度信息)
  1.  
  1. 结果如下:
  1. time_namelookup: 0.026s
  2. time_connect: 0.127s
  3. time_appconnect: 0.404s
  4. time_pretransfer: 0.404s
  5. tme_redirect: 0.000s
  6. time_starttransfer: 1.373s
  7. time_total: 1.373s

以上显示网络连接时间为0.127秒(其中DNS解析为0.026秒),总体请求1.373秒。

参考:

https://blog.csdn.net/weifangan/article/details/80741981

https://www.cnblogs.com/chenxiaomeng/p/10470481.html

linux之curl工具的更多相关文章

  1. [Linux][转载]Curl命令详解

    命令:curl 在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,是一款很强大的http命令行工具,当处在无界面的服务器上的时候,利用curl下载上传文件是较为方便的事情. 语法 ...

  2. curl工具

    在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具.它支持文件的上传和下载,是综合传输工具,但按传统,习惯称url为下载工具. 用法: cu ...

  3. Linux中curl的用法

    一.简介:在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,是一款强大的http命令行工具.支持文件的上传和下载,是综合传输工具. 二.语法:curl [option] [url ...

  4. linux多线程下载工具mwget

    linux多线程下载工具mwget 经常使用wget进行文件下载,然而wget的处理速度并不如人意.遇到一些国外的站点,经常慢得像蜗牛一般.然而为了解决这个问题,便有了mwget:m表示multi多线 ...

  5. 工作常用的linux/mysql/php/工具命令

    工作常用的linux/mysql/php/工具命令: 1. tar备份目录 tar zcvf ****.tar.gz ****/ tar 备份跳过目录 tar --exclude=test1 3. s ...

  6. 20个linux命令行工具监视性能(下)

    昨天晚上第一次翻译了<20 Command Line Tools to Monitor Linux Performance>中的前十个命令,翻译得不是很好,今天晚上继续把后面的十个也翻译给 ...

  7. linux自动交互工具expect,tcl安装和安装包,以及自动互信脚本

    linux自动交互工具expect,tcl安装,以及自动互信脚本 工作中需要对几十台服务器做自动互信,无意中发现expect命令,研究一番. 在网上找了许多资料也没有安装成功,摸索着总算成功了.现分享 ...

  8. 77个常用Linux命令和工具

    77个常用Linux命令和工具 Linux管理员不能单靠GUI图形界面吃饭.这就是我们编辑这篇最实用Linux命令手册的原因.这个指南是特别为Linux管理员和系统管理员 设计的,汇集了最有用的一些工 ...

  9. Linux ---> 监控JVM工具

    Linux ---> 监控JVM工具shkingshking 发布时间: 2013/10/10 01:27 阅读: 2642 收藏: 26 点赞: 1 评论: 0 JDK内置工具使用 jps(J ...

随机推荐

  1. Angular:组件之间的通信@Input、@Output和ViewChild

    ①父组件给子组件传值 1.父组件: ts: export class HomeComponent implements OnInit { public hxTitle = '我是首页的头部'; con ...

  2. uni-app开发中的各种问题处理

    特别注意: ※:在components下的组件,图片路径用 /static/img/back.png  这样的根路径形式,不要用../static  或者 ../../static 的形式,不然很坑, ...

  3. 这个大学在Github开源了计算机课程,看完在家上个 985

    微信搜「后端技术学堂」有干货,本文已收录于Github:https://github.com/imcoderlemon/CodeClass 内含原创干货文章,千本计算机电子书,3本LeetCode题解 ...

  4. 1.微博回调接口 和绑定user接口

    1.1 oauth/views.py 中添加试图函数 http://192.168.56.100:8888/oauth/weibo/callback/ # 通过vue前端传入的code,微博身份验证c ...

  5. DG修改SYS用户密码(ORA-16810,ORA-01017)

    修改主库PROD1密码后,查看configuration状态看到以下报错: [oracle@edgzrip1-PROD1 ~]$ dgmgrl sys/oracleDGMGRL for Linux: ...

  6. elasticsearch6.5.x-centos6

    elasticsearch6.5.x-centos6 elasticsearch 和 关系型数据库中的类比 es ====== RDBMS index ----- database type ---- ...

  7. pytest接口测试轻松入门

    通过Postman请求结果如下图: 那我们怎么用pytest进行测试呢? 在接口测试,我们要用到requests包,实现代码如下: import pytest import allure import ...

  8. numpy的好处

    python是很慢的,因为python在执行代码的时候会执行很多复杂的check功能,比如 b=1; a=b/0.5 这个运算看起来很简单,但是在计算机的内部.b要先从一个整数integer转化成一个 ...

  9. TP学习第二天—

    一.控制器和对应方法的创建 2.路由解析 传统的路由解析方法: 具体url地址模式设置(配置文件在 ThinkPHP/Conf/convertion.php) 停到了之前的 黑马传智的 TP课,换了个 ...

  10. Python之word文档模板套用 - 真正的模板格式套用

    Python之word文档模板套用: 1 ''' 2 #word模板套用2:套用模板 3 ''' 4 5 #导入所需库 6 from docx import Document 7 ''' 8 #另存w ...