Linux curl命令参数详解--转载

linux curl是通过url语法在命令行下上传或下载文件的工具软件,它支持http,https,ftp,ftps,telnet等多种协议,常被用来抓取网页和监控Web服务器状态。

$ curl -h
Usage: curl [options...] <url>-E, --cert <certificate[:password]> Client certificate file and password-b, --cookie <data> Send cookies from string/file
 -c, --cookie-jar <filename> Write cookies to <filename> after operation
     --create-dirs   Create necessary local directory hierarchy
     --crlf          Convert LF to CRLF in upload
     --crlfile <file> Get a CRL list in PEM format from the given file
 -d, --data <data>   HTTP POST data-D, --dump-header <filename> Write the received headers to <filename>-F, --form <name=content> Specify multipart MIME data-I, --head          Show document info only
 -H, --header <header/@file> Pass custom header(s) to server-i, --include       Include protocol response headers in the output
 -k, --insecure      Allow insecure server connections when using SSL
 -#, --progress-bar  Display transfer progress as a bar-x, --proxy [protocol://]host[:port] Use this proxy-U, --proxy-user <user:password> Proxy user and password-e, --referer <URL> Referrer URL
 -X, --request <command> Specify request command to use

一、Linux curl用法举例:

GET请求

curl http://localhost:8000/say_hello?name=Dave\&times=3
curl -G -d "name=Dave&times=3" http://localhost:8000/say_hello

POST请求 

简单方法

curl -d "name=value&name2=value2" http://www.baidu.com #post数据
curl -d a=b&c=d&txt@/tmp/txt http://www.baidu.com  #post文件

发送json格式数据

curl -H "Content-Type:application/json" -H "Data_Type:msg" -X POST --data '{"name": "Dave", "times": 3}' http://localhost:8000/say_hello

模拟表单,上传文件:

curl -F file=@/tmp/me.txt http://www.aiezu.com

 相当于设置form表单的method="POST"和enctype='multipart/form-data'两个属性。 

Cookies

接收cookies:

curl -c /tmp/cookies http://www.baidu.com #cookies保存到/tmp/cookies文件

 发送cookies:

curl -b "key1=val1;key2=val2;" http://www.baidu.com #发送cookies文本
curl -b /tmp/cookies http://www.baidu.com #从文件中读取cookies

如发现乱码,可以使用iconv转码:

curl http://iframe.ip138.com/ic.asp|iconv -fgb2312

iconv的用法请参阅:在Linux/Unix系统下用iconv命令处理文本文件中文乱码问题

设置请求头

curl -A "Mozilla/5.0 Firefox/21.0" http://www.baidu.com #设置http请求头User-Agent
curl -e "http://pachong.org/" http://www.baidu.com #设置http请求头Referer
curl -H "Connection:keep-alive \n User-Agent: Mozilla/5.0" http://www.aiezu.com

设置http响应头

curl -I http://www.aiezu.com #仅仅返回header
curl -D /tmp/header http://www.aiezu.com #将http header保存到/tmp/header文件

https认证

curl -u aiezu:password http://www.aiezu.com #用户名密码认证
curl -E mycert.pem https://www.baidu.com #采用证书认证

忽略认证

curl -k -H "Content-Type:application/json" -H "Data_Type:msg" -X POST --data '{"name":"Dave"}'  http://localhost:8000/say_hello

 

-k:忽略证书认证

代理

linux curl使用http代理抓取页面:

curl -x 111.95.243.36:80 http://iframe.ip138.com/ic.asp|iconv -fgb2312
curl -x 111.95.243.36:80 -U aiezu:password http://www.baidu.com

使用socks代理抓取页面:

curl --socks4 202.113.65.229:443 http://iframe.ip138.com/ic.asp|iconv -fgb2312
curl --socks5 202.113.65.229:443 http://iframe.ip138.com/ic.asp|iconv -fgb2312 

代理服务器地址可以从爬虫代理上获取。

其他

curl -# http://www.baidu.com #以“#”号输出进度条
curl -o /tmp/aiezu http://www.baidu.com #保存http响应到/tmp/aiezu

 原文地址:http://www.aiezu.com/system/linux/linux_curl_syntax.html

 

 

posted @ 2019-08-15 17:01  逐梦客!  阅读(245)  评论(0)    收藏  举报