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

一、查看网页源码

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

curl www.jollypay.com

二、显示http头信息

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

curl -i http://www.codebelief.com 

HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Thu, 11 May 2017 08:25:46 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 24206
Connection: keep-alive
X-Powered-By: Express
Cache-Control: public, max-age=0
ETag: W/"5e8e-Yw5ZdnVVly9/aEnMX7fVXQ"
Vary: Accept-Encoding <!DOCTYPE html>
<html lang="en">
......
</html>
curl -I http://www.codebelief.com 

HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Thu, 11 May 2017 08:24:45 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 24206
Connection: keep-alive
X-Powered-By: Express
Cache-Control: public, max-age=0
ETag: W/"5e8e-Yw5ZdnVVly9/aEnMX7fVXQ"
Vary: Accept-Encoding

三、显示通信过程

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

四、保存下载网页

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

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

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

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

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

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

4、通过ftp下载文件

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

5、下载多个文件

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

五、上传文件

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

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

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

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

curl  www.jollypay.com

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<h1>301 Moved Permanently</h1>
<p>The requested resource has been assigned a new permanent URI.</p>
<hr/>Powered by Tengine</body>
</html>

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

curl  -L www.jollypay.com

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

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

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 后面跟上要保存的文件名。

curl -c “cookie-example” http://www.example.com

十、使用 -b 读取 Cookie

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

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

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

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 用于指定发送数据的方式:

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

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

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

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

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

或者使用 -G 选项:

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

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

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

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

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

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

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

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

十二、测试网页返回值

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

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

url_effective 最终获取的url地址,尤其是当你指定给curl的地址存在301跳转,且通过-L继续追踪的情形。

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.)

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)

time_total 总时间,按秒计。精确到小数点后三位。 (The total time, in seconds, that the full operation lasted. The time will be displayed with millisecond resolution.)

time_namelookup DNS解析时间,从请求开始到DNS解析完毕所用时间。(The time, in seconds, it took from the start until the name resolving was completed.)

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.)

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))

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.)

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))

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.)

size_download 下载大小。(The total amount of bytes that were downloaded.)

size_upload 上传大小。(The total amount of bytes that were uploaded.)

size_header  下载的header的大小(The total amount of bytes of the downloaded headers.)

size_request 请求的大小。(The total amount of bytes that were sent in the HTTP request.)

speed_download 下载速度,单位-字节每秒。(The average download speed that curl measured for the complete download. Bytes per second.)

speed_upload 上传速度,单位-字节每秒。(The average upload speed that curl measured for the complete upload. Bytes per second.)

content_type 就是content-Type,不用多说了,这是一个访问我博客首页返回的结果示例(text/html; charset=UTF-8);(The Content-Type of the requested document, if there was any.)

num_connects Number of new connects made in the recent transfer. (Added in 7.12.3)

num_redirects Number of redirects that were followed in the request. (Added in 7.12.3)

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)

ftp_entry_path The initial path libcurl ended up in when logging on to the remote FTP server. (Added in 7.15.4)

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渠道的网络访问情况

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
-o /dev/null:把curl 返回的内容写到垃圾回收站即/dev/null
-s 不显示统计信息(不加的话会有一个百分比的下载进度信息)
 
结果如下:
 time_namelookup:  0.026s
time_connect: 0.127s
time_appconnect: 0.404s
time_pretransfer: 0.404s
tme_redirect: 0.000s
time_starttransfer: 1.373s
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. 06 python开发之函数

    06 python开发之函数 目录 06 python开发之函数 6 函数 6.1 基本使用 6.1.1 基本概念 6.1.2 定义函数 6.2 调用函数与函数返回值 6.2.1 调用函数三种形式 6 ...

  2. RocketMQ(六):nameserver队列存储定位解析

    在rocketmq中,nameserver充当了一个配置管理者的角色,看起来好似不太重要.然而它是一个不或缺的角色,没有了它的存在,各个broker就是一盘散沙,各自为战. 所以,实际上,在rocke ...

  3. Day2 列表list

    list 列表    list是Python内置的一种数据类型, 是一种有序的集合, 可以随时添加或删除其中的元素,可以包含不同数据类型的元素.可以作为元素被别的list包含 .        nam ...

  4. centos7下安装iostat命令

    [root@node01 yum.repos.d]# yum intall -y sysstat Loaded plugins: fastestmirror No such command: inta ...

  5. 判断一个对象是否为空?怎么得到一个对象的第几个键名(key)?

    var obj = {"微信":[],"qq":[]} console.log( Object.keys(obj) ) // ["微信",& ...

  6. HBase按照行键范围删除数据

    #!/bin/bash #TOOL_PATH=$(cd "$(dirname "$0")"; pwd) #TOOL_PATH_TMP=$(cd "$( ...

  7. 【Python】自动化测试的7个步骤

    我们对自动化测试充满了希望,然而,自动化测试却经常带给我们沮丧和失望.虽然,自动化测试可以把我们从困难的环境中解放出来,在实施自动化测试解决问题的同时,又带来同样多的问题.在开展自动化测试的工作中,关 ...

  8. 【Go语言绘图】图片添加文字(二)

    这一篇将继续介绍gg库中绘制文字相关的方法,主要包括:DrawStringAnchored().DrawStringWrapped().MeasureMultilineString().WordWra ...

  9. Websocket---认识篇

    为什么需要 WebSocket ? 了解计算机网络协议的人,应该都知道:HTTP 协议是一种无状态的.无连接的.单向的应用层协议.它采用了请求/响应模型.通信请求只能由客户端发起,服务端对请求做出应答 ...

  10. os模块和os.path模块常用方法

    今天和大家分享python内置模块中的os模块和os.path模块. 1.什么是模块呢? 在计算机开发过程中,代码越写越多,也就越来越难以维护,所以为了可维护的代码,我们会把函数进行分组,放在不同的文 ...