Linux下curl 命令的基本使用方法

简介: Linux下curl 命令的基本使用方法

一、curl命令介绍

cURL利用URL语法在命令行下工作的文件传输工具,1997年首次发行,常用来请求 Web 服务器。它的名字就是客户端(client)的 URL 工具的意思。


它的功能非常强大,命令行参数多达几十种。它支持文件上传和下载,所以是综合传输工具,但按传统,习惯称cURL为下载工具,cURL还包含了用于程序开发的libcurl库,可以直接使用C语言调用。


cURL支持的通信协议有FTP、FTPS、HTTP、HTTPS、TFTP、SFTP、Gopher、SCP、Telnet、DICT、FILE、LDAP、LDAPS、IMAP、POP3、SMTP和RTSP。


curl还支持SSL认证、HTTP POST、HTTP PUT、FTP上传, HTTP form based upload、proxies、HTTP/2、cookies、用户名+密码认证(Basic, Plain, Digest, CRAM-MD5, NTLM, Negotiate and Kerberos)、file transfer resume、proxy tunneling。



二、curl命令常用用法介绍

2.1 使用curl命令捕获网页数据

wbyq@wbyq:~$ curl www.baidu.com

该命令的语法是直接向www.baidu.com服务器地址发出get请求,服务器返回的数据会直接显示在命令行。

如果想将返回的数据保存在文件里,可以使用重定向命令。

wbyq@wbyq:~$ curl "www.baidu.com" >test.html
或者
wbyq@wbyq:~$ curl -o test.html "www.baidu.com"

如果请求的URL里含有&转义字符,可以在URL连接上加上双引号 或者在&前加\ 就可以取消转义。

curl "https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E5%9B%BE%E7%89%87&hs=2&pn=0&spn=0&di=175890&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&ie=utf-8&oe=utf-8&cl=2&lm=-1&cs=2534506313%2C1688529724&os=1097436471%2C408122739&simid=3354786982%2C133358663&adpicid=0&lpn=0&ln=30&fr=ala&fm=&sme=&cg=&bdtype=0&oriquery=%E5%9B%BE%E7%89%87&objurl=http%3A%2F%2Fa3.att.hudong.com%2F14%2F75%2F01300000164186121366756803686.jpg&fromurl=ippr_z2C%24qAzdH3FAzdH3Fp7rtwg_z%26e3Bkwthj_z%26e3Bv54AzdH3Ftrw1AzdH3Fwn_89_0c_a8naaaaa8m98bm8d8nmm0cmbanmbm_3r2_z%26e3Bip4s&gsm=6&islist=&querylist="

2.2 使用curl命令下载文件

下面命令是下载一张腾讯官网的图片,只要给对地址直接下载即可。

curl "http://qzonestyle.gtimg.cn/qzone/qzactStatics/imgs/20190626150721_85796c.png" >123.png

image.png

如果不想指定保存的名称,可以使用使用-O参数,按照服务器上的文件名,自动存在本地。

wbyq@wbyq:~$ curl -O "http://qzonestyle.gtimg.cn/qzone/qzactStatics/imgs/20190626150721_85796c.png"

使用curl下载QQ安装包。

wbyq@wbyq:~$ curl -O "https://down.qq.com/qqweb/PCQQ/PCQQ_EXE/PCQQ2020.exe"

如果想知道请求过程,可以加上-v参数,方便调试。

