博客 > 一篇文了解Linux命令cURL
浏览量:4057次评论:0次
作者:锐成网络整理时间:2024-05-21 13:49:45
cURL是一款在Linux系统中广泛使用的命令行工具,它能够通过各种协议进行数据传输和交互。无论在开发、测试还是日常使用中,cURL都展现出其强大的功能和灵活性。本文将为您详细介绍Linux命令cURL,包括cURL概念、curl命令参数详解、cURL命令退出码以及用法演示,以供参考。
cURL概念
cURL 是常用的命令行工具,用来请求 Web 服务器。它的名字就是客户端(client)的 URL 工具的意思。
cURL 的原作者是 Daniel Stenberg (目前是 cURL 的核心开发者),同时也是 IETF HTTPbis 工作组的资深成员。Daniel 在 1998 年创建了 curl 项目,他编写了最初的 curl 版本,并创建了 libcurl 库。到目前为止,代码仓库包括的 24000 次 commit 有超过一半是 Daniel 本人提交的,他依然是项目的核心开发者。Daniel 表示已将 curl 视为自己的孩子。
作为一款强力工具,支持的协议包括 (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET and TFTP),还支持POST、cookies、认证、从指定偏移处下载部分文件等功能,具有用户代理字符串、限速、文件大小、进度条、cookie支持、用户认证、断点续传等特征。
一、命令的安装
sudo apt-get install curl
二、cURL命令语法
curl [options] [URL...]
三、URL格式
在WWW上,每一信息资源都有统一的且在网上唯一的地址,该地址就叫URL(Uniform Resource Locator,统一资源定位器),它是WWW的统一资源定位标志,就是指网络地址。URL的格式定义要参考 RFC 1808 。
地址:http://www.w3.org/Addressing/rfc1808.txt
《Relative Uniform Resource Locators 》 
URL由三部分组成:资源类型、存放资源的主机域名、资源文件名。也可认为由4部分组成:协议、主机、端口、路径URL的一般语法格式为:
protocol :// hostname[:port] / path / [;parameters][?query]#fragment
(带方括号[]的为可选项)。
protocol(协议)
指定使用的传输协议,下表列出 protocol 属性的有效方案名称。最常用的是HTTP协议,它也是WWW中应用最广的协议。
file 资源是本地计算机上的文件。格式file:///,注意后边应是三个斜杠。
ftp 通过 FTP访问资源。格式 FTP://
gopher 通过 Gopher 协议访问该资源。
http 通过 HTTP 访问该资源。格式 HTTP://
https 通过安全的 HTTPS 访问该资源。格式 HTTPS://
mailto 资源为电子邮件地址,通过 SMTP 访问。格式 mailto:
MMS 通过 支持MMS(流媒体)协议的播放该资源。(代表软件:Windows Media Player)格式 MMS://
ed2k 通过 支持ed2k(专用下载链接)协议的P2P软件访问该资源。(代表软件:电驴) 格式 ed2k://
Flashget 通过 支持Flashget:(专用下载链接)协议的P2P软件访问该资源。(代表软件:快车) 格式 Flashget://
thunder 通过 支持thunder(专用下载链接)协议的P2P软件访问该资源。(代表软件:迅雷) 格式 thunder://
news 通过 NNTP 访问该资源。
hostname(主机名)
是指存放资源的服务器的域名系统(DNS) 主机名或 IP 地址。有时,在主机名前也可以包含连接到服务器所需的用户名和密码(格式:username:password@hostname)。
port(端口号)
整数,可选,省略时使用方案的默认端口,各种传输协议都有默认的端口号,如http的默认端口为80。如果输入时省略,则使用默认端口号。有时候出于安全或其他考虑,可以在服务器上对端口进行重定义,即采用非标准端口号,此时,URL中就不能省略端口号这一项。
path(路径)
由零或多个“/”符号隔开的字符串,一般用来表示主机上的一个目录或文件地址。
parameters(参数)
这是用于指定特殊参数的可选项。
query(查询)
可选,用于给动态网页(如使用CGI、ISAPI、PHP/JSP/ASP/ASP.NET等技术制作的网页)传递参数,可有多个参数,用“&”符号隔开,每个参数的名和值用“=”符号隔开。
fragment(信息片断)
字符串,用于指定网络资源中的片断。例如一个网页中有多个名词解释,可使用fragment直接定位到某一名词解释。
四、curl命令参数详解
由于linux curl功能十分强大,所以命令参数十分多,下表只筛选出来部分常用的参数,更多参数请运行“man curl”命令查看。
在以下选项中,(H) 表示仅适用 HTTP/HTTPS ,(F) 表示仅适用于 FTP
| 
			 参数1  | 
			
			 参数2  | 
			
			 说明  | 
		
| 
			 
  | 
			
			 --anyauth  | 
			
			 选择 "any" 认证方法 (H)  | 
		
| 
			 -a,  | 
			
			 --append  | 
			
			 添加要上传的文件 (F/SFTP)  | 
		
