Linux Command CURL Usage


Curl is an open source transmission tool for data transmission in command line mode, and supports multiple protocols including: FTP, HTTP, HTTPS, IMAP, POP3, TELNET, etc. It also supports HTTP POST method, PUT method, FTP upload, cookie, user name/password authentication, download file endpoint resuming, etc. The function is very powerful.

1. Try it first

Curl http://www.baidu.com , after pressing Enter, the HTML content will be displayed on the screen.

2. Download

curl –o baidu.html http://www.baidu.com , you can also see the download progress prompt after execution. After 100% completion, it will automatically exit and save the webpage to baidu.html.

It also has an uppercase O option, which is saved locally according to the file name on the server. If you execute curl -O http://www.baidu.com, an error will be reported, indicating that the file name cannot be found. If you replace it with curl –O http://baike.baidu.com/view/1326315.htm , it will automatically save the file as 1326315.html.

Tools like flashget under Windows also support chunking and resuming from breakpoints, and curl doesn't show weakness.

-C or --continue-at: Continue the upload from a breakpoint, the usage is as follows:

curl -C http://www.example.com

-r or --range: download in chunks, usage:

curl -r 0-10240 -o "example.part1" http://www.example.com/example.zip &

curl -r 10241-20480 -o "example.part2"

http://www.example.com/example.zip

Adding & means to execute in the background, you need to press CTRL+C to exit.

But after downloading, you need to merge these broken files by yourself

If you use UNIX or Apple, just use cat example.part* > example.zip

If you are using Windows, use copy /b example.part1+example.part2 example.zip to solve

3. Upload

-T/--upload-file: upload files to the server, usage:

curl -T "img[1-1000].png" ftp://example.com/upload/

or upload multiple files

curl -T "{file1,file2}" http://www.example.com

4. POST method

-d or --data parameter: post request, usage is curl -d "id=1&name=test" http://example.com/example.PHP ,

You need to separate the request parameters from the URL, and you can use curl -d "id=1" -d "name=test" http://example.com/example.php , which is equivalent to submitting two parameters. When there are special characters in the submitted parameter value, they need to be escaped first. For example, if there is a space, it needs to be escaped into %20.

--data-urlencode parameter: special characters can be automatically escaped without manual escaping in advance.

-F or --form: Upload a local file to the server, usage: curl -F "filename=@/home/test/test.pic" http://example.com/example.php . Don't miss the @ symbol.

5. Set referer

Sometimes if we directly request a URL and cannot succeed, it needs to judge whether the referer is correct, then it can be simulated by the -e or --referer parameter

curl --referer http://www.example.com http://www.example.com

6. Designated User Agent

-A/--user-agent: disguised as the specified browser Chrome access, usage:

curl -A “Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36” http://www.example.com

7. 伪造cookie

-b或--cookie: 有两种用法,一是指定参数和值:curl --cookie "name=xxx" http://www.example.com ;二是从文件读取:curl -b /cookie.txt http://www.example.com

8. 保存cookie

-c/--cookie-jar:curl命令执行后保存操作时生成的cookie到文件:

curl -c ./cookie.txt -d username=aaaa -d pwd=****** http://www.example.com

9. 定义输出显示内容

现在为止对它的崇拜已经是犹如滔滔江水连绵不绝了,慢!还有一个powerful的选项.

-w/--write-out: 可以定义输出的内容,如常用的http码,tcp连接时间,域名解析的时间,握手时间及第一时间响应时间等,非常强大。

用法如:

curl -o /dev/null -s -w %{http_code} "http://www.baidu.com" 打印出返回的http码

curl -o /dev/null -s -w “time_total: %{time_total}\n" "http://www.baidu.com" 打印响应时间

如果你对curl还有更大的兴趣,其他更多的功能请找无所不能的男人帮忙吧!


Curl是一个命令行方式下传输数据的开源传输工具,支持多种协议包括:FTP,HTTP,HTTPS,IMAP,POP3,TELNET等。同样支持HTTP POST方法,PUT方法,FTP上传,cookie,用户名/密码认证,下载文件端点续传等,功能十分强大。

1. 先来试试

curl http://www.baidu.com ,回车之后,HTML内容就显示在屏幕上了。

2. 下载

curl –o baidu.html http://www.baidu.com ,执行后还可以看到下载进度提示,完成100%后会自动退出了,把网页保存到baidu.html中了。

它还有一个大写O的选项,是按照服务器上的文件名保存到本地,如果执行curl –O http://www.baidu.com ,是会报错的,提示找不到文件名,如果换成curl –O http://baike.baidu.com/view/1326315.htm , 就自动保存文件为1326315.html。

Windows下像flashget这样的工具还支持分块以及断点续传,curl也毫不示弱。

-C或--continue-at:断点续传,用法如下:

curl -C http://www.example.com

-r或--range:分块下载,用法:

curl -r 0-10240 -o "example.part1" http://www.example.com/example.zip &

curl -r 10241-20480 -o "example.part2"

http://www.example.com/example.zip

其中加&表示在后台执行,需要按CTRL+C才能退出。

不过下载完后需要自己把这些破碎的文件合并起来

如果你用UNIX或苹果,用 cat example.part* > example.zip就可以

如果用的是Windows,用copy /b example.part1+example.part2 example.zip来解决

3. 上传

-T/--upload-file:往服务器上传文件,用法:

curl -T "img[1-1000].png" ftp://example.com/upload/

或上传多个文件

curl -T "{file1,file2}" http://www.example.com

4. POST方法

-d或--data参数:post请求,用法为curl -d “id=1&name=test” http://example.com/example.PHP

需把请求的参数和URL分开,同时可以使用curl -d “id=1” -d “name=test” http://example.com/example.php ,相当于提交了两个参数。当提交的参数值中有特殊字符就需要先转义。如空格时,就需要转义成%20。

--data-urlencode参数:就可以自动转义特殊字符,无需人工事先转义。

-F或--form:将本地文件上传到服务器,用法为:curl -F “filename=@/home/test/test.pic” http://example.com/example.php 。千万不能漏掉@符号。

5. 设置referer

有时候我们如果直接请求某个URL不能成功,它需要判断referer是否正确,那就可以通过-e或--referer参数模拟

curl --referer http://www.example.com http://www.example.com

6. 指定User Agent

-A/--user-agent:伪装成指定的浏览器Chrome访问,用法:

curl -A “Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36” http://www.example.com

7. 伪造cookie

-b或--cookie: 有两种用法,一是指定参数和值:curl --cookie "name=xxx" http://www.example.com ;二是从文件读取:curl -b /cookie.txt http://www.example.com

8. 保存cookie

-c/--cookie-jar:curl命令执行后保存操作时生成的cookie到文件:

curl -c ./cookie.txt -d username=aaaa -d pwd=****** http://www.example.com

9. 定义输出显示内容

现在为止对它的崇拜已经是犹如滔滔江水连绵不绝了,慢!还有一个powerful的选项.

-w/--write-out: 可以定义输出的内容,如常用的http码,tcp连接时间,域名解析的时间,握手时间及第一时间响应时间等,非常强大。

用法如:

curl -o /dev/null -s -w %{http_code} "http://www.baidu.com" 打印出返回的http码

curl -o /dev/null -s -w “time_total: %{time_total}\n" "http://www.baidu.com" 打印响应时间

如果你对curl还有更大的兴趣,其他更多的功能请找无所不能的男人帮忙吧!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325967715&siteId=291194637