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

发布于 2025-01-09 07:33:26 字数 886 浏览 1 评论 0原文

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

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.]

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

I need to download thousands of files with curl. I know how to parallelize with xargs -Pn (or gnu parallel) but I've just discovered curl itself can parallelize downloads with the argument -Z|--parallel introduced in curl-7.66 (see curl-goez-parallel) which might be cleaner or easier to share.
I need to use -o|--output option and --create-dirs. URLs need to be percent-encoded, the URL path becoming the folder path which also need to be escaped as path can contain single quotes, spaces, and usual suspects (hence -O option is not safe and -OJ option doesn't help).
If I understand well, curl command should be build like so:

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.]

This works indeed, but what's the best way to deal with thousands URLS. Can a config file used with -K config be useful? what if the -o path/to/file_x http://site/path/to/file_x is the output of another program? I haven't found any way to record commands in a file, one command per line, say.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺渁∝ 2025-01-16 07:33:26

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

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

config.txt:

url=http://site/path/to/file1
output="./path/to/file1"
url=http://site/path/to/file2
output="./path/to/file2"
...

并使用此curl命令:

$ curl --parallel --parallel-immediate --parallel-max 60 --config config.txt

该命令比这更深入,并且需要最新的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"

用于选项像 -K, --config 参见 https://curl.se/docs/manpage.html

华泰

I ended up asking on curl user mailing list.

The solution that worked for me was building a config (plain text) file built like so programmatically:

config.txt:

url=http://site/path/to/file1
output="./path/to/file1"
url=http://site/path/to/file2
output="./path/to/file2"
...

and use this curl command:

$ curl --parallel --parallel-immediate --parallel-max 60 --config config.txt

The command is a bit reacher than this and requires a recent curl version (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"

For options like -K, --config see https://curl.se/docs/manpage.html

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文