Linux shell编程学习笔记68: curl 命令行网络数据传输工具 选项数量雷人(上)
0 前言
在网络时代,有经常需要在网络上传输数据,时我们需要通过网络下载文件,为了满足这种时代需要,Linux提供了众多网络命令,我们今天先研究curl命令。例如,我们可以使用 curl
从 URL 下载文件,或将文件上传到服务器。
1 curl命令 的功能、格式和选项说明
我们可以使用 curl --man | more 命令查看curl命令 的帮助信息。
[purpleEndurer @ bash ~]curl --man | more_ _ ____ _Project ___| | | | _ \| |/ __| | | | |_) | || (__| |_| | _ <| |___\___|\___/|_| \_\_____|NAMEcurl - transfer a URLSYNOPSIScurl [options] [URL...]DESCRIPTIONcurl is a tool to transfer data from or to a server, using one o
f thesupported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP,IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS,TELNET and TFTP). The command is designed to work without user inter-action.curl offers a busload of useful tricks like proxy support, user authen-tication, FTP upload, HTTP post, SSL connections, cookies, file trans-fer resume, Metalink, and more. As you will see below, the number of
--More--
1.1 curl命令 的功能
curl 是 client url 或 command line url 的缩写。
curl是一个从命令行通过互联网传输数据的工具,它可以利用HTTP、HTTPS、FTP和SFTP等多种协议,从网络服务器传输数据或将数据传输到网络服务器。
除了进行文件传输,我们还可使用 curl命令
从终端发出 HTTP 请求,如 GET 和 POST。这在测试或与网络服务和 API 交互时非常有用。
利用 curl命令
,我们可以在不打开浏览器的情况下检查网页内容,因为它可以获取并显示原始数据。
1.2 curl命令的格式
除了 curl --man命令,我们也可以使用curl --help命令获取curl命令的格式和选项说明。
[purpleEndurer @ bash ~]curl --help
Usage: curl [options...] <url>
Options: (H) means HTTP/HTTPS only, (F) means FTP only--anyauth Pick "any" authentication method (H)-a, --append Append to target file when uploading (F/SFTP)--basic Use HTTP Basic Authentication (H)--cacert FILE CA certificate to verify peer against (SSL)--capath DIR CA directory to verify peer against (SSL)-E, --cert CERT[:PASSWD] Client certificate file and password (SSL)--cert-type TYPE Certificate file type (DER/PEM/ENG) (SSL)--ciphers LIST SSL ciphers to use (SSL)--compressed Request compressed response (using deflate or gzip)-K, --config FILE Specify which config file to read--connect-timeout SECONDS Maximum time allowed for connection-C, --continue-at OFFSET Resumed transfer offset-b, --cookie STRING/FILE String or file to read cookies from (H)-c, --cookie-jar FILE Write cookies to this file after operation (H)--create-dirs Create necessary local directory hierarchy--crlf Convert LF to CRLF in upload--crlfile FILE Get a CRL list in PEM format from the given file-d, --data DATA HTTP POST data (H)--data-ascii DATA HTTP POST ASCII data (H)--data-binary DATA HTTP POST binary data (H)--data-urlencode DATA HTTP POST data url encoded (H)--delegation STRING GSS-API delegation permission--digest Use HTTP Digest Authentication (H)--disable-eprt Inhibit using EPRT or LPRT (F)--disable-epsv Inhibit using EPSV (F)-D, --dump-header FILE Write the headers to this file--egd-file FILE EGD socket path for random data (SSL)--engine ENGINGE Crypto engine (SSL). "--engine list" for list-f, --fail Fail silently (no output at all) on HTTP errors (H)-F, --form CONTENT Specify HTTP multipart POST data (H)--form-string STRING Specify HTTP multipart POST data (H)--ftp-account DATA Account data string (F)--ftp-alternative-to-user COMMAND String to replace "USER [name]" (F)--ftp-create-dirs Create the remote dirs if not present (F)--ftp-method [MULTICWD/NOCWD/SINGLECWD] Control CWD usage (F)--ftp-pasv Use PASV/EPSV instead of PORT (F)-P, --ftp-port ADR Use PORT with given address instead of PASV (F)--ftp-skip-pasv-ip Skip the IP address for PASV (F)--ftp-pret Send PRET before PASV (for drftpd) (F)--ftp-ssl-ccc Send CCC after authenticating (F)--ftp-ssl-ccc-mode ACTIVE/PASSIVE Set CCC mode (F)--ftp-ssl-control Require SSL/TLS for ftp login, clear for transfer (F)-G, --get Send the -d data with a HTTP GET (H)-g, --globoff Disable URL sequences and ranges using {} and []-H, --header LINE Custom header to pass to server (H)-I, --head Show document info only-h, --help This help text--hostpubmd5 MD5 Hex encoded MD5 string of the host public key. (SSH)-0, --http1.0 Use HTTP 1.0 (H)--ignore-content-length Ignore the HTTP Content-Length header-i, --include Include protocol headers in the output (H/F)-k, --insecure Allow connections to SSL sites without certs (H)--interface INTERFACE Specify network interface/address to use-4, --ipv4 Resolve name to IPv4 address-6, --ipv6 Resolve name to IPv6 address-j, --junk-session-cookies Ignore session cookies read from file (H)--keepalive-time SECONDS Interval between keepalive probes--key KEY Private key file name (SSL/SSH)--key-type TYPE Private key file type (DER/PEM/ENG) (SSL)--krb LEVEL Enable Kerberos with specified security level (F)--libcurl FILE Dump libcurl equivalent code of this command line--limit-rate RATE Limit transfer speed to this rate-l, --list-only List only names of an FTP directory (F)--local-port RANGE Force use of these local port numbers-L, --location Follow redirects (H)--location-trusted like --location and send auth to other hosts (H)-M, --manual Display the full manual--mail-from FROM Mail from this address--mail-rcpt TO Mail to this receiver(s)--mail-auth AUTH Originator address of the original email--max-filesize BYTES Maximum file size to download (H/F)--max-redirs NUM Maximum number of redirects allowed (H)-m, --max-time SECONDS Maximum time allowed for the transfer--metalink Process given URLs as metalink XML file--negotiate Use HTTP Negotiate Authentication (H)-n, --netrc Must read .netrc for user name and password--netrc-optional Use either .netrc or URL; overrides -n--netrc-file FILE Set up the netrc filename to use-N, --no-buffer Disable buffering of the output stream--no-keepalive Disable keepalive use on the connection--no-sessionid Disable SSL session-ID reusing (SSL)--noproxy List of hosts which do not use proxy--ntlm Use HTTP NTLM authentication (H)-o, --output FILE Write output to <file> instead of stdout--pass PASS Pass phrase for the private key (SSL/SSH)--post301 Do not switch to GET after following a 301 redirect (H)--post302 Do not switch to GET after following a 302 redirect (H)--post303 Do not switch to GET after following a 303 redirect (H)-#, --progress-bar Display transfer progress as a progress bar--proto PROTOCOLS Enable/disable specified protocols--proto-redir PROTOCOLS Enable/disable specified protocols on redirect-x, --proxy [PROTOCOL://]HOST[:PORT] Use proxy on given port--proxy-anyauth Pick "any" proxy authentication method (H)--proxy-basic Use Basic authentication on the proxy (H)--proxy-digest Use Digest authentication on the proxy (H)--proxy-negotiate Use Negotiate authentication on the proxy (H)--proxy-ntlm Use NTLM authentication on the proxy (H)-U, --proxy-user USER[:PASSWORD] Proxy user and password--proxy1.0 HOST[:PORT] Use HTTP/1.0 proxy on given port-p, --proxytunnel Operate through a HTTP proxy tunnel (using CONNECT)--pubkey KEY Public key file name (SSH)-Q, --quote CMD Send command(s) to server before transfer (F/SFTP)--random-file FILE File for reading random data from (SSL)-r, --range RANGE Retrieve only the bytes within a range--raw Do HTTP "raw", without any transfer decoding (H)-e, --referer Referer URL (H)-J, --remote-header-name Use the header-provided filename (H)-O, --remote-name Write output to a file named as the remote file--remote-name-all Use the remote file name for all URLs-R, --remote-time Set the remote file's time on the local output-X, --request COMMAND Specify request command to use--resolve HOST:PORT:ADDRESS Force resolve of HOST:PORT to ADDRESS--retry NUM Retry request NUM times if transient problems occur--retry-delay SECONDS When retrying, wait this many seconds between each--retry-max-time SECONDS Retry only within this period-S, --show-error Show error. With -s, make curl show errors when they occur-s, --silent Silent mode. Don't output anything--socks4 HOST[:PORT] SOCKS4 proxy on given host + port--socks4a HOST[:PORT] SOCKS4a proxy on given host + port--socks5 HOST[:PORT] SOCKS5 proxy on given host + port--socks5-basic Enable username/password auth for SOCKS5 proxies--socks5-gssapi Enable GSS-API auth for SOCKS5 proxies--socks5-hostname HOST[:PORT] SOCKS5 proxy, pass host name to proxy--socks5-gssapi-service NAME SOCKS5 proxy service name for gssapi--socks5-gssapi-nec Compatibility with NEC SOCKS5 server-Y, --speed-limit RATE Stop transfers below speed-limit for 'speed-time' secs-y, --speed-time SECONDS Time for trig speed-limit abort. Defaults to 30--ssl Try SSL/TLS (FTP, IMAP, POP3, SMTP)--ssl-reqd Require SSL/TLS (FTP, IMAP, POP3, SMTP)-2, --sslv2 Use SSLv2 (SSL)-3, --sslv3 Use SSLv3 (SSL)--ssl-allow-beast Allow security flaw to improve interop (SSL)--stderr FILE Where to redirect stderr. - means stdout--tcp-nodelay Use the TCP_NODELAY option-t, --telnet-option OPT=VAL Set telnet option--tftp-blksize VALUE Set TFTP BLKSIZE option (must be >512)-z, --time-cond TIME Transfer based on a time condition-1, --tlsv1 Use => TLSv1 (SSL)--tlsv1.0 Use TLSv1.0 (SSL)--tlsv1.1 Use TLSv1.1 (SSL)--tlsv1.2 Use TLSv1.2 (SSL)--tlsv1.3 Use TLSv1.3 (SSL)--tls-max VERSION Use TLS up to VERSION (SSL)--trace FILE Write a debug trace to the given file--trace-ascii FILE Like --trace but without the hex output--trace-time Add time stamps to trace/verbose output--tr-encoding Request compressed transfer encoding (H)-T, --upload-file FILE Transfer FILE to destination--url URL URL to work with-B, --use-ascii Use ASCII/text transfer-u, --user USER[:PASSWORD] Server user and password--tlsuser USER TLS username--tlspassword STRING TLS password--tlsauthtype STRING TLS authentication type (default SRP)--unix-socket FILE Connect through this UNIX domain socket-A, --user-agent STRING User-Agent to send to server (H)-v, --verbose Make the operation more talkative-V, --version Show version number and quit-w, --write-out FORMAT What to output after completion--xattr Store metadata in extended file attributes-q If used as the first parameter disables .curlrc
[purpleEndurer @ bash ~]
curl命令的格式:
curl [选项] <统一资源定位符>
1.3 curl选项说明
curl的选项是目前所学的命令中最多的一个。
说明中的(H) 表示仅 HTTP/HTTPS,(F) 表示仅 FTP
选项 | 说明 |
---|---|
--anyauth | 选择“any”认证方式(H) |
-a,--append | 上传时追加到目标文件 (F/SFTP) |
--basic | 使用 HTTP 基本身份验证 (H) |
--cacert FILE | CA 证书以验证对等方 (SSL) |
--capath | 用于验证对等体的 DIR CA 目录 (SSL) |
-E,--cert CERT[:PASSWD] | 客户端证书文件和密码 (SSL) |
--cert-type TYPE | 证书文件类型 (DER/PEM/ENG) (SSL) |
--ciphers | 列出要使用的 SSL 密码 (SSL) |
--compressed | 请求压缩响应(使用 deflate 或 gzip) |
-K,--config FILE | 指定要读取的配置文件 |
--connect-timeout SECONDS | 允许的最大连接时间 |
-C,--continue-at OFFSET | 恢复传输偏移 |
-b,--cookie STRING/FILE | 从中读取 cookie 的字符串或文件 (H) |
-c,--cookie-jar FILE | 操作后将 cookie 写入指定文件 (H) |
--create-dirs | 创建必要的本地目录层次结构 |
--crlf | 在上传中将 LF 转换为 CRLF |
--crlfile FILE | 从给定文件中获取 PEM 格式的 CRL 列表 |
-d,--data DATA | HTTP POST 数据 (H) |
--data-ascii DATA | HTTP POST ASCII数据(H) |
--data-binary DATA | HTTP POST 二进制数据 (H) |
--data-urlencode DATA | HTTP POST 数据 url 编码 (H) |
--delegation STRING | GSS-API 委托权限 |
--digest | 使用 HTTP 摘要身份验证 (H) |
--disable-eprt | 使用 EPRT 或 LPRT 抑制 (F) |
--disable-epsv | 使用 EPSV 抑制 (F) |
-D,--dump-header FILE | 将头文件写入此文件 |
--egd-file FILE | 随机数据 的 EGD 套接字路径(SSL) |
--engine ENGINGE | 加密引擎 。列表为“--engine list”(SSL) |
-f,--fail | 在 HTTP 错误 上静默失败(完全没有输出)(H) |
-F,--form CONTENT | 指定 HTTP 多部分 POST 数据 (H) |
--form-string STRING | 指定 HTTP 多部分 POST 数据 (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 | 此帮助文本 |
--hostpubmd5 MD5 | 主机公钥的十六进制编码的 MD5 字符串。(SSH) |
-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 | Dump 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 | 发送邮件到此收件人 |
--mail-auth AUTH | 原始电子邮件的发件人地址 |
--max-filesize BYTES | 下载的最大文件大小 (H/F) |
--max-redirs NUM | 允许的最大重定向数 (H) |
-m,--max-time SECONDS | 允许传输的最大时间 |
--metalink | 将给定的 URL 处理为元链接 XML 文件 |
--negotiate | 使用 HTTP 协商身份验证 (H) |
-n,--netrc | 用户名和密码必须读取 .netrc |
--netrc-optional | 使用 .netrc 或 URL;覆盖 -n |
--netrc-file FILE | 设置要使用的 netrc 文件名 |
-N,--no-buffer | 禁用输出流的缓冲 |
--no-keepalive | 在连接上禁用 keepalive 使用 |
--no-sessionid | 禁用 SSL 会话 ID 重用 (SSL) |
--noproxy | 不使用代理的主机列表 |
--ntlm | 使用 HTTP NTLM 身份验证 (H) |
-o,--output FILE | 将输出写入文件而不是标准输出 |
--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 | 在代理上使用基本身份验证 (H) |
--proxy-digest | 在代理上使用摘要式身份验证 (H) |
--proxy-negotiate | 在代理上使用 Negotiate 身份验证 (H) |
--proxy-ntlm | 在代理上使用 NTLM 身份验证 (H) |
-U,--proxy-user USER[:P ASSWORD] | 代理用户和密码 |
--proxy1.0 HOST[:P ORT] | 在给定端口上使用 HTTP/1.0 代理 |
-p,--proxytunnel | 通过 HTTP 代理隧道进行操作(使用 CONNECT) |
--pubkey KEY | 公钥文件名 (SSH) |
-Q,--quote CMD | 在传输之前向服务器发送命令 (F/SFTP) |
--random-file FILE | 用于从中读取随机数据的文件 (SSL) |
-r,--range RANGE | 仅检索范围内的字节 |
--raw | 做 HTTP “raw”,不带任何传输解码 (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 | 如果出现暂时性问题,请重试请求 NUM 次 |
--retry-delay SECONDS | 重试时,在每次之间等待这指定秒数 |
--retry-max-time SECONDS | 仅在旨定时间段内重试 |
-S,--show-error | 显示错误。使用 -s,使 curl 在发生错误时显示错误 |
-s,--silent | 静音模式。不输出任何内容 |
--socks4 HOST[:PORT] | 给定主机 + 端口上的 SOCKS4 代理 |
--socks4a HOST[:PORT] | 给定主机 + 端口上的 SOCKS4a 代理 |
--socks5 HOST[:PORT] | 给定主机 + 端口上的 SOCKS5 代理 |
--socks5-basic | 为 SOCKS5 代理启用用户名/密码身份验证 |
--socks5-gssapi | 为 SOCKS5 代理启用 GSS-API 身份验证 |
--socks5-hostname HOST[:PORT] | SOCKS5代理,将主机名传递给代理 |
--socks5-gssapi-service NAME | socks5 gssapi 的代理服务名称 |
--socks5-gssapi-nec | 与 NEC SOCKS5 服务器的兼容性 |
-Y,--speed-limit RATE | 停止低于速度限制的传输,持续 'speed-time' 秒 |
-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(标准错误) 重定向到何处。- 表示 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) |
--tlsv1.3 | 使用 TLSv1.3 (SSL) |
--tls-max VERSION | 使用TLS最高版本(SSL) |
--trace FILE | 将调试跟踪写入给定文件 |
--trace-ascii FILE | 类似于 --trace,但没有十六进制输出 |
--trace-time | 为跟踪/详细输出添加时间戳 |
--tr-encoding | 请求压缩传输编码 (H) |
-T,--upload-file FILE | 将 FILE 传输到目标 |
--url URL | 要处理的 URL |
-B,--use-ascii | 使用 ASCII/文本传输 |
-u,--user USER[:PASSWORD] | 服务器用户和密码 |
--tlsuser 用户 | TLS 用户名 |
--tlspassword STRING | TLS 密码 |
--tlsauthtype STRING | TLS 身份验证类型(默认 SRP) |
--unix-socket FILE | 通过此 UNIX 域套接字连接 |
-A,--user-agent STRING | User-Agent 发送到服务器 (H) |
-v,--verbose | 使操作更详细 |
-V,--version | 显示版本号并退出 |
-w,--write-out | FORMAT 完成后输出什么 |
--xattr | 在扩展文件属性中存储元数据 |
-q | 如果用作第一个参数,则禁用 .curlrc |
2 curl命令应用实例
2.1 curl :显示获取本命令的帮助或手册信息的格式
[purpleEndurer @ bash ~] curl
curl: try 'curl --help' or 'curl --manual' for more information
[purpleEndurer @ bash ~]
我们可以尝试 使用命令 'curl --help' 或 'curl --manual' 来了解更多信息。
2.2 curl 统一资源定位符:显示指定统一资源定位符的内容(源代码)
这里的 统一资源定位符 可以是网站域名或网页地址。
2.2.1 curl g.cn : 显示g.cn的源代码
[purpleEndurer @ bash ~] curl g.cn
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://google.cn/">here</A>.
</BODY></HTML>
[purpleEndurer @ bash ~]
2.1.2 curl www.google.cn:显示 www.google.cn 的源代码。
[purpleEndurer @ bash ~] curl www.google.cn
<!DOCTYPE html>
<html lang="zh"><head><meta charset="utf-8"><title>Google</title><style>html { background: #fff; margin: 0 1em; }body { font: .8125em/1.5 arial, sans-serif; text-align: center; }h1 { font-size: 1.5em; font-weight: normal; margin: 1em 0 0; }p#footer { color: #767676; font-size: .77em; }p#footer a { background: url(//www.google.cn/intl/zh-CN_cn/images/cn_icp.gif) top right no-repeat; padding: 5px 20px 5px 0; }ul { margin: 2em; padding: 0; }li { display: inline; padding: 0 2em; }div { -moz-border-radius: 20px; -webkit-border-radius: 20px; border: 1px solid #ccc; border-radius: 20px; margin: 2em auto 1em; max-width: 650px; min-width: 544px; }div:hover, div:hover * { cursor: pointer; }div:hover { border-color: #999; }div p { margin: .5em 0 1.5em; }img { border: 0; }</style></head><body><div><a href="https://www.google.com.hk/webhp?hl=zh-CN&sourceid=cnhp"><img src="//www.google.cn/intl/zh-CN_cn/landing/cnexp/google-search.png" alt="Google" width="586" height="257"></a><h1><a href="https://www.google.com.hk/webhp?hl=zh-CN&sourceid=cnhp"><strong id="target">google.com.hk</strong></a></h1><p>请收藏我们的网址</div><p id="footer"><span>ICP证合字B2-20070004号</span></p></body>
</html>
[purpleEndurer @ bash ~]
2.3 将统一定位符资源保存到文件
2.3.1 方法1:curl 统一定位符 > 文件名
第1种方法是使用输出重定向功能,将curl命令的输出重定向到指定的文件。
[purpleEndurer @ bash ~] curl g.cn > g.htm% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed
100 215 100 215 0 0 1934 0 --:--:-- --:--:-- --:--:-- 1919
[purpleEndurer @ bash ~] cat g.htm
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://google.cn/">here</A>.
</BODY></HTML>
[purpleEndurer @ bash ~]
在这个实例中,我们先用命令 curl g.cn > g.htm 将g.cn的源代码重定向到g.htm,这时curl命令在标准输出(stdout)显示的就不再像例2.2.1那样显示g.cn的源代码,而是
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 215 100 215 0 0 1934 0 --:--:-- --:--:-- --:--:-- 1919
然后我们使用 cat g.htm命令查看 g.htm的内容
接着我们可以使用 命令 du -h g.htm 查看g.htm的文件大小
[purpleEndurer @ bash ~] du -h g.htm
4.0K g.htm
[purpleEndurer @ bash ~]
2.3.2 方法2 使用-o选项:curl -o 文件名 统一资源定位符
[purpleEndurer @ bash ~] curl -o g.html g.cn% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed
100 215 100 215 0 0 1205 0 --:--:-- --:--:-- --:--:-- 1201
[purpleEndurer @ bash ~] du -l g.html
4 g.html
[purpleEndurer @ bash ~] cat g.html
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://google.cn/">here</A>.
</BODY></HTML>
[purpleEndurer @ bash ~]
我们首先使用 curl -o g.html g.cn命令将g.cn的内容保存到g.html,然后分别使用du命令查看g.html的文件大小,用cat命令查看g.html的文件内容。
2.3.3 方法3 使用-O选项:curl -O 统一资源定位符
[purpleEndurer @ bash ~] curl -O g.cn
curl: Remote file name has no length!
curl: try 'curl --help' or 'curl --manual' for more information
[purpleEndurer @ bash ~]
居然没有保存下来,原因是我们在这里只指定了g.cn这个域名,而没有指定具体的文件名,所以curl提示远程文件名没有长度。
当我们使用命令curl -O www.g.cn/index.htm,指定了index.htm这个具体的文件名,资源就可以保存下来了:
[purpleEndurer @ bash ~] curl -O www.g.cn/index.htm% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed
100 1570 100 1570 0 0 23439 0 --:--:-- --:--:-- --:--:-- 23787
[purpleEndurer @ bash ~] ls
Code index.htm
[purpleEndurer @ bash ~] cat index.htm
<!DOCTYPE html>
<html lang=en><meta charset=utf-8><meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width"><title>Error 404 (Not Found)!!1</title><style>*{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}</style><a href=//www.google.com/><span id=logo aria-label=Google></span></a><p><b>404.</b> <ins>That’s an error.</ins><p>The requested URL <code>/index.htm</code> was not found on this server. <ins>That’s all we know.</ins>
[purpleEndurer @ bash ~] du -l index.htm
4 index.htm
[purpleEndurer @ bash ~]
然后我们可以使用ls、cat和du等命令查看文件index.htm的内容、长度等信息。
2.3.4 使用xarg命令下载多个URL资源:xargs -n 1 curl -O < URL资源列表文件
例如,有如下博文资源:
科学设计程序员面试内容,破解“八股文”之弊 的url是:
https://blog.csdn.net/Purpleendurer/article/details/140781410?spm=1001.2014.3001.5501生成式 AI 的发展方向,是 Chat 还是 Agent? 的url是:
https://blog.csdn.net/Purpleendurer/article/details/140731562?spm=1001.2014.3001.5501“微软蓝屏”事件的警示:内外双修 加强测试 的url是:
https://blog.csdn.net/Purpleendurer/article/details/140678899?spm=1001.2014.3001.5501
我们可以把以上3篇博文内容全部保存到本地文件。
2.3.4.1 步骤1:把以上资源的url保存到myurl.txt中。
这里我们使用输出重定向功能创建myurl.txt。
[purpleEndurer @ bash ~] echo "https://blog.csdn.net/Purpleendurer/article/details/140781410?spm=1001.2014.3001.5501" > myurl.txt
[purpleEndurer @ bash ~] echo "https://blog.csdn.net/Purpleendurer/article/details/140731562?spm=1001.2014.3001.5501" >> myurl.txt
[purpleEndurer @ bash ~] echo "https://blog.csdn.net/Purpleendurer/article/details/140678899?spm=1001.2014.3001.5501" >> myurl.txt
[purpleEndurer @ bash ~] cat myurl.txt
https://blog.csdn.net/Purpleendurer/article/details/140781410?spm=1001.2014.3001.5501
https://blog.csdn.net/Purpleendurer/article/details/140731562?spm=1001.2014.3001.5501
https://blog.csdn.net/Purpleendurer/article/details/140678899?spm=1001.2014.3001.5501
[purpleEndurer @ bash ~]
2.3.4.2 步骤2:使用命令 xargs -n 1 curl -O < myurl.txt
[purpleEndurer @ bash ~] xargs -n 1 curl -O < myurl.txt% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed
100 211k 0 211k 0 0 267k 0 --:--:-- --:--:-- --:--:-- 267k% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed
100 225k 0 225k 0 0 262k 0 --:--:-- --:--:-- --:--:-- 262k% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed
100 207k 0 207k 0 0 190k 0 --:--:-- 0:00:01 --:--:-- 191k
[purpleEndurer @ bash ~]
我们用ls命令查看当前目录下的内容,
[purpleEndurer @ bash ~] ls
140678899?spm=1001.2014.3001.5501 140731562?spm=1001.2014.3001.5501 140781410?spm=1001.2014.3001.5501 Code myurl.txt
可以看到新保存下的来的3个文件:
- 140678899?spm=1001.2014.3001.5501
- 140731562?spm=1001.2014.3001.5501
- 140781410?spm=1001.2014.3001.5501
我们可以用cat <文件名> | more 命令查看文件的内容,例如使用命令 cat 140678899?spm=1001.2014.3001.5501 | more 查看文件 140678899?spm=1001.2014.3001.5501 的内容:
[PurpleEndurer @ bash ~] cat 140678899?spm=1001.2014.3001.5501 | more<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><link rel="canonical" href="https://blog.csdn.net/Purpleendurer/article/details/140678899"/><meta http-equiv="content-type" content="text/html; charset=utf-8"><meta name="renderer" content="webkit"/><meta name="force-rendering" content="webkit"/><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"><meta name="report" content='{"pid": "blog", "spm":"1001.2101"}'><meta name="referrer" content="always"><meta http-equiv="Cache-Control" content="no-siteapp" /><link rel="alternate" media="handheld" href="#" /><meta name="shenma-site-verification" content="5a59773ab8077d4a62bf469ab966a63b_1497598848"><meta name="applicable-device" content="pc"><link href="https://g.csdnimg.cn/static/logo/favicon32.ico" rel="shortcut icon" type="image/x-icon" /><title>“微软蓝屏”事件的警示:内外双修 加强测试_719蓝屏 反思-CSDN博客</title><script>(function(){ var el = document.createElement("script"); el.src = "https://s3a.pstatp.com/toutiao/push.js?1abfa13dfe74d72d41d83c86d240de427e7cac50c51ead53b2e79d40c7952a23ed7716d05b4
a0f683a653eab3e214672511de2457e74e99286eb2c33f4428830"; el.id = "ttzz";
--More--
,
这个文件是博文《“微软蓝屏”事件的警示:内外双修 加强测试》的内容。
2.3.5 利用序列 下载多个URL资源
例如,假设在http://qzone.qq.com.com/pic 下存在如下5个文件:
- qzone1.JPG
- qzone2.JPG
- qzone3.JPG
- qzone4.JPG
- qzone5.JPG
那么我们可以使用序列 qzone[1-5].JPG 来表示这5个文件。
也就可以使用命令 curl -O http://qzone.qq.com.com/pic/qzone[1-5].JPG 将它们来保存到本地。
2.4 保存cookie到文件:curl -c 文件名 网站
例如,我们想保存http://qzone.qq.com.com的cookie,那么可以用命令 curl -c qzone-cookie.txt http://qzone.qq.com.com 将cookie保存到文件 qzone-cookie.txt 中
[PurpleEndurer @ bash ~] curl -c qzone-cookie.txt http://qzone.qq.com.com
[PurpleEndurer @ bash ~] cat qzone-cookie.txt
# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.qzone.qq.com.com FALSE / FALSE 0 PHPSESSID r0jv2c60s8bava08eut7mrg017
qzone.qq.com.com FALSE / FALSE 0 XYZSRV wtc2-a-2
[PurpleEndurer @ bash ~]
然后我们使用命令 cat qzone-cookie.txt 查看保存到文件 qzone-cookie.txt 中的cookie。
2.5 保存header信息到文件:curl -D 文件 网址
例如,我们想保存http://qzone.qq.com.com的hearder,那么可以用命令 curl -D header.txt http://qzone.qq.com.com 将hearder 保存到文件 header.txt 中
[PurpleEndurer @ bash ~] curl -D header.txt http://qzone.qq.com.com
[PurpleEndurer @ bash ~] cat header.txt
HTTP/1.1 302 Moved Temporarily
Date: Sun, 04 Aug 2024 13:36:17 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
set-cookie: PHPSESSID=ehhc7f1d5a6d2sush8168b0is4; path=/
set-cookie: XYZSRV=wtc2-a-1; path=/
expires: Thu, 19 Nov 1981 08:52:00 GMT
cache-control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
pragma: no-cache
location: https://gen.xyz/
CF-Cache-Status: DYNAMIC
Report-To: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v4?s=4FADjXtpF77uXFkcLQM3%2BA1kVadxIq6E5lzX6mYBbCkZgaNcMGk6HGFQ3fw89mKmzYa3%2Bdu0U9SF3%2BNGRQj%2FR4rAJj8dgCr7PX8R0BkmoVHGCL9TfK7rSXF4Zl2agF%2FSDMY%3D"}],"group":"cf-nel","max_age":604800}
NEL: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
Server: cloudflare
CF-RAY: 8adeef7a2e432018-IAD[PurpleEndurer @ bash ~]
然后我们使用命令 cat header.txt 查看保存到文件 header.txt 中的header信息。
相关文章:

Linux shell编程学习笔记68: curl 命令行网络数据传输工具 选项数量雷人(上)
0 前言 在网络时代,有经常需要在网络上传输数据,时我们需要通过网络下载文件,为了满足这种时代需要,Linux提供了众多网络命令,我们今天先研究curl命令。例如,我们可以使用 curl 从 URL 下载文件࿰…...
马尔科夫决策过程
马尔科夫决策过程 贝尔曼方程 贝尔曼方程(Bellman Equation)是动态规划中的一个核心概念,用于解决最优决策问题。贝尔曼方程通过递归的方式,将问题分解为子问题,从而使得最优策略的求解变得可行。贝尔曼方程广泛应用…...
未知攻焉知防:从攻击者视角看网络安全的“攻守之道”
自首届网络安全攻防实战演练开展以来,这一活动已成为网络安全领域备受关注的大事件。今年,攻防实战演练更上升到了一个全新高度,包括行动任务数量、演练周期时长、攻击强度以及演练类别等,较以往都有极大提升,堪称“史…...

数字孪生赋能智慧城市大脑智建设方案(可编辑65页PPT)
引言:随着科技的飞速发展,智慧城市的建设已成为全球城市发展的新趋势。数字孪生技术作为其中的关键技术之一,正逐步赋能智慧城市大脑的建设,推动城市治理从数字化向智能化、智慧化转型升级。本方案旨在简要介绍数字孪生赋能智慧城…...

c++----内存管理
okk,大家好。我们大家学习了鄙人的前面前面几篇博客,并且还稍微使用了一些c的基础知识。并且我们前面都说过,我们前面学习的知识都说过。我们前面的几篇博客都是我们以后使用c基础。但是我们大家都知道现在代码都关注什么时间啊,内…...

C++——哈希结构
1.unordered系列关联式容器 本节主要介绍unordered_map和unordered_set两个容器,底层使用哈希实现的 unordered_map 1.unordered_map是储存<key,value>键值对的关联式容器,其允许通过key快速查找到对应的value,和map非常相似&#x…...

智能小程序 Ray 开发面板 SDK —— 无线开关一键执行模板教程(一)
1. 准备工作 前提条件 已阅读 Ray 新手村任务,了解 Ray 框架的基础知识已阅读 使用 Ray 开发万能面板,了解 Ray 面板开发的基础知识 构建内容 在此 Codelab 中,您将利用面板小程序开发构建出一个支持一键执行及自动化的无线开关面板&…...
rockDB(1)
文章目录 概述编译rocksdb压缩库 基本接口 小结 概述 RocksDB 是 Facebook 的一个实验项目,目的是希望能开发一套能在服务器压力下,真正发挥高 速存储硬件性能的高效数据库系统。这是一个C库,允许存储任意长度二进制 KV 数据。支持原 子读写…...
[element-ui] 自动获取el-input的焦点
<el-input v-model"filterPlanName" ref"autoFocus" ></el-input>this.$nextTick((_) > {this.$refs.autoFocus.focus(); })参考: [element-ui]自动获取el-input的焦点...
智能闹钟的睡眠评估算法是如何工作的呢
智能闹钟的睡眠评估算法是智能闹钟功能的核心部分,它主要通过以下几个步骤来工作: 一、数据收集 传感器数据:智能闹钟内置多种传感器,如心率传感器、呼吸传感器、体动传感器以及环境传感器(如温度、湿度、光线传感器…...

Vue + View-ui-plus Upload实现手动上传
本文实现Vue Upload组件多文件手动上传,支持上传图片(image)、压缩文件(zip/rar)、表格(excel)、pdf 一、dom结构 <Row><Col :span"19"></Col><Col :span"2"><div class"ivu-btn-uplo…...

Radxa ROCK 3C开发板编译Opencv,支持调用树莓派摄像头模块V2
目录 1、ROCK 3C和树莓派摄像头模块V2介绍2、ROCK 3C在rsetup开启支持3、测试指令4、编译Opencv4.1 增加swap,确保内存够用4.2 安装依赖和下载opencv4.3 编译参考链接 5、使用opencv调用树莓派摄像头模块V2 1、ROCK 3C和树莓派摄像头模块V2介绍 ROCK 3C 是一款基于…...

Spring02
文章目录 1. IOC/DI注解开发2. IOC/DI注解开发管理第三方bean3. Spring整合4. AOP简介5. AOP入门案例6. AOP工作流程7. AOP配置管理8. AOP事务管理 1. IOC/DI注解开发 注解开发定义bean用的是2.5版提供的注解,纯注解开发用的是3.0版提供的注解 pom.xml添加依赖 &l…...

Linux系统中的高级内核模块调试技术
引言 在Linux系统中进行高级内核模块开发时,调试是不可或缺的重要环节。调试技术能够帮助开发人员发现和解决代码中的错误和问题,提高开发效率和代码质量。本文将深入探讨Linux系统中高级内核模块调试的技术和方法,包括常用的调试工具、调试…...

竞赛报名管理系统asp.net+sqlserver
竞赛报名管理系统 功能简单 内容单调 适合学习 asp.net 三层架构 sqlserver2022数据库 账号登陆注册 用户管理 克赛管理 竞赛报名 竞赛评分 公告维护 修改密码 新增竞赛 2019数据库版本低 附加不了 需要高版本数据库 说明文档 运行前附加数据库.mdf(或sql生成数据…...
Python爬虫核心面试题2
网络爬虫 1. 什么是HTTP协议?它有哪些常见的请求方法?2. 在进行网络爬虫时,如何判断一个网站是否允许被爬取?3. 在使用HTTP请求时,如何处理重定向?4. 解释HTTP状态码200、404、500的含义。5. 什么是Session…...

【2024年华数杯全国大学生数学建模竞赛】C题:老外游中国 问题思路分析及Python代码实现
【2024 年华数杯全国大学生数学建模竞赛】C题:老外游中国 问题思路分析及Python代码实现 1 题目 最近,“city 不 city”这一网络流行语在外国网红的推动下备受关注。随着我国过境免签政策的落实,越来越多外国游客来到中国,通过网…...
HTTP/2:让网络飞起来
文章目录 一、HTTP/2 的基本概念和背景二、HTTP/2 的主要特性和优势2.1 二进制帧2.2 多路复用2.3 头部压缩2.4 服务器推送 三、HTTP/2 的实现和部署四、HTTP/2 与现有技术的比较五、HTTP/2 与 Web 性能优化六、结束语:让 HTTP/2 助力你的 Web 开发 今天我们来聊聊一…...
C++ primer plus 第17 章 输入、输出和文件:刷新输出缓冲区
C primer plus 第17 章 输入、输出和文件:刷新输出缓冲区 C primer plus 第17 章 输入、输出和文件:刷新输出缓冲区 文章目录 C primer plus 第17 章 输入、输出和文件:刷新输出缓冲区17.2.3刷新输出缓冲区 17.2.3刷新输出缓冲区 如果程序使…...
项目总结2
文件的分片上传 格外功能是:秒传,断点续传。 今天最惨,上午找bug,下午一直在修改,晚上脑子what了,混乱的很,数据表之间的逻辑不清晰,导致我传值,还有操作数据库一直有问…...
在软件开发中正确使用MySQL日期时间类型的深度解析
在日常软件开发场景中,时间信息的存储是底层且核心的需求。从金融交易的精确记账时间、用户操作的行为日志,到供应链系统的物流节点时间戳,时间数据的准确性直接决定业务逻辑的可靠性。MySQL作为主流关系型数据库,其日期时间类型的…...

srs linux
下载编译运行 git clone https:///ossrs/srs.git ./configure --h265on make 编译完成后即可启动SRS # 启动 ./objs/srs -c conf/srs.conf # 查看日志 tail -n 30 -f ./objs/srs.log 开放端口 默认RTMP接收推流端口是1935,SRS管理页面端口是8080,可…...
反射获取方法和属性
Java反射获取方法 在Java中,反射(Reflection)是一种强大的机制,允许程序在运行时访问和操作类的内部属性和方法。通过反射,可以动态地创建对象、调用方法、改变属性值,这在很多Java框架中如Spring和Hiberna…...

HashMap中的put方法执行流程(流程图)
1 put操作整体流程 HashMap 的 put 操作是其最核心的功能之一。在 JDK 1.8 及以后版本中,其主要逻辑封装在 putVal 这个内部方法中。整个过程大致如下: 初始判断与哈希计算: 首先,putVal 方法会检查当前的 table(也就…...
动态 Web 开发技术入门篇
一、HTTP 协议核心 1.1 HTTP 基础 协议全称 :HyperText Transfer Protocol(超文本传输协议) 默认端口 :HTTP 使用 80 端口,HTTPS 使用 443 端口。 请求方法 : GET :用于获取资源,…...

push [特殊字符] present
push 🆚 present 前言present和dismiss特点代码演示 push和pop特点代码演示 前言 在 iOS 开发中,push 和 present 是两种不同的视图控制器切换方式,它们有着显著的区别。 present和dismiss 特点 在当前控制器上方新建视图层级需要手动调用…...
JS手写代码篇----使用Promise封装AJAX请求
15、使用Promise封装AJAX请求 promise就有reject和resolve了,就不必写成功和失败的回调函数了 const BASEURL ./手写ajax/test.jsonfunction promiseAjax() {return new Promise((resolve, reject) > {const xhr new XMLHttpRequest();xhr.open("get&quo…...

[大语言模型]在个人电脑上部署ollama 并进行管理,最后配置AI程序开发助手.
ollama官网: 下载 https://ollama.com/ 安装 查看可以使用的模型 https://ollama.com/search 例如 https://ollama.com/library/deepseek-r1/tags # deepseek-r1:7bollama pull deepseek-r1:7b改token数量为409622 16384 ollama命令说明 ollama serve #:…...

Ubuntu系统复制(U盘-电脑硬盘)
所需环境 电脑自带硬盘:1块 (1T) U盘1:Ubuntu系统引导盘(用于“U盘2”复制到“电脑自带硬盘”) U盘2:Ubuntu系统盘(1T,用于被复制) !!!建议“电脑…...

水泥厂自动化升级利器:Devicenet转Modbus rtu协议转换网关
在水泥厂的生产流程中,工业自动化网关起着至关重要的作用,尤其是JH-DVN-RTU疆鸿智能Devicenet转Modbus rtu协议转换网关,为水泥厂实现高效生产与精准控制提供了有力支持。 水泥厂设备众多,其中不少设备采用Devicenet协议。Devicen…...