wbyq@wbyq:~$ curl -v -o 123.png "http://qzonestyle.gtimg.cn/qzone/qzactStatics/imgs/20190626150721_85796c.png"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 58.49.224.43...
* TCP_NODELAY set
* Connected to qzonestyle.gtimg.cn (58.49.224.43) port 80 (#0)
> GET /qzone/qzactStatics/imgs/20190626150721_85796c.png HTTP/1.1
> Host: qzonestyle.gtimg.cn
> User-Agent: curl/7.58.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Server: X2S_Platform
< Connection: keep-alive
< Date: Fri, 09 Oct 2020 08:36:40 GMT
< Cache-Control: max-age=259200
< Expires: Mon, 12 Oct 2020 08:36:40 GMT
< Last-Modified: Wed, 26 Jun 2019 15:08:46 GMT
< Content-Type: image/png
< Content-Length: 21366
< X-NWS-LOG-UUID: 13077040326766635903 360f37d0bbde5c541b71c39a5a6c037d
< Access-Control-Expose-Headers: x-client-proto-ver
< Access-Control-Expose-Headers: X-Server-Ip
< Access-Control-Expose-Headers: X-Upstream-IP
< server_ip: 58.49.224.17
< Keep-Alive: timeout=60
< alt-svc: quic=":443"; ma=86400; v="39,38,37,36,35"
< Vary: Origin, Accept
< X-Client-Ip: 172.24.57.14
< X-Server-IP: 58.49.224.17
< X-Cache-Lookup: Hit From Disktank3
< X-DataSrc: 2
< X-ReqGue: 0
< X-Cache-Lookup: Hit From Inner Cluster 
< 
{ [1720 bytes data]
100 21366  100 21366    0     0   113k      0 --:--:-- --:--:-- --:--:--  112k
* Connection #0 to host qzonestyle.gtimg.cn left intact
wbyq@wbyq:~$ 

2.3 curl调用接口获取北京标准时间

免费的数据接口地址: http://www.k780.com/

获取北京时间:

wbyq@wbyq:~$ curl "http://api.k780.com:88/?app=life.time&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json"
{"success":"1","result":{"timestamp":"1602232873","datetime_1":"2020-10-09 16:41:13","datetime_2":"2020年10月09日 16时41分13秒","week_1":"5","week_2":"星期五","week_3":"周五","week_4":"Friday"}}

2.4 curl调用接口获取天气预报

获取南昌未来7天的天气预报。

wbyq@wbyq:~$ curl "http://api.k780.com:88/?app=weather.future&weaid=南昌&&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json"
{"success":"1","result":[{"weaid":"237","days":"2020-10-09","week":"星期五","cityno":"nanchang","citynm":"南昌","cityid":"101240101","temperature":"25℃/17℃","humidity":"0%/0%","weather":"晴","weather_icon":"http://api.k780.com/upload/weather/d/0.gif","weather_icon1":"http://api.k780.com/upload/weather/n/0.gif","wind":"北风","winp":"小于3级","temp_high":"25","temp_low":"17","humi_high":"0","humi_low":"0","weatid":"1","weatid1":"1","windid":"8","winpid":"0","weather_iconid":"0","weather_iconid1":"0"},{"weaid":"237","days":"2020-10-10","week":"星期六","cityno":"nanchang","citynm":"南昌","cityid":"101240101","temperature":"25℃/17℃","humidity":"0%/0%","weather":"晴","weather_icon":"http://api.k780.com/upload/weather/d/0.gif","weather_icon1":"http://api.k780.com/upload/weather/n/0.gif","wind":"东北风转北风","winp":"小于3级","temp_high":"25","temp_low":"17","humi_high":"0","humi_low":"0","weatid":"1","weatid1":"1","windid":"1","winpid":"0","weather_iconid":"0","weather_iconid1":"0"},{"weaid":"237","days":"2020-10-11","week":"星期日","cityno":"nanchang","citynm":"南昌","cityid":"101240101","temperature":"25℃/17℃","humidity":"0%/0%","weather":"晴","weather_icon":"http://api.k780.com/upload/weather/d/0.gif","weather_icon1":"http://api.k780.com/upload/weather/n/0.gif","wind":"北风","winp":"小于3级","temp_high":"25","temp_low":"17","humi_high":"0","humi_low":"0","weatid":"1","weatid1":"1","windid":"8","winpid":"0","weather_iconid":"0","weather_iconid1":"0"},{"weaid":"237","days":"2020-10-12","week":"星期一","cityno":"nanchang","citynm":"南昌","cityid":"101240101","temperature":"25℃/17℃","humidity":"0%/0%","weather":"晴","weather_icon":"http://api.k780.com/upload/weather/d/0.gif","weather_icon1":"http://api.k780.com/upload/weather/n/0.gif","wind":"北风","winp":"3-4级","temp_high":"25","temp_low":"17","humi_high":"0","humi_low":"0","weatid":"1","weatid1":"1","windid":"8","winpid":"1","weather_iconid":"0","weather_iconid1":"0"},{"weaid":"237","days":"2020-10-13","week":"星期二","cityno":"nanchang","citynm":"南昌","cityid":"101240101","temperature":"25℃/18℃","humidity":"0%/0%","weather":"晴转多云","weather_icon":"http://api.k780.com/upload/weather/d/0.gif","weather_icon1":"http://api.k780.com/upload/weather/n/1.gif","wind":"北风","winp":"3-4级","temp_high":"25","temp_low":"18","humi_high":"0","humi_low":"0","weatid":"1","weatid1":"2","windid":"8","winpid":"1","weather_iconid":"0","weather_iconid1":"1"},{"weaid":"237","days":"2020-10-14","week":"星期三","cityno":"nanchang","citynm":"南昌","cityid":"101240101","temperature":"22℃/17℃","humidity":"0%/0%","weather":"阴","weather_icon":"http://api.k780.com/upload/weather/d/2.gif","weather_icon1":"http://api.k780.com/upload/weather/n/2.gif","wind":"北风","winp":"3-4级","temp_high":"22","temp_low":"17","humi_high":"0","humi_low":"0","weatid":"3","weatid1":"3","windid":"8","winpid":"1","weather_iconid":"2","weather_iconid1":"2"},{"weaid":"237","days":"2020-10-15","week":"星期四","cityno":"nanchang","citynm":"南昌","cityid":"101240101","temperature":"19℃/16℃","humidity":"0%/0%","weather":"中雨转大雨","weather_icon":"http://api.k780.com/upload/weather/d/8.gif","weather_icon1":"http://api.k780.com/upload/weather/n/9.gif","wind":"无持续风向","winp":"小于3级","temp_high":"19","temp_low":"16","humi_high":"0","humi_low":"0","weatid":"9","weatid1":"10","windid":"0","winpid":"0","weather_iconid":"8","weather_iconid1":"9"}]}

请求参数说明

image.png

可以是 weather.city 接口中的 weaid,cityid,citynm;也可以是 IP 地址(系统自动分析该 IP 所在城市)

jsoncallback string 0 js 跨域使用 jsonp 时可使用此参数

返回参数说明

image.png

具体的参数介绍可以到这里查看:http://www.k780.com/

2.5  使用post请求向服务器上传参数

语法格式:

curl http://ip:port/service/name/op -X POST -d 'param1=16&param2=0&param3=[{"value":1,"value":1}]'

例如:

curl "http://xxx.xxx.xxx:8083/carmonitorsys/errlog/upload.action?token=6899fa62-86b3-4fe3-925e-8afd31dd3370&devId=1126626497aaa88" -X POST -H "Content-type:application/json" -d '{"日志":"2020年10月9日"}'


目录
相关文章
|
2月前
|
Linux 应用服务中间件 Shell
二、Linux文本处理与文件操作核心命令
熟悉了Linux的基本“行走”后,就该拿起真正的“工具”干活了。用grep这个“放大镜”在文件里搜索内容,用find这个“探测器”在系统中寻找文件,再用tar把东西打包带走。最关键的是要学会使用管道符|,它像一条流水线,能把这些命令串联起来,让简单工具组合出强大的功能,比如 ps -ef | grep 'nginx' 就能快速找出nginx进程。
420 2
二、Linux文本处理与文件操作核心命令
|
2月前
|
Linux
linux命令—stat
`stat` 是 Linux 系统中用于查看文件或文件系统详细状态信息的命令。相比 `ls -l`,它提供更全面的信息,包括文件大小、权限、所有者、时间戳(最后访问、修改、状态变更时间)、inode 号、设备信息等。其常用选项包括 `-f` 查看文件系统状态、`-t` 以简洁格式输出、`-L` 跟踪符号链接,以及 `-c` 或 `--format` 自定义输出格式。通过这些选项,用户可以灵活获取所需信息,适用于系统调试、权限检查、磁盘管理等场景。
290 137
|
2月前
|
安全 Ubuntu Unix
一、初识 Linux 与基本命令
玩转Linux命令行,就像探索一座新城市。首先要熟悉它的“地图”,也就是/根目录下/etc(放配置)、/home(住家)这些核心区域。然后掌握几个“生存口令”:用ls看周围,cd去别处,mkdir建新房,cp/mv搬东西,再用cat或tail看文件内容。最后,别忘了随时按Tab键,它能帮你自动补全命令和路径,是提高效率的第一神器。
676 57
|
5月前
|
JSON 自然语言处理 Linux
linux命令—tree
tree是一款强大的Linux命令行工具,用于以树状结构递归展示目录和文件,直观呈现层级关系。支持多种功能,如过滤、排序、权限显示及格式化输出等。安装方法因系统而异常用场景包括:基础用法(显示当前或指定目录结构)、核心参数应用(如层级控制-L、隐藏文件显示-a、完整路径输出-f)以及进阶操作(如磁盘空间分析--du、结合grep过滤内容、生成JSON格式列表-J等)。此外,还可生成网站目录结构图并导出为HTML文件。注意事项:使用Tab键补全路径避免错误;超大目录建议限制遍历层数;脚本中推荐禁用统计信息以优化性能。更多详情可查阅手册mantree。
502 143
linux命令—tree
|
1月前
|
存储 安全 Linux
Linux卡在emergency mode怎么办?xfs_repair 命令轻松解决
Linux虚拟机遇紧急模式?别慌!多因磁盘挂载失败。本文教你通过日志定位问题,用`xfs_repair`等工具修复文件系统,三步快速恢复。掌握查日志、修磁盘、验重启,轻松应对紧急模式,保障系统稳定运行。
399 2
|
2月前
|
缓存 监控 Linux
Linux内存问题排查命令详解
Linux服务器卡顿?可能是内存问题。掌握free、vmstat、sar三大命令,快速排查内存使用情况。free查看实时内存,vmstat诊断系统整体性能瓶颈,sar实现长期监控,三者结合,高效定位并解决内存问题。
273 0
Linux内存问题排查命令详解
|
2月前
|
Unix Linux 程序员
Linux文本搜索工具grep命令使用指南
以上就是对Linux环境下强大工具 `grep` 的基础到进阶功能介绍。它不仅能够执行简单文字查询任务还能够处理复杂文字处理任务,并且支持强大而灵活地正则表达规范来增加查询精度与效率。无论您是程序员、数据分析师还是系统管理员,在日常工作中熟练运用该命令都将极大提升您处理和分析数据效率。
273 16
|
4月前
|
监控 Linux 网络安全
Linux命令大全:从入门到精通
日常使用的linux命令整理
817 14
|
5月前
|
Linux 网络安全 数据安全/隐私保护
使用Linux系统的mount命令挂载远程服务器的文件夹。
如此一来,你就完成了一次从你的Linux发车站到远程服务器文件夹的有趣旅行。在这个技术之旅中,你既探索了新地方,也学到了如何桥接不同系统之间的距离。
951 21
|
5月前
|
监控 Linux
Linux系统中使用df命令详解磁盘使用情况。
`df`命令是Linux系统管理员和用户监控和管理磁盘空间使用的重要工具。掌握它的基本使用方法和选项可以帮助在必要时分析和解决空间相关问题。简洁但功能丰富,`df`命令确保了用户可以快速有效地识别和管理文件系统的空间使用情况。
430 13