curl使用

简介: curl使用

@TOC


前言

使用 curl 工具


一、curl use case

常见参数项包括:

-i :显示响应头信息
-o :将请求结果写入到指定文件中
-s :静默模式,不显示额外信息
-w :指定输出内容格式

Simple Usage
Get the main page from a web-server (从Web服务器获取主页)

curl https://www.example.com/

在这里插入图片描述

Get a README file from an FTP server:(从FTP服务器获取自述文件)

curl ftp://ftp.example.com/README

Get a web page from a server using port 8000:(使用端口8000从服务器获取网页:)

curl http://www.example.com:8000/

Get a directory listing of an FTP site:

curl ftp://ftp.example.com/

Get the all terms matching curl from a dictionary:

curl dict://dict.example.com/m:curl

Get the definition of curl from a dictionary:

curl dict://dict.example.com/d:curl

Fetch two documents at once:

curl ftp://ftp.example.com/ http://www.example.com:8000/

Get a file off an FTPS server:

curl ftps://files.are.example.com/secrets.txt

or use the more appropriate FTPS way to get the same file:

curl --ftp-ssl ftp://files.are.example.com/secrets.txt

Get a file from an SSH server using SFTP:

curl -u username sftp://example.com/etc/issue

Get a file from an SSH server using SCP using a private key (not password-protected) to authenticate:

curl -u username: --key ~/.ssh/id_rsa scp://example.com/~/file.txt

Get a file from an SSH server using SCP using a private key (password-protected) to authenticate:

curl -u username: --key ~/.ssh/id_rsa --pass private_key_password
scp://example.com/~/file.txt

Get the main page from an IPv6 web server:

curl "http://[2001:1890:1112:1::20]/"

Get a file from an SMB server:

curl -u "domain\username:passwd" smb://server.example.com/share/file.txt

二、下载操作

Get a web page and store in a local file with a specific name:

curl -o thatpage.html http://www.example.com/

Get a web page and store in a local file, make the local file get the name of the remote document (if no file name part is specified in the URL, this will fail):

curl -O http://www.example.com/index.html

Fetch two files and store them with their remote names:

curl -O www.haxx.se/index.html -O curl.se/download.html

根据上面的例子,我找了一个可以下载zip文件的链接,尝试了一下,如下提示,下载失败了。
在这里插入图片描述
问下ChatGPT:

该错误通常表示curl无法验证服务器的合法性,因此无法建立安全连接。这可能是由于以下几个原因导致的:

证书验证失败:curl默认会验证服务器的SSL证书。如果服务器的证书无效、过期、自签名或与curl的信任根证书不匹配,就会导致验证失败。在这种情况下,curl会中止连接以防止潜在的安全风险。

解决方法:你可以尝试使用--insecure选项来禁用证书验证,但这会降低连接的安全性。例如:

shell
Copy
curl --insecure -O <文件链接>


请注意,在禁用证书验证时要格外小心,确保你了解与连接相关的潜在风险。

代理问题:如果你正在使用代理服务器,可能是代理服务器配置或证书问题导致验证失败。你可以尝试检查代理服务器的配置并确保证书有效。

解决方法:在curl命令中添加代理相关的选项,例如:

shell
Copy
curl -x <代理服务器地址:端口> -O <文件链接>

<代理服务器地址:端口>替换为你实际使用的代理服务器地址和端口。

网络连接问题:有时,网络连接问题可能会导致无法建立安全连接。

解决方法:确保你的网络连接正常,尝试重新运行curl命令。如果问题持续存在,可能需要排查网络问题或联系网络管理员。

我使用第一种方式:不验证证书,果然下载下来了。而且是下载到当前的工作文件夹。C:\Users\xxx\test.zip

在这里插入图片描述

如果自己想指定文件地址

curl --insecure -o Contacts/test.zip https://www.c-lodop.com/download/Lodop6.2NoteBook20230829.zip

确保 Contacts 这个文件夹存在,否则报错,并不会帮你生成该文件夹
在这里插入图片描述
在这里插入图片描述

参考链接: https://curl.se/docs/

相关文章
|
3月前
|
测试技术 网络安全 数据安全/隐私保护
curl 命令的使用一例
curl 命令的使用一例
48 0
|
12月前
curl使用
curl使用
|
Linux Windows
curl命令使用
curl命令使用
421 0
curl命令使用
|
Web App开发 JSON 运维
快来看,敖丙还不会curl
不知道大家在平时有没有用过curl,之前我是没怎么用的,最近也开始用起来了。
158 0
快来看,敖丙还不会curl
|
测试技术 Linux Python
我差一点误会了curl
记录一次curl使用遇到的问题
562 0
|
域名解析 XML 存储
Curl
Curl参数
|
数据安全/隐私保护
curl常见命令
下载文件 curl 命令正常情况下将收到的内容打印到标准输出,通过-o或者-O参数将下载内容保持 curl -o zxy.html http://www.baidu.com #将文件保存为zxy.html curl -O http://www.gnu.org/software/gettext/Manuel/gettext.html curl -O -# http://www.gnu.org/software/gettext/Manuel/gettext.html # -#表示下载时刻显示进度条。
935 0
|
Web App开发 PHP
|
Web App开发 Linux 网络安全