curl命令详解

时间:2020-04-06 23:44:44   收藏:0   阅读:99
在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具。它支持文件的上传和下载,是综合传输工具,但按传统,习惯称url为下载工具。

语法: curl [option] [url]

常见参数:

-A/--user-agent <string> 设置用户代理发送给服务器
-b/--cookie <name=string/file> cookie字符串或文件读取位置
-c/--cookie-jar <file> 操作结束后把cookie写入到这个文件中
-C/--continue-at <offset> 断点续转
-D/--dump-header <file> 把header信息写入到该文件中
-e/--referer 来源网址
-f/--fail 连接失败时不显示http错误
-o/--output 把输出写到该文件中
-O/--remote-name 把输出写到该文件中,保留远程文件的文件名
-r/--range <range> 检索来自HTTP/1.1或FTP服务器字节范围
-s/--silent 静音模式。不输出任何东西
-T/--upload-file <file> 上传文件
-u/--user <user[:password]> 设置服务器的用户和密码
-w/--write-out [format] 什么输出完成后
-x/--proxy <host[:port]> 在给定的端口上使用HTTP代理
-#/--progress-bar 进度条显示当前的传送状态
-I 只展示基础信息(响应头)

例子:
1 基础用法:

Administrator@PC-20141114NHWZ MINGW64 /e/shell/file_folder
$ curl  https://testing-studio.com/

可以看到该页面的html代码打印在屏幕上
2 可以将html代码输出到文件中

Administrator@PC-20141114NHWZ MINGW64 /e/shell/file_folder
$ curl  https://testing-studio.com/ > testing.html
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 17229    0 17229    0     0  39156      0 --:--:-- --:--:-- --:--:-- 39246

可以看到,目录下新增了文件testing.html

3 将html代码追加到某个文件中

Administrator@PC-20141114NHWZ MINGW64 /e/shell/file_folder
$ curl  https://testing-studio.com/ >> testing.html
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 17229    0 17229    0     0  15748      0 --:--:--  0:00:01 --:--:-- 15763

可以看到testing.html又追加了内容
4 过滤该页面的所有超链接

Administrator@PC-20141114NHWZ MINGW64 /e/shell/file_folder
$ curl  https://testing-studio.com/ | grep href > filtering.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 17229    0 17229    0     0  58206      0 --:--:-- --:--:-- --:--:-- 58403

可以看到把所有的href都过滤出来了技术图片
5 -I可以查看响应头信息

Administrator@PC-20141114NHWZ MINGW64 /e/shell/file_folder
$ curl -s -I https://testing-studio.com/
HTTP/2 200
server: nginx
date: Sat, 22 Feb 2020 06:58:34 GMT
content-type: text/html; charset=utf-8
vary: Accept-Encoding
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
x-download-options: noopen
x-permitted-cross-domain-policies: none
referrer-policy: strict-origin-when-cross-origin
x-discourse-route: list/latest
cache-control: no-cache, no-store
x-request-id: 43d9067e-4b9b-4e98-8b6b-966516d0452f
x-runtime: 0.059283
strict-transport-security: max-age=31536000
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!