如何有效地使用curl -Z(--parallel)?

jgr*_*ran 2 curl command-line-interface

我需要使用curl下载数千个文件。我知道如何与xargs -Pn(或) 并行化,但我刚刚发现curl本身可以使用curl-7.66中引入的gnu parallel参数并行化下载(请参阅curl-goez-parallel),这可能更干净或更容易共享。我需要使用选项 和。URL 需要进行百分比编码,URL 路径成为文件夹路径,也需要转义,因为路径可以包含单引号、空格和常见的可疑内容(因此不安全并且没有帮助)。如果我理解得很好,curl 命令应该像这样构建:-Z|--parallel-o|--output--create-dirs-O option-OJ option

curl -Z -o path/to/file1 http://site/path/to/file1 -o path/to/file2 http://site/path/to/file2 [-o path/to/file3 http://site/path/to/file3, etc.]
Run Code Online (Sandbox Code Playgroud)

这确实有效,但是处理数千个 URL 的最佳方法是什么?config与 一起使用的文件-K config有用吗?如果-o path/to/file_x http://site/path/to/file_x是另一个程序的输出怎么办?我还没有找到任何方法在文件中记录命令,例如每行一个命令。

jgr*_*ran 6

我最终在curl用户邮件列表上询问。

对我有用的解决方案是构建一个配置(纯文本)文件,以编程方式构建:

配置.txt:

url=http://site/path/to/file1
output="./path/to/file1"
url=http://site/path/to/file2
output="./path/to/file2"
...
Run Code Online (Sandbox Code Playgroud)

并使用这个curl命令:

$ curl --parallel --parallel-immediate --parallel-max 60 --config config.txt
Run Code Online (Sandbox Code Playgroud)

该命令比这个更复杂,并且需要最新的curl版本(7.81+)

$ curl --parallel --parallel-immediate --parallel-max 60 --config config.txt \
  --fail-with-body --retry 5 --create-dirs \
  --write-out "code %{response_code} url %{url} type %{content_type}\n"
Run Code Online (Sandbox Code Playgroud)

有关 -K、--config 等选项,请参阅https://curl.se/docs/manpage.html

华泰