为什么对于特定的 REST URL,wget 可以工作而curl 会失败?

ter*_*don 11 wget curl

在回答另一个网站上的问题时,我在尝试从此 URL 获取时遇到了奇怪的差异:curlhttps : //www.uniprot.org/uniprot/A2Z669.fastawget

\n

由于某种原因,curl只是默默地无法下载,而wget正确地获取文件A2Z669.fasta

\n
$ ls -la\ntotal 300\ndrwxr-xr-x   2 terdon terdon 266240 Dec 11 12:22 .\ndrwxr-xr-x 202 terdon terdon  32768 Dec 10 17:31 ..\n\n$ curl https://www.uniprot.org/uniprot/A2Z669.fasta\n$ ls -la\ntotal 300\ndrwxr-xr-x   2 terdon terdon 266240 Dec 11 12:22 .\ndrwxr-xr-x 202 terdon terdon  32768 Dec 10 17:31 ..\n
Run Code Online (Sandbox Code Playgroud)\n

显式设置输出文件没有帮助,只会创建一个空文件:

\n
$ curl -o file "https://www.uniprot.org/uniprot/A2Z669.fasta"\n  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n                                 Dload  Upload   Total   Spent    Left  Speed\n  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\n\n$ ls -la\ntotal 300\ndrwxr-xr-x   2 terdon terdon 266240 Dec 11 12:25 .\ndrwxr-xr-x 202 terdon terdon  32768 Dec 10 17:31 ..\n-rw-r--r--   1 terdon terdon      0 Dec 11 12:25 file\n$ cat file\n$ \n
Run Code Online (Sandbox Code Playgroud)\n

然而,wget工作得很好:

\n
$ wget https://www.uniprot.org/uniprot/A2Z669.fasta\n--2023-12-11 12:24:42--  https://www.uniprot.org/uniprot/A2Z669.fasta\nLoaded CA certificate \'/etc/ssl/certs/ca-certificates.crt\'\nResolving www.uniprot.org (www.uniprot.org)... 193.62.193.81\nConnecting to www.uniprot.org (www.uniprot.org)|193.62.193.81|:443... connected.\nHTTP request sent, awaiting response... 301 Moved Permanently\nLocation: https://rest.uniprot.org/uniprot/A2Z669.fasta [following]\n--2023-12-11 12:24:42--  https://rest.uniprot.org/uniprot/A2Z669.fasta\nResolving rest.uniprot.org (rest.uniprot.org)... 193.62.193.81\nConnecting to rest.uniprot.org (rest.uniprot.org)|193.62.193.81|:443... connected.\nHTTP request sent, awaiting response... 301 Moved Permanently\nLocation: https://rest.uniprot.org/uniprotkb/A2Z669.fasta [following]\n--2023-12-11 12:24:43--  https://rest.uniprot.org/uniprotkb/A2Z669.fasta\nReusing existing connection to rest.uniprot.org:443.\nHTTP request sent, awaiting response... 200 OK\nLength: unspecified [text/plain]\nSaving to: \xe2\x80\x98A2Z669.fasta\xe2\x80\x99\n\nA2Z669.fasta            [ <=>               ]     314  --.-KB/s    in 0s      \n\n2023-12-11 12:24:43 (6.65 MB/s) - \xe2\x80\x98A2Z669.fasta\xe2\x80\x99 saved [314]\n\n$ ls -la\ntotal 304\ndrwxr-xr-x   2 terdon terdon 266240 Dec 11 12:24 .\ndrwxr-xr-x 202 terdon terdon  32768 Dec 10 17:31 ..\n-rw-r--r--   1 terdon terdon    314 Dec 11 12:24 A2Z669.fasta\n
Run Code Online (Sandbox Code Playgroud)\n

它似乎也不特定于该特定文件。我尝试了来自同一 REST API ( https://www.uniprot.org/uniprot/P05067.fasta ) 的另一个 URL,并得到了相同的行为。

\n

我在 Arch 系统上运行它:

\n
$ wget --version | head -n1\nGNU Wget 1.21.4 built on linux-gnu.\n\n$ curl --version | head -n1\ncurl 8.4.0 (x86_64-pc-linux-gnu) libcurl/8.4.0 OpenSSL/3.1.4 zlib/1.3 brotli/1.1.0 zstd/1.5.5 libidn2/2.3.4 libpsl/0.21.2 (+libidn2/2.3.4) libssh2/1.11.0 nghttp2/1.58.0\n
Run Code Online (Sandbox Code Playgroud)\n

这里发生了什么?wget当失败时什么会起作用curl

\n

Ste*_*itt 26

wget默认情况下遵循重定向,curl不\​​xe2\x80\x99t。如果添加-L,则curl效果很好:

\n
curl -OL https://www.uniprot.org/uniprot/A2Z669.fasta\n
Run Code Online (Sandbox Code Playgroud)\n

-O告诉curl输出到文件而不是标准输出,匹配wget\xe2\x80\x99s 默认行为。)

\n

  • 在我看来,我将 `wget` 归类为“命令行浏览器”,将 `curl` 归类为“HTTP 的 shell API”。我发现,以这种方式思考可以帮助我理解为什么选择某些默认值。例如,“wget”默认下载到名称与 URI 匹配的文件,而“curl”则流式传输到标准输出。 (13认同)
  • 不是白痴;-)。`curl` 对重定向的处理很容易被忘记。 (9认同)
  • 很容易忘记,因为这是一个令人讨厌的默认设置。需要一个在重定向处停止的选项会更有意义。 (4认同)
  • @JörgWMittag我倾向于沿着类似的思路思考——“wget”作为命令行下载工具(具有方便的“镜像”功能),而“curl”作为命令行HTTP工具。 (2认同)