| 
			 
  | 
			
			 --basic  | 
			
			 使用HTTP基础认证(Basic Authentication)(H)  | 
		
| 
			 
  | 
			
			 --cacert FILE  | 
			
			 CA 证书,用于每次请求认证 (SSL)  | 
		
| 
			 
  | 
			
			 --capath DIR  | 
			
			 CA 证书目录 (SSL)  | 
		
| 
			 -E,  | 
			
			 --cert CERT[:PASSWD]  | 
			
			 客户端证书文件及密码 (SSL)  | 
		
| 
			 
  | 
			
			 --cert-type TYPE  | 
			
			 证书文件类型 (DER/PEM/ENG) (SSL)  | 
		
| 
			 
  | 
			
			 --ciphers LIST  | 
			
			 SSL 秘钥 (SSL)  | 
		
| 
			 
  | 
			
			 --compressed  | 
			
			 请求压缩 (使用 deflate 或 gzip)  | 
		
| 
			 -K,  | 
			
			 --config FILE  | 
			
			 指定配置文件  | 
		
| 
			 
  | 
			
			 --connect-timeout SECONDS  | 
			
			 连接超时设置  | 
		
| 
			 -C,  | 
			
			 --continue-at OFFSET  | 
			
			 断点续转  | 
		
| 
			 -b,  | 
			
			 --cookie STRING/FILE  | 
			
			 Cookies字符串或读取Cookies的文件位置 (H)  | 
		
| 
			 -c,  | 
			
			 --cookie-jar FILE  | 
			
			 操作结束后,要写入 Cookies 的文件位置 (H)  | 
		
| 
			 
  | 
			
			 --create-dirs  | 
			
			 创建必要的本地目录层次结构  | 
		
| 
			 
  | 
			
			 --crlf  | 
			
			 在上传时将 LF 转写为 CRLF  | 
		
| 
			 
  | 
			
			 --crlfile FILE  | 
			
			 从指定的文件获得PEM格式CRL列表  | 
		
| 
			 -d,  | 
			
			 --data DATA  | 
			
			 HTTP POST 数据 (H)  | 
		
| 
			 
  | 
			
			 --data-ascii DATA ASCII  | 
			
			 编码 HTTP POST 数据 (H)  | 
		
| 
			 
  | 
			
			 --data-binary DATA binary  | 
			
			 编码 HTTP POST 数据 (H)  | 
		
| 
			 
  | 
			
			 --data-urlencode DATA url  | 
			
			 编码 HTTP POST 数据 (H)  | 
		
| 
			 
  | 
			
			 --delegation STRING GSS-API  | 
			
			 委托权限  | 
		
| 
			 
  | 
			
			 --digest  | 
			
			 使用数字身份验证 (H)  | 
		
| 
			 
  | 
			
			 --disable-eprt  | 
			
			 禁止使用 EPRT 或 LPRT (F)  | 
		
| 
			 
  | 
			
			 --disable-epsv  | 
			
			 禁止使用 EPSV (F)  | 
		
| 
			 -D,  | 
			
			 --dump-header FILE  | 
			
			 将头信息写入指定的文件  | 
		
| 
			 
  | 
			
			 --egd-file FILE  | 
			
			 为随机数据设置EGD socket路径(SSL)  | 
		
| 
			 
  | 
			
			 --engine ENGINGE  | 
			
			 加密引擎 (SSL). "--engine list" 指定列表  | 
		
| 
			 -f,  | 
			
			 --fail  | 
			
			 连接失败时不显示HTTP错误信息 (H)  | 
		
| 
			 -F,  | 
			
			 --form CONTENT  | 
			
			 模拟 HTTP 表单数据提交(multipart POST) (H)  | 
		
| 
			 
  | 
			
			 --form-string STRING  | 
			
			 模拟 HTTP 表单数据提交 (H)  | 
		
| 
			 
  | 
			
			 --ftp-account DATA  | 
			
			 帐户数据提交 (F)  | 
		
| 
			 
  | 
			
			 --ftp-alternative-to-user COMMAND  | 
			
			 指定替换 "USER [name]" 的字符串 (F)  | 
		
| 
			 
  | 
			
			 --ftp-create-dirs  | 
			
			 如果不存在则创建远程目录 (F)  | 
		
| 
			 
  | 
			
			 --ftp-method [MULTICWD/NOCWD/SINGLECWD]  | 
			
			 控制 CWD (F)  | 
		
| 
			 
  | 
			
			 --ftp-pasv  | 
			
			 使用 PASV/EPSV 替换 PORT (F)  | 
		
| 
			 -P,  | 
			
			 --ftp-port ADR  | 
			
			 使用指定 PORT 及地址替换 PASV (F)  | 
		
| 
			 
  | 
			
			 --ftp-skip-pasv-ip  | 
			
			 跳过 PASV 的IP地址 (F)  | 
		
| 
			 
  | 
			
			 --ftp-pret  | 
			
			 在 PASV 之前发送 PRET (drftpd) (F)  | 
		
