Curl 与 Wget 下载工具教程
Curl 和 Wget 是 Linux 系统中两个最常用的网络传输工具。Curl 功能更为全面,支持多种协议和 API 调用;Wget 则在文件下载和网站镜像方面表现出色。在搬瓦工 VPS 上,这两个工具广泛用于下载软件包、调用 API、测试网络连接等场景。本教程将全面讲解它们的用法。
一、Curl 基础用法
1.1 基本请求
# GET 请求
curl https://example.com
# 显示响应头
curl -I https://example.com
# 显示响应头和正文
curl -i https://example.com
# 静默模式(不显示进度)
curl -s https://example.com
# 跟随重定向
curl -L https://example.com
# 保存到文件
curl -o output.html https://example.com
curl -O https://example.com/file.tar.gz
1.2 HTTP 方法
# POST 请求
curl -X POST https://api.example.com/data \
-H "Content-Type: application/json" \
-d '{"name":"test","value":123}'
# PUT 请求
curl -X PUT https://api.example.com/data/1 \
-H "Content-Type: application/json" \
-d '{"name":"updated"}'
# DELETE 请求
curl -X DELETE https://api.example.com/data/1
# PATCH 请求
curl -X PATCH https://api.example.com/data/1 \
-H "Content-Type: application/json" \
-d '{"status":"active"}'
1.3 请求头与认证
# 自定义请求头
curl -H "Authorization: Bearer TOKEN" \
-H "Accept: application/json" \
https://api.example.com/data
# Basic 认证
curl -u username:password https://api.example.com/
# Bearer Token
curl -H "Authorization: Bearer YOUR_TOKEN" https://api.example.com/
# Cookie 操作
curl -c cookies.txt https://example.com/login # 保存 Cookie
curl -b cookies.txt https://example.com/dashboard # 发送 Cookie
# User-Agent
curl -A "Mozilla/5.0" https://example.com
1.4 文件上传
# 表单文件上传
curl -F "file=@/path/to/file.pdf" https://example.com/upload
# 多文件上传
curl -F "file1=@image1.jpg" -F "file2=@image2.jpg" https://example.com/upload
# 表单数据
curl -X POST https://example.com/form \
-d "username=admin&password=123456"
# URL 编码数据
curl --data-urlencode "query=搬瓦工 VPS" https://example.com/search
二、Curl 高级功能
2.1 SSL/TLS
# 忽略 SSL 证书验证(测试用)
curl -k https://self-signed.example.com
# 指定 CA 证书
curl --cacert /path/to/ca.crt https://example.com
# 客户端证书认证
curl --cert client.crt --key client.key https://example.com
# 查看 SSL 证书信息
curl -vI https://example.com 2>&1 | grep -A 6 "Server certificate"
2.2 网络调试
# 详细输出
curl -v https://example.com
# 测量响应时间
curl -o /dev/null -s -w "DNS解析: %{time_namelookup}s\n连接耗时: %{time_connect}s\nTLS握手: %{time_appconnect}s\n首字节: %{time_starttransfer}s\n总耗时: %{time_total}s\nHTTP状态: %{http_code}\n" https://example.com
# 限速下载
curl --limit-rate 1M -O https://example.com/large-file.tar.gz
# 设置超时
curl --connect-timeout 5 --max-time 30 https://example.com
# 使用代理
curl -x http://proxy:8080 https://example.com
curl -x socks5://proxy:1080 https://example.com
2.3 断点续传
# 断点续传下载
curl -C - -O https://example.com/large-file.iso
# 指定下载范围
curl -r 0-999999 -o part1 https://example.com/file.tar.gz
curl -r 1000000-1999999 -o part2 https://example.com/file.tar.gz
三、Wget 基础用法
3.1 文件下载
# 基本下载
wget https://example.com/file.tar.gz
# 指定输出文件名
wget -O custom_name.tar.gz https://example.com/file.tar.gz
# 后台下载
wget -b https://example.com/large-file.iso
# 静默模式
wget -q https://example.com/file.tar.gz
# 显示进度条
wget --progress=bar https://example.com/file.tar.gz
3.2 断点续传与重试
# 断点续传
wget -c https://example.com/large-file.iso
# 设置重试次数
wget --tries=5 https://example.com/file.tar.gz
# 设置超时
wget --timeout=30 https://example.com/file.tar.gz
# 限速下载
wget --limit-rate=1m https://example.com/large-file.iso
3.3 批量下载
# 从文件读取 URL 列表批量下载
wget -i url_list.txt
# 递归下载网站
wget -r -l 2 -np https://example.com/docs/
# 下载整个网站(镜像)
wget --mirror --convert-links --adjust-extension \
--page-requisites --no-parent https://example.com/
# 下载特定文件类型
wget -r -A "*.pdf,*.doc" https://example.com/docs/
# 排除特定目录
wget -r --exclude-directories=/temp,/cache https://example.com/
四、实际应用场景
4.1 搬瓦工 API 调用
# 获取 VPS 信息(通过 bwh81.net)
curl -s "https://api.64clouds.com/v1/getServiceInfo?veid=YOUR_VEID&api_key=YOUR_KEY" | jq .
# 重启 VPS
curl -s "https://api.64clouds.com/v1/restart?veid=YOUR_VEID&api_key=YOUR_KEY"
# 获取流量使用情况
curl -s "https://api.64clouds.com/v1/getLiveServiceInfo?veid=YOUR_VEID&api_key=YOUR_KEY" | \
jq '{data_counter: .data_counter, data_next_reset: .data_next_reset}'
4.2 健康检查脚本
#!/bin/bash
# 网站健康检查
URLS=(
"https://example.com"
"https://api.example.com/health"
"https://blog.example.com"
)
for URL in "${URLS[@]}"; do
STATUS=$(curl -o /dev/null -s -w "%{http_code}" --max-time 10 "$URL")
if [ "$STATUS" -eq 200 ]; then
echo "[OK] $URL ($STATUS)"
else
echo "[FAIL] $URL ($STATUS)"
fi
done
4.3 下载并安装软件
# 下载并安装 Node.js
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt install nodejs -y
# 下载并验证文件完整性
wget https://example.com/app.tar.gz
wget https://example.com/app.tar.gz.sha256
sha256sum -c app.tar.gz.sha256
# 下载最新版 Docker Compose
COMPOSE_VERSION=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | jq -r .tag_name)
curl -L "https://github.com/docker/compose/releases/download/${COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
五、Curl 与 Wget 对比
- 协议支持:Curl 支持更多协议(HTTP、FTP、SMTP、IMAP 等),Wget 主要支持 HTTP 和 FTP。
- 递归下载:Wget 内建递归下载功能,Curl 不支持。
- API 调用:Curl 更适合 RESTful API 交互,支持各种 HTTP 方法和请求头。
- 断点续传:两者都支持,但 Wget 默认处理更智能。
- 输出控制:Curl 的输出格式化更灵活(
-w选项)。
总结
Curl 和 Wget 是搬瓦工 VPS 上不可或缺的网络工具。Curl 适合 API 调用和网络调试,Wget 适合文件下载和网站镜像。掌握这两个工具,配合 jq JSON 处理 和 Bash 脚本编程,可以构建强大的自动化运维流程。选购搬瓦工 VPS 请查看全部方案,使用优惠码 NODESEEK2026 可享受 6.77% 循环折扣。