CURL 常用参数

在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具.

1.查看响应头信息:

-I :显示http response的头信息.

[root@localhost]# curl -I www.sina.com
HTTP/1.1 200 OK
Date: Thu, 12 Oct 2017 08:30:05 GMT
Content-Type: text/html
Content-Length: 20996
Last-Modified: Thu, 01 Jun 2017 19:46:25 GMT
Connection: keep-alive
ETag: "59306f11-5204"
Expires: Thu, 12 Oct 2017 08:35:05 GMT
Cache-Control: max-age=300
Accept-Ranges: bytes
Set-Cookie: TS016d18b4=01d72e94ac923b2a94e5e75bd7061d4b2816f58e5dc928b1acac133bd03f260317ed74cb3e; Path=/

2.指定代理(proxy)服务器以及其端口.在日常测试中可以绑定某个主机的ip和端口,来测试域名访问的正确性.

[root@localhost]# curl -I -x 101.201.70.60:80 http://www.juzico.com

HTTP/1.1 200 OK
Date: Thu, 12 Oct 2017 08:55:20 GMT
Server: Apache/2.2.15 (CentOS)
X-Powered-By: PHP/5.5.38
Set-Cookie: sye=.html; expires=Sat, 11-Nov-2017 16:55:20 GMT; Max-Age=2620800
Vary: Accept-Encoding
Connection: close
Content-Type: text/html; charset=UTF-8

3.测试网页返回值.

[root@localhost]# curl -o /dev/null -s -w %{http_code} www.sina.com
200

4.模拟浏览器登录:

[root@localhost]# curl -I -A "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.0)" http://www.sina.com
HTTP/1.1 200 OK
Date: Thu, 12 Oct 2017 09:45:00 GMT
Content-Type: text/html
Content-Length: 20996
Last-Modified: Thu, 01 Jun 2017 19:46:25 GMT
Connection: keep-alive
ETag: "59306f11-5204"
Expires: Thu, 12 Oct 2017 09:50:00 GMT
Cache-Control: max-age=300
Accept-Ranges: bytes
Set-Cookie: TS016d18b4=01d72e94ac128c5e9a13de0faeb28c4d416904f2901fb3ff899e3c7f1c562a61888839a5b3; Path=/

5.通过-o/-O选项保存下载的文件到指定的文件中.

-o:将文件保存为命令行中指定的文件名的文件中
-O:使用URL中默认的文件名保存文件到本地

#小写o参数,将图片下载并重命名为1.jpg

[root@localhost]# curl -o 1.jpg http://pic51.nipic.com/file/20141023/2531170_115622554000_2.jpg

#大写O参数,将图片保存到本地,使用自己本身的名字.

[root@localhost]#curl -O http://pic51.nipic.com/file/20141023/2531170_115622554000_2.jpg

6.循环下载

[root@localhost]# curl -O https://www.baidu.com/2531170[1-5].JPG

7.显示下载进度条

curl -# -O http://pic51.nipic.com/file/20141023/2531170_115622554000_2.jpg

8.上传文件

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

curl -T 1.jpg -u 用户名:密码 ftp://www.sina.com/img/
curl常用参数:
    : 不带任何参数时 curl 将返回指定url中的数据并打印在屏幕上
    -o:--output 将指定curl返回保存为out文件,内容从html/jpg到各种MIME类型文件.
    -O:--remote-name 按服务器上的名称保存下载的文件
    -s:去掉所有状态,静音模式。不输出任何东西
    -x/--proxy 在给定的端口上使用HTTP代理
    -I  header信息
    -L 当页面有跳转的时候,输出跳转到的页面
    -0/--http1.0  使用HTTP 1.0 (-零)
    -w:按照后面的格式写出rt(单位是 秒)
        time_namelookup:DNS 解析域名[www.taobao.com]的时间
        time_commect:client和server端建立TCP 连接的时间
        time_starttransfer:从client发出请求;到web的server 响应第一个字节的时间
        time_total:client发出请求;到web的server发送会所有的相应数据的时间
        speed_download:下周速度  单位 byte/s

 9.curl使用token从gitlab下载包:

curl -I -m 10 -o /dev/null -s -w %{http_code} --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" ${DOWNLOAD_URL}


下载包并改名:
curl --header 'PRIVATE-TOKEN: 1BzfJssZB2SMshSywygX' 'http://gitlab.xxxxx.org/xxxxx/static_etongdai_com_common/repository/archive.zip?ref=master' -o /static//common.20180118143359.zip

下载包:
curl -O --header 'PRIVATE-TOKEN: 1BzfJssZB2SMshSywygX' 'http://gitlab.xxxxx.org/niguandong/static_xxxxx_com_common/repository/archive.zip?ref=master' 

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

curl -v http://www.baidu.com

#更详细的信息,并且保存起来.
curl --trace-ascii output.txt http://www.baidu.com

##########################################################################################

wget常用方法:

# 可以在本地/dev/null的帮助下,无痕测试文件下载速度
[root@localhost ~]# wget -O /dev/null http://nginx.org/download/nginx-1.19.0.tar.gz

#下载指定文件到当前文件夹
[root@localhost ~]# wget http://nginx.org/download/nginx-1.19.0.tar.gz

# 使用 wget -O 下载并以不同的文件名保存
[root@localhost ~]# wget -O nginx-1.tar.gz http://nginx.org/download/nginx-1.19.0.tar.gz

# 限制下载速度
[root@localhost ~]# wget --limit-rate=100k http://nginx.org/download/nginx-1.19.0.tar.gz

# 断点续传
[root@localhost ~]# wget -c http://nginx.org/download/nginx-1.19.0.tar.gz

#后台下载
[root@localhost ~]# wget -b http://nginx.org/download/nginx-1.19.0.tar.gz

#使用 wget –tries 增加重试次数 
如果网络有问题或下载一个大文件也有可能失败。wget默认重试20次连接下载文件。如果需要,你可以使用–tries增加重试次数。

[root@localhost ~]# wget –tries=40  http://nginx.org/download/nginx-1.19.0.tar.gz

#使用 wget -i 下载多个文件,将所有URL写入到文件中

[root@localhost ~]# wget -i filelist.txt

# 使用 wget -o 把下载信息存入日志文件 

[root@localhost ~]# wget -o download.log  http://nginx.org/download/nginx-1.19.0.tar.gz

 

链接文档:

http://www.ruanyifeng.com/blog/2019/09/curl-reference.html

http://blog.sina.com.cn/s/blog_af6bb6bc0102vi24.html

posted @ 2017-12-12 09:52  梦徒  阅读(1460)  评论(0编辑  收藏  举报