| 
			 
  | 
			
			 --ftp-ssl-ccc  | 
			
			 在认证之后发送 CCC (F)  | 
		
| 
			 
  | 
			
			 --ftp-ssl-ccc-mode ACTIVE/PASSIVE  | 
			
			 设置 CCC 模式 (F)  | 
		
| 
			 
  | 
			
			 --ftp-ssl-control ftp  | 
			
			 登录时需要 SSL/TLS (F)  | 
		
| 
			 -G,  | 
			
			 --get  | 
			
			 使用 HTTP GET 方法发送 -d 数据 (H)  | 
		
| 
			 -g,  | 
			
			 --globoff  | 
			
			 禁用的 URL 队列 及范围使用 {} 和 []  | 
		
| 
			 -H,  | 
			
			 --header LINE  | 
			
			 要发送到服务端的自定义请求头 (H)  | 
		
| 
			 -I,  | 
			
			 --head  | 
			
			 仅显示响应文档头  | 
		
| 
			 -h,  | 
			
			 --help  | 
			
			 显示帮助  | 
		
| 
			 -0,  | 
			
			 --http1.0  | 
			
			 使用 HTTP 1.0 (H)  | 
		
| 
			 
  | 
			
			 --ignore-content-length  | 
			
			 忽略 HTTP Content-Length 头  | 
		
| 
			 -i,  | 
			
			 --include  | 
			
			 在输出中包含协议头 (H/F)  | 
		
| 
			 -k,  | 
			
			 --insecure  | 
			
			 允许连接到 SSL 站点,而不使用证书 (H)  | 
		
| 
			 
  | 
			
			 --interface INTERFACE  | 
			
			 指定网络接口/地址  | 
		
| 
			 -4,  | 
			
			 --ipv4  | 
			
			 将域名解析为 IPv4 地址  | 
		
| 
			 -6,  | 
			
			 --ipv6  | 
			
			 将域名解析为 IPv6 地址  | 
		
| 
			 -j,  | 
			
			 --junk-session-cookies  | 
			
			 读取文件中但忽略会话cookie (H)  | 
		
| 
			 
  | 
			
			 --keepalive-time SECONDS  | 
			
			 keepalive 包间隔  | 
		
| 
			 
  | 
			
			 --key KEY  | 
			
			 私钥文件名 (SSL/SSH)  | 
		
| 
			 
  | 
			
			 --key-type TYPE  | 
			
			 私钥文件类型 (DER/PEM/ENG) (SSL)  | 
		
| 
			 
  | 
			
			 --krb LEVEL  | 
			
			 启用指定安全级别的 Kerberos (F)  | 
		
| 
			 
  | 
			
			 --libcurl FILE  | 
			
			 命令的libcurl等价代码  | 
		
| 
			 
  | 
			
			 --limit-rate RATE  | 
			
			 限制传输速度  | 
		
| 
			 -l,  | 
			
			 --list-only  | 
			
			 只列出FTP目录的名称 (F)  | 
		
| 
			 
  | 
			
			 --local-port RANGE  | 
			
			 强制使用的本地端口号  | 
		
| 
			 -L,  | 
			
			 --location  | 
			
			 跟踪重定向 (H)  | 
		
| 
			 
  | 
			
			 --location-trusted  | 
			
			 类似 --location 并发送验证信息到其它主机 (H)  | 
		
| 
			 -M,  | 
			
			 --manual  | 
			
			 显示全手动  | 
		
| 
			 
  | 
			
			 --mail-from FROM  | 
			
			 从这个地址发送邮件  | 
		
| 
			 
  | 
			
			 --mail-rcpt TO  | 
			
			 发送邮件到这个接收人(s)  | 
		
| 
			 
  | 
			
			 --mail-auth AUTH  | 
			
			 原始电子邮件的起始地址  | 
		
| 
			 
  | 
			
			 --max-filesize BYTES  | 
			
			 下载的最大文件大小 (H/F)  | 
		
| 
			 
  | 
			
			 --max-redirs NUM  | 
			
			 最大重定向数 (H)  | 
		
| 
			 -m,  | 
			
			 --max-time SECONDS  | 
			
			 允许的最多传输时间  | 
		
| 
			 
  | 
			
			 --metalink  | 
			
			 处理指定的URL上的XML文件  | 
		
| 
			 
  | 
			
			 --negotiate  | 
			
			 使用 HTTP Negotiate 认证 (H)  | 
		
| 
			 -n,  | 
			
			 --netrc  | 
			
			 必须从 .netrc 文件读取用户名和密码  | 
		
| 
			 
  | 
			
			 --netrc-optional  | 
			
			 使用 .netrc 或 URL; 将重写 -n 参数  | 
		
| 
			 
  | 
			
			 --netrc-file FILE  | 
			
			 设置要使用的 netrc 文件名  | 
		
| 
			 -N,  | 
			
			 --no-buffer  | 
			
			 禁用输出流的缓存  | 
		
| 
			 
  | 
			
			 --no-keepalive  | 
			
			 禁用 connection 的 keepalive  | 
		
