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日"}'


目录
相关文章
|
10天前
|
监控 Linux
如何检查 Linux 内存使用量是否耗尽?这 5 个命令堪称绝了!
本文介绍了在Linux系统中检查内存使用情况的5个常用命令:`free`、`top`、`vmstat`、`pidstat` 和 `/proc/meminfo` 文件,帮助用户准确监控内存状态,确保系统稳定运行。
88 6
|
11天前
|
Linux
在 Linux 系统中,“cd”命令用于切换当前工作目录
在 Linux 系统中,“cd”命令用于切换当前工作目录。本文详细介绍了“cd”命令的基本用法和常见技巧,包括使用“.”、“..”、“~”、绝对路径和相对路径,以及快速切换到上一次工作目录等。此外,还探讨了高级技巧,如使用通配符、结合其他命令、在脚本中使用,以及实际应用案例,帮助读者提高工作效率。
47 3
|
11天前
|
监控 安全 Linux
在 Linux 系统中,网络管理是重要任务。本文介绍了常用的网络命令及其适用场景
在 Linux 系统中,网络管理是重要任务。本文介绍了常用的网络命令及其适用场景,包括 ping(测试连通性)、traceroute(跟踪路由路径)、netstat(显示网络连接信息)、nmap(网络扫描)、ifconfig 和 ip(网络接口配置)。掌握这些命令有助于高效诊断和解决网络问题,保障网络稳定运行。
35 2
|
18天前
|
缓存 监控 Linux
|
22天前
|
Linux Shell 数据安全/隐私保护
|
23天前
|
域名解析 网络协议 安全
|
5天前
|
运维 监控 网络协议
运维工程师日常工作中最常用的20个Linux命令,涵盖文件操作、目录管理、权限设置、系统监控等方面
本文介绍了运维工程师日常工作中最常用的20个Linux命令,涵盖文件操作、目录管理、权限设置、系统监控等方面,旨在帮助读者提高工作效率。从基本的文件查看与编辑,到高级的网络配置与安全管理,这些命令是运维工作中的必备工具。
31 3
|
29天前
|
运维 监控 网络协议
|
11天前
|
安全 网络协议 Linux
本文详细介绍了 Linux 系统中 ping 命令的使用方法和技巧,涵盖基本用法、高级用法、实际应用案例及注意事项。
本文详细介绍了 Linux 系统中 ping 命令的使用方法和技巧,涵盖基本用法、高级用法、实际应用案例及注意事项。通过掌握 ping 命令,读者可以轻松测试网络连通性、诊断网络问题并提升网络管理能力。
42 3
|
14天前
|
Linux
在 Linux 系统中,`find` 命令是一个强大的文件查找工具
在 Linux 系统中,`find` 命令是一个强大的文件查找工具。本文详细介绍了 `find` 命令的基本语法、常用选项和具体应用示例,帮助用户快速掌握如何根据文件名、类型、大小、修改时间等条件查找文件,并展示了如何结合逻辑运算符、正则表达式和排除特定目录等高级用法。
45 6
下一篇
无影云桌面