zoukankan      html  css  js  c++  java
  • linux之curl工具

    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 

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

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

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

    前面讲到了使用 -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一下即可,即%%,同时, 是换行, 是回车, 是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
      time_connect:  %{time_connect}s
      time_appconnect:  %{time_appconnect}s
      time_pretransfer:  %{tme_pretransfer}s
      tme_redirect:  %{time_redirect}s
     time_starttransfer:  %{time_starttransfer}s
      time_total:  %{time_total}s
     " -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

  • 相关阅读:
    进程和线程的概述
    注意两个词汇的区别:并行和并发
    WebRTC MCU( Multipoint Conferencing Unit)服务器调研
    (译)WebRTC实战: STUN, TURN, Signaling
    关于图数据库查询语言:Cypher
    Neo4j安装后的密码修改
    XYC2016上半年工作笔记整理
    WebRTC技术调研
    在Django中使用Neo4j
    传统企业做互联网的困局
  • 原文地址:https://www.cnblogs.com/xulan0922/p/13969240.html
Copyright © 2011-2022 走看看