| 
			 
  | 
			
			 --no-sessionid  | 
			
			 禁止重复使用 SSL session-ID (SSL)  | 
		
| 
			 
  | 
			
			 --noproxy  | 
			
			 不使用代理的主机列表  | 
		
| 
			 
  | 
			
			 --ntlm  | 
			
			 使用 HTTP NTLM 认证 (H)  | 
		
| 
			 -o,  | 
			
			 --output FILE  | 
			
			 将输出写入文件,而非 stdout  | 
		
| 
			 
  | 
			
			 --pass PASS  | 
			
			 传递给私钥的短语 (SSL/SSH)  | 
		
| 
			 
  | 
			
			 --post301  | 
			
			 在 301 重定向后不要切换为 GET 请求 (H)  | 
		
| 
			 
  | 
			
			 --post302  | 
			
			 在 302 重定向后不要切换为 GET 请求 (H)  | 
		
| 
			 
  | 
			
			 --post303  | 
			
			 在 303 重定向后不要切换为 GET 请求 (H)  | 
		
| 
			 -#,  | 
			
			 --progress-bar  | 
			
			 以进度条显示传输进度  | 
		
| 
			 
  | 
			
			 --proto PROTOCOLS  | 
			
			 启用/禁用 指定的协议  | 
		
| 
			 
  | 
			
			 --proto-redir PROTOCOLS  | 
			
			 在重定向上 启用/禁用 指定的协议  | 
		
