基本用法(配合sed/awk/grep)
$curl http: //bpsky.net

下载保存
$curl http://bpsky.net > index.html
$curl -o index.html http://bpsky.net
$curl -O http://bpsky.net/target.tar.gz

通过代理
$curl -x 123.45 . 67.89 : 1080 -o page.html http://bpsky.net

保存cookie
$curl -x 123.45 . 67.89 : 1080 -o page1.html -D cookie0001.txt http://bpsky.net

使用cookie
$curl -x 123.45 . 67.89 : 1080 -o page1.html -D cookie0002.txt -b cookie0001.txt http://bpsky.net

模仿浏览器
$curl -A “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)” -x 123.45 . 67.89 : 1080 -o page.html -D cookie0001.txt http://bpsky.net

伪造referer
$curl -A “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)” -x 123.45 . 67.89 : 1080 -e “mail.yahoo.com” -o page.html -D cookie0001.txt http://bpsky.net

循环下载
$curl -O http://cgi2.tky.3web.ne.jp/~zzh/screen[1-10].JPG

循环(匹配)下载
$curl -O http://cgi2.tky.3web.ne.jp/~{zzh,nick}/[001-201].JPG # >like zzh/001.JPG

循环(引用)下载
$curl -o #2_#1.jpg http://cgi2.tky.3web.ne.jp/~{zzh,nick}/[001-201].JPG # like >001_zzh.jpg

断点续传
$curl -c -O http://cgi2.tky.3wb.ne.jp/~zzh/screen1.JPG
$curl -r 0 - 10240 -o “zhao.part1” http://cgi2.tky.3web.ne.jp/~zzh/zhao1.mp3 &
$curl -r 10241 - 20480 -o “zhao.part1” http://cgi2.tky.3web.ne.jp/~zzh/zhao1.mp3 &
$curl -r 20481 - 40960 -o “zhao.part1” http://cgi2.tky.3web.ne.jp/~zzh/zhao1.mp3 &
$curl -r 40961 - -o “zhao.part1” http://cgi2.tky.3web.ne.jp/~zzh/zhao1.mp3

$cat zhao.part* > zhao.mp3

GET 上传
$curl http://www.yahoo.com/login.cgi?user=nickwolfe&password=12345

POST 上传
$curl -d “user=nickwolfe&password=12345” http://www.yahoo.com/login.cgi

POST 文件上传
$curl -F upload= $localfile -F $btn_name=$btn_value http://cgi2.tky.3web.ne.jp/~zzh/up_file.cgi

curl 使用(原创)

Posted by Neo in Linux, 系统管理 on 2010/05/25

1.访问http页面内容,输出到标准输出

curl http://www.neocanable.com

2.生成文件

curl -o index.html http://www.neocanable.com
以远程文件名保存
curl -O http://www.neocanable.com

参数-o为输出到某个文件,上面的命令等同于wget http://www.neocanable.com或者curl http://www.neocanable.com > index.html

3.添加proxy

curl -x xxx.xxx.xxx.xxx http://www.neocanable.com
通过代理ip访问网页

4.添加浏览器信息

通常服务器的日志会记录客户端浏览器的信息
curl -A “浏览器信息” http://www.neocanable.com

5.批量下载文件

curl http://www.xxx.com/action/[1-100].html > /dev/null
这个最适合爬自己网站的缓存了

文件下载后重新命名和类正则使用,下载后的文件是demo1-001.html
curl -o #1_#2 http://www.xxx.com/~{demo1,demo2}/[1-100].html
创建需要的目录
curl -o –create-dirs http://www.xxx.com/~{demo1,demo2}/[1-100].html

6.分块下载

curl -r 0-1024 http://www.xxx.com/aa.zip
curl -r 1025- http://www.xxx.com/aa.zip
先下1M,然后再下剩下的

7.curl ftp

访问ftp地址
curl -u username:password ftp://www.xxx.com
curl -u ftp://www.xxx.com
添加端口
curl -u username:password -P8899 ftp://www.xxx.com

上传文件到ftp
curl -T /home/neo/demo.jpg -u username:password ftp://www.xxx.com

8.测试参数

测试站点相应时间
curl -o /dev/null -s -w %{time_connect}:%{time_starttransfer}:%{time_total} www.google.com
查看http_code
curl -o /dev/null -s -w %{http_code} http://www.neocanable.com
网页或文件大小
curl -o /dev/null -s -w %{size_header} http://www.neocanable.com

http_code:http返回类似404,200,500等
time_total:总相应时间
time_namelookup:域名解析时间
time_connect:连接到目标地址耗费的时间
time_pretransfer:从执行到开始传输文件的时间间隔
time_starttransfer:从执行到开始传输文件的时间间隔
size_download:下载网页或文件大小
size_upload:上传文件大小
size_header:响应头
size_request:发送请求参数大小
speed_download:传输速度
speed_upload:平均上传速度
content_type:下载文件类型. (Added in 7.9.5)

9.post和get请求

get请求
curl “param1=name&params2=pass” http://www.xxx.com
post请求
curl -d “param1=name&params2=pass” http://www.xxx.com

10.响应超时

curl -m 40 http://www.xxx.com
curl –timeout 40 http://www.xxx.com

11.破解网站的防盗链

curl -e “http://www.a.net” http://www.b.net/acion

12.网站头部信息

curl -I http://www.neocanable.com

13.更总url跳转

curl -L http://url.cn/2yQFfd

14.正确的给url编码

curl –data-urlencode http://www.xxx.com/action?name=张三&sex=男

15.限制url的传输速度

curl –limit-rate http://www.xxx.com/action

16.限制下载文件大小

curl –max-filesize 1024 http://www.xxx.com/action

超过1M将不执行操作,并且返回出错

5.POST带参数请求(application/x-www-form-urlencoded)

curl -d '' http://pingcai.me/api/login?n=123&p=123
或
curl -d 'n=123&p=123' http://localhost:8080/login

6.模拟表单提交(multipart/form-data)

curl -F upload=/tmp/a.txt  -F n='tom'  http://localhost:8080/upload    #上传文件

7.POST带JSON数据

curl -d '{"name":"leo","passwd":"123"}' -H 'content-type:application/json' http://localhost:8080/json
文档更新时间: 2023-09-22 02:33   作者:月影鹏鹏
IT运维支持-AIWALY-月影工作室