Curl非常有用,以下是遇到的一些问题和解决方案。
Windows文本文件导致Bash脚本无法Curl下载
今天从Huggingface上下载RedPajama数据集时遇到Curl报错。首先我手动下载urls.txt文件。内容是一个URL的列表,一行一个URL。随后使用该数据集提供的bash脚本分批下载数据集。内容如下:
while read line; do
dload_loc=${line#https://data.together.xyz/redpajama-data-1T/v1.0.0/}
mkdir -p $(dirname $dload_loc)
curl "$line" -O "$dload_loc"
done < urls.txt
运行以后报错:
curl: (3) URL using bad/illegal format or missing URL
搜索了很久发现是由于Windows文本文件换行符的问题。在Windows中的换行符为\n\r,多出来的\r导致curl下载报错。例如:
$ curl $'http://localhost/somepath\r'
curl: (3) URL using bad/illegal format or missing URL
具体可参考:Curl bad URL (3)
连接Proxy代理并进行NTLM身份认证
使用Curl挂代理可以使用-x参数。比如:
curl -x 127.0.0.1:8081 --head https://data.together.xyz/redpajama/arxiv/a.jsonl
运行后收到错误信息:
407 authentication required
curl: (56) Received HTTP code 407 from proxy after CONNECT
从回传的HEAD信息中发现Proxy需要身份认证:
Proxy-Connection: Keep-Alive
Proxy-Authenticate: Negotiate
Proxy-Authenticate: NTLM
Proxy-Authenticate: Basic realm="MaAfee web Gateway"
关于NTLM相关内容可以参考:NTLM Overview。这里直接跳到具体解决方案,使用“- U”参数提供用户名和密码:
curl -x 127.0.0.1:8081 -U james:password --head https://data.together.xyz/redpajama/arxiv/a.jsonl
强制取消SSL证书检查
下载时报错:
curl: (60) SSL certifiate problem: unable to get local issuer certificate
More details here: https://curl.se/docs/sslcerts.html
curl failed to erify the legitimacy of the server and therefore could not establish a secure connection to it. To learn more about this situation and how to fix it, please visit the web page mentioned above.
在URL前面加上-k参数,取消SSL证书验证。参考:curl – Is data encrypted when using the –insecure option
CURL断点续传和重连
晚上使用Curl脚本下载100个Pajama的数据集,第二天早上发现有7个文件下载失败,尝试使用Curl断点续传。使用“-C(大写) -”参数,后面的“-”不能少,默认偏移量的意思。命令行如下:
curl -k -C - https://data.together.xyz/redpajama-data-1T/v1.0.0/arxiv/arxiv_023827cd-7ee8-42e6-aa7b-661731f4c70f.jsonl
-o arxiv/a.jsonl
如果要加重试的话,加上参数“–retry 3”,意思是重试3次。具体文档可参考:使用curl断点续传下载文件
CURL断点续传报错Cannot resume
curl的“-C(大写) -”参数并不是每次都能正常工作的。例如在下载Pajama数据集的时候,就发生一下情况:
curl: (33) HTTP server doesn't seem to support byte ranges. Cannot resume.
遇到以上情况,可以尝试使用“-H”参数指定Range(该参数包含首尾,例如以下代码是下载1024bytes)然后将下载内容追加写入目标文件,例如
curl -H "Range: bytes=0-1023" http://i.imgur.com/z4d4kWk.jpg >> a.jpg #未测试
curl -H "Range: bytes=1024-2047" http://i.imgur.com/z4d4kWk.jpg >> a.jpg #未测试
CURL断点续传的另外一个样例(未测试):
## Get first 20000 bytes ##
curl -o file.png --header "Range: bytes=0-20000" http://www.cyberciti.biz/media/images/misc/static/2012/11/ifdata-welcome-0.png
## Resume at 20001 offset and download the rest of the file ##
curl -o file.png -C 20001 http://www.cyberciti.biz/media/images/misc/static/2012/11/ifdata-welcome-0.png

扫码联系船长