| 
			 -x,  | 
			
			 --proxy [PROTOCOL://]HOST[:PORT]  | 
			
			 在指定的端口上使用代理  | 
		
| 
			 
  | 
			
			 --proxy-anyauth  | 
			
			 在代理上使用 "any" 认证方法 (H)  | 
		
| 
			 
  | 
			
			 --proxy-basic  | 
			
			 在代理上使用 Basic 认证 (H)  | 
		
| 
			 
  | 
			
			 --proxy-digest  | 
			
			 在代理上使用 Digest 认证 (H)  | 
		
| 
			 
  | 
			
			 --proxy-negotiate  | 
			
			 在代理上使用 Negotiate 认证 (H)  | 
		
| 
			 
  | 
			
			 --proxy-ntlm  | 
			
			 在代理上使用 NTLM 认证 (H)  | 
		
| 
			 -U,  | 
			
			 --proxy-user USER[:PASSWORD]  | 
			
			 代理用户名及密码  | 
		
| 
			 
  | 
			
			 --proxy1.0 HOST[:PORT]  | 
			
			 在指定的端口上使用 HTTP/1.0 代理  | 
		
| 
			 -p,  | 
			
			 --proxytunnel  | 
			
			 使用HTTP代理 (用于 CONNECT)  | 
		
| 
			 
  | 
			
			 --pubkey KEY  | 
			
			 公钥文件名 (SSH)  | 
		
| 
			 -Q,  | 
			
			 --quote CMD  | 
			
			 在传输开始前向服务器发送命令 (F/SFTP)  | 
		
| 
			 
  | 
			
			 --random-file FILE  | 
			
			 读取随机数据的文件 (SSL)  | 
		
| 
			 -r,  | 
			
			 --range RANGE  | 
			
			 仅检索范围内的字节  | 
		
| 
			 
  | 
			
			 --raw  | 
			
			 使用原始HTTP传输,而不使用编码 (H)  | 
		
| 
			 -e,  | 
			
			 --referer  | 
			
			 Referer URL (H)  | 
		
| 
			 -J,  | 
			
			 --remote-header-name  | 
			
			 从远程文件读取头信息 (H)  | 
		
| 
			 -O,  | 
			
			 --remote-name  | 
			
			 将输出写入远程文件  | 
		
| 
			 
  | 
			
			 --remote-name-all  | 
			
			 使用所有URL的远程文件名  | 
		
| 
			 -R,  | 
			
			 --remote-time  | 
			
			 将远程文件的时间设置在本地输出上  | 
		
| 
			 -X,  | 
			
			 --request COMMAND  | 
			
			 使用指定的请求命令  | 
		
| 
			 
  | 
			
			 --resolve HOST:PORT:ADDRESS  | 
			
			 将 HOST:PORT 强制解析到 ADDRESS  | 
		
| 
			 
  | 
			
			 --retry NUM  | 
			
			 出现问题时的重试次数  | 
		
| 
			 
  | 
			
			 --retry-delay SECONDS  | 
			
			 重试时的延时时长  | 
		
| 
			 
  | 
			
			 --retry-max-time SECONDS  | 
			
			 仅在指定时间段内重试  | 
		
| 
			 -S,  | 
			
			 --show-error  | 
			
			 显示错误. 在选项 -s 中,当 curl 出现错误时将显示  | 
		
| 
			 -s,  | 
			
			 --silent  | 
			
			 Silent模式。不输出任务内容  | 
		
| 
			 
  | 
			
			 --socks4 HOST[:PORT]  | 
			
			 在指定的 host + port 上使用 SOCKS4 代理  | 
		
| 
			 
  | 
			
			 --socks4a HOST[:PORT]  | 
			
			 在指定的 host + port 上使用 SOCKSa 代理  | 
		
| 
			 
  | 
			
			 --socks5 HOST[:PORT]  | 
			
			 在指定的 host + port 上使用 SOCKS5 代理  | 
		
| 
			 
  | 
			
			 --socks5-hostname HOST[:PORT] SOCKS5  | 
			
			 代理,指定用户名、密码  | 
		
| 
			 
  | 
			
			 --socks5-gssapi-service NAME  | 
			
			 为gssapi使用SOCKS5代理服务名称  | 
		
| 
			 
  | 
			
			 --socks5-gssapi-nec  | 
			
			 与NEC Socks5服务器兼容  | 
		
| 
			 -Y,  | 
			
			 --speed-limit RATE  | 
			
			 在指定限速时间之后停止传输  | 
		
| 
			 -y,  | 
			
			 --speed-time SECONDS  | 
			
			 指定时间之后触发限速. 默认 30  | 
		
| 
			 
  | 
			
			 --ssl  | 
			
			 尝试 SSL/TLS (FTP, IMAP, POP3, SMTP)  | 
		
| 
			 
  | 
			
			 --ssl-reqd  | 
			
			 需要 SSL/TLS (FTP, IMAP, POP3, SMTP)  | 
		
| 
			 -2,  | 
			
			 --sslv2  | 
			
			 使用 SSLv2 (SSL)  | 
		
| 
			 -3,  | 
			
			 --sslv3  | 
			
			 使用 SSLv3 (SSL)  | 
		
| 
			 
  | 
			
			 --ssl-allow-beast  | 
			
			 允许的安全漏洞,提高互操作性(SSL)  | 
		
| 
			 
  | 
			
			 --stderr FILE  | 
			
			 重定向 stderr 的文件位置. - means stdout  | 
		
| 
			 
  | 
			
			 --tcp-nodelay  | 
			
			 使用 TCP_NODELAY 选项  | 
		
| 
			 -t,  | 
			
			 --telnet-option OPT=VAL  | 
			
			 设置 telnet 选项  | 
		
| 
			 
  | 
			
			 --tftp-blksize VALUE  | 
			
			 设备 TFTP BLKSIZE 选项 (必须 >512)  | 
		
| 
			 -z,  | 
			
			 --time-cond TIME  | 
			
			 基于时间条件的传输  | 
		
| 
			 -1,  | 
			
			 --tlsv1  | 
			
			 使用 => TLSv1 (SSL)  | 
		
| 
			 
  | 
			
			 --tlsv1.0  | 
			
			 使用 TLSv1.0 (SSL)  | 
		
| 
			 
  | 
			
			 --tlsv1.1  | 
			
			 使用 TLSv1.1 (SSL)  | 
		
| 
			 
  | 
			
			 --tlsv1.2  | 
			
			 使用 TLSv1.2 (SSL)  | 
		
| 
			 
  | 
			
			 --trace FILE  | 
			
			 将 debug 信息写入指定的文件  | 
		
| 
			 
  | 
			
			 --trace-ascii FILE  | 
			
			 类似 --trace 但使用16进度输出  | 
		
| 
			 
  | 
			
			 --trace-time  | 
			
			 向 trace/verbose 输出添加时间戳  | 
		
| 
			 
  | 
			
			 --tr-encoding  | 
			
			 请求压缩传输编码 (H)  | 
		
| 
			 -T,  | 
			
			 --upload-file FILE  | 
			
			 将文件传输(上传)到指定位置  | 
		
| 
			 
  | 
			
			 --url URL  | 
			
			 指定所使用的 URL  | 
		
| 
			 -B,  | 
			
			 --use-ascii  | 
			
			 使用 ASCII/text 传输  | 
		
| 
			 -u,  | 
			
			 --user USER[:PASSWORD]  | 
			
			 指定服务器认证用户名、密码  | 
		
| 
			 
  | 
			
			 --tlsuser USER  | 
			
			 TLS 用户名  | 
		
| 
			 
  | 
			
			 --tlspassword STRING  | 
			
			 TLS 密码  | 
		
| 
			 
  | 
			
			 --tlsauthtype STRING  | 
			
			 TLS 认证类型 (默认 SRP)  | 
		
| 
			 
  | 
			
			 --unix-socket FILE  | 
			
			 通过这个 UNIX socket 域连接  | 
		
| 
			 -A,  | 
			
			 --user-agent STRING  | 
			
			 要发送到服务器的 User-Agent (H)  | 
		
| 
			 -v,  | 
			
			 --verbose  | 
			
			 显示详细操作信息  | 
		
| 
			 -V,  | 
			
			 --version  | 
			
			 显示版本号并退出  | 
		
| 
			 -w,  | 
			
			 --write-out FORMAT  | 
			
			 完成后输出什么  | 
		
| 
			 
  | 
			
			 --xattr  | 
			
			 将元数据存储在扩展文件属性中  | 
		
| 
			 -q  | 
			
			 
  | 
			
			 .curlrc 如果作为第一个参数无效  | 
		
五、Linux cURL命令退出码
下面是linux curl命令的错误代码和她们的相应的错误消息,命令执行错误的时候可以通过错误码来查看出错原因,方便开发调试。
| 
			 退 出 码  | 
			
			 错误描述  | 
		
| 
			 1  | 
			
			 Unsupported protocol. This build of curl has no support for this protocol.  | 
		
| 
			 2  | 
			
			 Failed to initialize.  | 
		
| 
			 3  | 
			
			 URL malformed. The syntax was not correct.  | 
		
| 
			 5  | 
			
			 Couldn't resolve proxy. The given proxy host could not be resolved.  | 
		
| 
			 6  | 
			
			 Couldn't resolve host. The given remote host was not resolved.  | 
		
| 
			 7  | 
			
			 Failed to connect to host.  | 
		
| 
			 8  | 
			
			 FTP weird server reply. The server sent data curl couldn't parse.  | 
		
| 
			 9  | 
			
			 FTP access denied. The server denied login or denied access to the particular resource or directory you wanted to reach. Most often you tried to change to a directory that doesn't exist on the server.  | 
		
| 
			 11  | 
			
			 FTP weird PASS reply. Curl couldn't parse the reply sent to the PASS request.  | 
		
| 
			 13  | 
			
			 FTP weird PASV reply, Curl couldn't parse the reply sent to the PASV request.  | 
		
| 
			 14  | 
			
			 FTP weird 227 format. Curl couldn't parse the 227-line the server sent.  | 
		
| 
			 15  | 
			
			 FTP can't get host. Couldn't resolve the host IP we got in the 227-line.  | 
		
| 
			 17  | 
			
			 FTP couldn't set binary. Couldn't change transfer method to binary.  | 
		
| 
			 18  | 
			
			 Partial file. Only a part of the file was transferred.  | 
		
| 
			 19  | 
			
			 FTP couldn't download/access the given file, the RETR (or similar) command failed.  | 
		
| 
			 21  | 
			
			 FTP quote error. A quote command returned error from the server.  | 
		
| 
			 22  | 
			
			 HTTP page not retrieved. The requested url was not found or returned another error with the HTTP error code being 400 or above. This return code only appears if -f/--fail is used.  | 
		
| 
			 23  | 
			
			 Write error. Curl couldn't write data to a local filesystem or similar.  | 
		
| 
			 25  | 
			
			 FTP couldn't STOR file. The server denied the STOR operation, used for FTP uploading.  | 
		
| 
			 26  | 
			
			 Read error. Various reading problems.  | 
		
| 
			 27  | 
			
			 Out of memory. A memory allocation request failed.  | 
		
| 
			 28  | 
			
			 Operation timeout. The specified time-out period was reached according to the conditions.  | 
		
| 
			 30  | 
			
			 FTP PORT failed. The PORT command failed. Not all FTP servers support the PORT command, try doing a transfer using PASV instead!  | 
		
| 
			 31  | 
			
			 FTP couldn't use REST. The REST command failed. This command is used for resumed FTP transfers.  | 
		
| 
			 33  | 
			
			 HTTP range error. The range "command" didn't work.  | 
		
| 
			 34  | 
			
			 HTTP post error. Internal post-request generation error.  | 
		
| 
			 35  | 
			
			 SSL connect error. The SSL handshaking failed.  | 
		
| 
			 36  | 
			
			 FTP bad download resume. Couldn't continue an earlier aborted download.  | 
		
| 
			 37  | 
			
			 FILE couldn't read file. Failed to open the file. Permissions?  | 
		
| 
			 38  | 
			
			 LDAP cannot bind. LDAP bind operation failed.  | 
		
| 
			 39  | 
			
			 LDAP search failed.  | 
		
| 
			 41  | 
			
			 Function not found. A required LDAP function was not found.  | 
		
| 
			 42  | 
			
			 Aborted by callback. An application told curl to abort the operation.  | 
		
| 
			 43  | 
			
			 Internal error. A function was called with a bad parameter.  | 
		
| 
			 45  | 
			
			 Interface error. A specified outgoing interface could not be used.  | 
		
| 
			 47  | 
			
			 Too many redirects. When following redirects, curl hit the maximum amount.  | 
		
| 
			 48  | 
			
			 Unknown TELNET option specified.  | 
		
| 
			 49  | 
			
			 Malformed telnet option.  | 
		
| 
			 51  | 
			
			 The peer's SSL certificate or SSH MD5 fingerprint was not ok.  | 
		
| 
			 52  | 
			
			 The server didn't reply anything, which here is considered an error.  | 
		
| 
			 53  | 
			
			 SSL crypto engine not found.  | 
		
| 
			 54  | 
			
			 Cannot set SSL crypto engine as default.  | 
		
| 
			 55  | 
			
			 Failed sending network data.  | 
		
| 
			 56  | 
			
			 Failure in receiving network data.  | 
		
| 
			 58  | 
			
			 Problem with the local certificate.  | 
		
| 
			 59  | 
			
			 Couldn't use specified SSL cipher.  | 
		
| 
			 60  | 
			
			 Peer certificate cannot be authenticated with known CA certificates.  | 
		
| 
			 61  | 
			
			 Unrecognized transfer encoding.  | 
		
| 
			 62  | 
			
			 Invalid LDAP URL.  | 
		
| 
			 63  | 
			
			 Maximum file size exceeded.  | 
		
| 
			 64  | 
			
			 Requested FTP SSL level failed.  | 
		
| 
			 65  | 
			
			 Sending the data requires a rewind that failed.  | 
		
| 
			 66  | 
			
			 Failed to initialize SSL Engine.  | 
		
| 
			 67  | 
			
			 The user name, password, or similar was not accepted and curl failed to log in.  | 
		
| 
			 68  | 
			
			 File not found on TFTP server.  | 
		
| 
			 69  | 
			
			 Permission problem on TFTP server.  | 
		
| 
			 70  | 
			
			 Out of disk space on TFTP server.  | 
		
| 
			 71  | 
			
			 Illegal TFTP operation.  | 
		
| 
			 72  | 
			
			 Unknown TFTP transfer ID.  | 
		
| 
			 73  | 
			
			 File already exists (TFTP).  | 
		
| 
			 74  | 
			
			 No such user (TFTP).  | 
		
| 
			 75  | 
			
			 Character conversion failed.  | 
		
| 
			 76  | 
			
			 Character conversion functions required.  | 
		
| 
			 77  | 
			
			 Problem with reading the SSL CA cert (path? access rights?).  | 
		
| 
			 78  | 
			
			 The resource referenced in the URL does not exist.  | 
		
| 
			 79  | 
			
			 An unspecified error occurred during the SSH session.  | 
		
| 
			 80  | 
			
			 Failed to shut down the SSL connection.  | 
		
| 
			 82  | 
			
			 Could not load CRL file, missing or wrong format (added in 7.19.0).  | 
		
| 
			 83  | 
			
			 Issuer check failed (added in 7.19.0).  | 
		
| 
			 XX  | 
			
			 More error codes will appear here in future releases. The existing ones are meant to never change.  | 
		
六、用法演示:
为节省篇幅,部分操作不再贴上执行结果。
1、查看网页源码
直接在curl命令后加上网址,就可以看到网页源码。我们以网址www.sina.com为例(选择该网址,主要因为它的网页代码较短):
root@ubuntu:/home/peng# curl www.sohu.com
<html>
<head><title>307 Temporary Redirect</title></head>
<body bgcolor="white">
<center><h1>307 Temporary Redirect</h1></center>
<hr><center>nginx</center>
</body>
</html>
执行结果显示 307 Temporary Redirect,说明该网址需要重定向。
如果要把这个网页保存下来,可以使用-o参数,这就相当于使用wget命令了。
curl -o [文件名] www.sohu.com
2、自动跳转
有的网址是自动跳转的。使用-L参数,curl就会跳转到新的网址。
curl -L www.sohu.com
键入上面的命令,结果就自动跳转为www.sohu.com.cn。
3、显示头信息
-i参数可以显示http response的头信息,连同网页代码一起。
root@ubuntu:/home/peng/driver/test#  curl -i www.sohu.com
HTTP/1.1 307 Temporary Redirect
Content-Type: text/html
Content-Length: 180
Connection: keep-alive
Server: nginx
Date: Tue, 25 Aug 2020 10:10:54 GMT
Location: https://www.sohu.com/
FSS-Cache: from 9790436.18244590.10468709
FSS-Proxy: Powered by 2384755.3433341.3062915
<html>
<head><title>307 Temporary Redirect</title></head>
<body bgcolor="white">
<center><h1>307 Temporary Redirect</h1></center>
<hr><center>nginx</center>
</body>
</html>
-I参数则是只显示http response的头信息。
4、显示通信过程
-v参数可以显示一次http通信的整个过程,包括端口连接和http request头信息。
root@ubuntu:/home/peng/driver/test#  curl -v www.sohu.com
* About to connect() to www.sohu.com port 80 (#0)
*   Trying 240e:83:201:3700::5... connected
> GET / HTTP/1.1
> User-Agent: curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: www.sohu.com
> Accept: */*
> 
< HTTP/1.1 307 Temporary Redirect
< Content-Type: text/html
< Content-Length: 180
< Connection: keep-alive
< Server: nginx
< Date: Tue, 25 Aug 2020 10:11:49 GMT
< Location: https://www.sohu.com/
< FSS-Cache: from 9855973.18375663.10534247
< FSS-Proxy: Powered by 2450292.3564414.3128453
< 
<html>
<head><title>307 Temporary Redirect</title></head>
<body bgcolor="white">
<center><h1>307 Temporary Redirect</h1></center>
<hr><center>nginx</center>
</body>
</html>
* Connection #0 to host www.sohu.com left intact
* Closing connection #0
如果你觉得上面的信息还不够,那么下面的命令可以查看更详细的通信过程。
curl --trace output.txt www.sohu.com
或者
curl --trace-ascii output.txt www.sohu.com
运行后,请打开output.txt文件查看。
5、发送表单信息
发送表单信息有GET和POST两种方法。GET方法相对简单,只要把数据附在网址后面就行。
curl example.com/form.cgi?data=xxx
POST方法必须把数据和网址分开,curl就要用到--data参数。
curl -X POST --data "data=xxx" example.com/form.cgi
如果你的数据没有经过表单编码,还可以让curl为你编码,参数是--data-urlencode。
curl -X POST--data-urlencode "date=April 1" example.com/form.cgi
6、HTTP动词
curl默认的HTTP动词是GET,使用-X参数可以支持其他动词。
curl -X POST www.example.com
curl -X DELETE www.example.com
7、文件上传
假定文件上传的表单是下面这样:
<form method="POST" enctype='multipart/form-data' action="upload.cgi">
 <input type=file name=upload>
 <input type=submit name=press value="OK">
</form>
你可以用curl这样上传文件:
curl --form upload=@localfilename --form press=OK [URL]
8、Referer字段
有时你需要在http request头信息中,提供一个referer字段,表示你是从哪里跳转过来的。
curl --referer http://www.example.com http://www.example.com
9、User Agent字段
这个字段是用来表示客户端的设备信息。服务器有时会根据这个字段,针对不同设备,返回不同格式的网页,比如手机版和桌面版。
iPhone4的User Agent是
Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7
curl可以这样模拟:
curl --user-agent "[User Agent]" [URL]
10、cookie
使用--cookie参数,可以让curl发送cookie。
curl --cookie "name=xxx" www.example.com
至于具体的cookie的值,可以从http response头信息的Set-Cookie字段中得到。
-c cookie-file可以保存服务器返回的cookie到文件,-b cookie-file可以使用这个文件作为cookie信息,进行后续的请求。
curl -c cookies http://example.com
curl -b cookies http://example.com
11、增加头信息
有时需要在http request之中,自行增加一个头信息。--header参数就可以起到这个作用。
$ curl --header "Content-Type:application/json" http://example.com
12、认证
使用curl选项 -u 可以完成HTTP或者FTP的认证,可以指定密码,也可以不指定密码在后续操作中输入密码:
curl -u user:pwd http://man.linuxde.net
curl -u user http://man.linuxde.net
13、FTP
1)、列出ftp服务器上的目录列表
curl ftp://www.xxx.com/ --user name:passwd
curl ftp://www.xxx.com/ –u name:passwd #简洁写法
curl ftp://name:passwd@www.xxx.com #简洁写法2
例如:在IP地址192.168.43.117上搭建FTP服务器,并设置用户名为user,密码为123456。
现在我们要显示服务器上根目录下的所有文件信息,命令如下:
curl -u user:123456 ftp://192.168.43.117
执行结果如下:
简洁写法:
curl ftp://user:123456@192.168.43.117
执行结果如下:
2)、只列出目录,不显示进度条
curl ftp://www.xxx.com –u name:passwd -s
3)、下载一个文件:
命令如下:
curl ftp://www.xxx.com/size.zip –u name:passwd -o size.zip
示例如下:从服务器的根目录下下载文件test.c,保存到本地,本地文件名也为test.c。【注意】如果没有-o选项,程序会吧数据流定向到stdout,即直接把文件内容显示到终端上。
curl ftp://user:123456@192.168.43.117/test.c -o test.c
执行结果如下:
简洁模式
curl -u user:123456 ftp://192.168.43.117/list.h -o list.h
执行结果如下:
4)、上载一个文件:
curl –u name:passwd -T size.mp3 ftp://www.xxx.com/mp3/
举例如下:
curl -u user:123456 ftp://192.168.43.117/ -T list.h
可以看到文件并没有上传成功,返回错误码是25,参考第五章
25 FTP couldn't STOR file. The server denied the STOR operation, used for FTP uploading.
可知,是因为服务器没有赋予存储的权限,所以设置服务器的write权限即可。
5)、从服务器上删除文件(使用curl传递ftp协议的DELE命令):
curl –u name:passwd ftp://www.xxx.com/ -X 'DELE mp3/size.mp3'
6)、另外curl不支持递归下载,不过可以用数组方式下载文件,比如我们要下载1-10.gif连续命名的文件:**
curl –u name:passwd ftp://www.xxx.com/img/[1-10].gif –O #O字母大写
7)、要连续下载多个文件:
curl –u name:passwd ftp://www.xxx.com/img/[one,two,three].jpg –O #O字母大写
重要声明:本文来自一口Linux,经授权转载,版权归原作者所有,不代表锐成观点,转载的目的在于传递更多知识和信息。
相关文章推荐
2025-09-04 16:54:58
2025-09-03 16:29:06
2025-09-03 14:04:55
2025-09-02 15:54:52
2025-09-01 16:31:24
热门工具
标签选择
阅读排行
我的评论
还未登录?点击登录