网站黄色在线观看视频,男人和女人操逼有免费的视频吗2018高清,91熟女丨老女人丨高潮丰满,丝袜高潮流白浆潮喷在线播放

中國站

中國站

國際版

聯系我們

400-002-9968

售前咨詢

售后服務

注冊 登錄

博客 > 一篇文了解Linux命令cURL

一篇文了解Linux命令cURL

  • 標簽:
  • cURL
  • Linux命令
  • cURL命令

瀏覽量:2588次評論:0次

作者:銳成網絡整理時間:2024-05-21 13:49:45

cURL是一款在Linux系統中廣泛使用的命令行工具,它能夠通過各種協議進行數據傳輸和交互。無論在開發(fā)、測試還是日常使用中,cURL都展現出其強大的功能和靈活性。本文將為您詳細介紹Linux命令cURL,包括cURL概念、curl命令參數詳解、cURL命令退出碼以及用法演示,以供參考。

cURL概念

cURL 是常用的命令行工具,用來請求 Web 服務器。它的名字就是客戶端(client)的 URL 工具的意思。

cURL 的原作者是 Daniel Stenberg (目前是 cURL 的核心開發(fā)者),同時也是 IETF HTTPbis 工作組的資深成員。Daniel 在 1998 年創(chuàng)建了 curl 項目,他編寫了最初的 curl 版本,并創(chuàng)建了 libcurl 庫。到目前為止,代碼倉庫包括的 24000 次 commit 有超過一半是 Daniel 本人提交的,他依然是項目的核心開發(fā)者。Daniel 表示已將 curl 視為自己的孩子。

作為一款強力工具,支持的協議包括 (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET and TFTP),還支持POST、cookies、認證、從指定偏移處下載部分文件等功能,具有用戶代理字符串、限速、文件大小、進度條、cookie支持、用戶認證、斷點續(xù)傳等特征。

一、命令的安裝

sudo apt-get install curl

二、cURL命令語法

curl [options] [URL...]

三、URL格式

在WWW上,每一信息資源都有統一的且在網上唯一的地址,該地址就叫URL(Uniform Resource Locator,統一資源定位器),它是WWW的統一資源定位標志,就是指網絡地址。URL的格式定義要參考 RFC 1808 。

地址:http://www.w3.org/Addressing/rfc1808.txt
《Relative Uniform Resource Locators 》 

URL由三部分組成:資源類型、存放資源的主機域名、資源文件名。也可認為由4部分組成:協議、主機、端口、路徑URL的一般語法格式為:

protocol :// hostname[:port] / path / [;parameters][?query]#fragment

(帶方括號[]的為可選項)。

protocol(協議)

指定使用的傳輸協議,下表列出 protocol 屬性的有效方案名稱。最常用的是HTTP協議,它也是WWW中應用最廣的協議。

file 資源是本地計算機上的文件。格式file:///,注意后邊應是三個斜杠。

ftp 通過 FTP訪問資源。格式 FTP://

gopher 通過 Gopher 協議訪問該資源。

http 通過 HTTP 訪問該資源。格式 HTTP://

https 通過安全的 HTTPS 訪問該資源。格式 HTTPS://

mailto 資源為電子郵件地址,通過 SMTP 訪問。格式 mailto:

MMS 通過 支持MMS(流媒體)協議的播放該資源。(代表軟件:Windows Media Player)格式 MMS://

ed2k 通過 支持ed2k(專用下載鏈接)協議的P2P軟件訪問該資源。(代表軟件:電驢) 格式 ed2k://

Flashget 通過 支持Flashget:(專用下載鏈接)協議的P2P軟件訪問該資源。(代表軟件:快車) 格式 Flashget://

thunder 通過 支持thunder(專用下載鏈接)協議的P2P軟件訪問該資源。(代表軟件:迅雷) 格式 thunder://

news 通過 NNTP 訪問該資源。

hostname(主機名)

是指存放資源的服務器的域名系統(DNS) 主機名或 IP 地址。有時,在主機名前也可以包含連接到服務器所需的用戶名和密碼(格式:username:password@hostname)。

port(端口號)

整數,可選,省略時使用方案的默認端口,各種傳輸協議都有默認的端口號,如http的默認端口為80。如果輸入時省略,則使用默認端口號。有時候出于安全或其他考慮,可以在服務器上對端口進行重定義,即采用非標準端口號,此時,URL中就不能省略端口號這一項。

path(路徑)

由零或多個“/”符號隔開的字符串,一般用來表示主機上的一個目錄或文件地址。

parameters(參數)

這是用于指定特殊參數的可選項。

query(查詢)

可選,用于給動態(tài)網頁(如使用CGI、ISAPI、PHP/JSP/ASP/ASP.NET等技術制作的網頁)傳遞參數,可有多個參數,用“&”符號隔開,每個參數的名和值用“=”符號隔開。

fragment(信息片斷)

字符串,用于指定網絡資源中的片斷。例如一個網頁中有多個名詞解釋,可使用fragment直接定位到某一名詞解釋。

四、curl命令參數詳解

由于linux curl功能十分強大,所以命令參數十分多,下表只篩選出來部分常用的參數,更多參數請運行“man curl”命令查看。

在以下選項中,(H) 表示僅適用 HTTP/HTTPS ,(F) 表示僅適用于 FTP

參數1

參數2

說明

 

--anyauth

選擇 "any" 認證方法 (H)

-a,

--append

添加要上傳的文件 (F/SFTP)

 

--basic

使用HTTP基礎認證(Basic Authentication)(H)

 

--cacert FILE

CA 證書,用于每次請求認證 (SSL)

 

--capath DIR

CA 證書目錄 (SSL)

-E,

--cert CERT[:PASSWD]

客戶端證書文件及密碼 (SSL)

 

--cert-type TYPE

證書文件類型 (DER/PEM/ENG) (SSL)

 

--ciphers LIST

SSL 秘鑰 (SSL)

 

--compressed

請求壓縮 (使用 deflate 或 gzip)

-K,

--config FILE

指定配置文件

 

--connect-timeout SECONDS

連接超時設置

-C,

--continue-at OFFSET

斷點續(xù)轉

-b,

--cookie STRING/FILE

Cookies字符串或讀取Cookies的文件位置 (H)

-c,

--cookie-jar FILE

操作結束后,要寫入 Cookies 的文件位置 (H)

 

--create-dirs

創(chuàng)建必要的本地目錄層次結構

 

--crlf

在上傳時將 LF 轉寫為 CRLF

 

--crlfile FILE

從指定的文件獲得PEM格式CRL列表

-d,

--data DATA

HTTP POST 數據 (H)

 

--data-ascii DATA  ASCII

編碼 HTTP POST 數據 (H)

 

--data-binary DATA  binary

編碼 HTTP POST 數據 (H)

 

--data-urlencode DATA  url

編碼 HTTP POST 數據 (H)

 

--delegation STRING GSS-API

委托權限

 

--digest

使用數字身份驗證 (H)

 

--disable-eprt

禁止使用 EPRT 或 LPRT (F)

 

--disable-epsv

禁止使用 EPSV (F)

-D,

--dump-header FILE

將頭信息寫入指定的文件

 

--egd-file FILE

為隨機數據設置EGD socket路徑(SSL)

 

--engine ENGINGE

加密引擎 (SSL). "--engine list" 指定列表

-f,

--fail

連接失敗時不顯示HTTP錯誤信息 (H)

-F,

--form CONTENT

模擬 HTTP 表單數據提交(multipart POST) (H)

 

--form-string STRING

模擬 HTTP 表單數據提交 (H)

 

--ftp-account DATA

帳戶數據提交 (F)

 

--ftp-alternative-to-user COMMAND

指定替換 "USER [name]" 的字符串 (F)

 

--ftp-create-dirs

如果不存在則創(chuàng)建遠程目錄 (F)

 

--ftp-method [MULTICWD/NOCWD/SINGLECWD]

控制 CWD (F)

 

--ftp-pasv

使用 PASV/EPSV 替換 PORT (F)

-P,

--ftp-port ADR

使用指定 PORT 及地址替換 PASV (F)

 

--ftp-skip-pasv-ip

跳過 PASV 的IP地址 (F)

 

--ftp-pret

在 PASV 之前發(fā)送 PRET (drftpd) (F)

 

--ftp-ssl-ccc

在認證之后發(fā)送 CCC (F)

 

--ftp-ssl-ccc-mode ACTIVE/PASSIVE

設置 CCC 模式 (F)

 

--ftp-ssl-control ftp

登錄時需要 SSL/TLS (F)

-G,

--get

使用 HTTP GET 方法發(fā)送 -d 數據  (H)

-g,

--globoff

禁用的 URL 隊列 及范圍使用 {} 和 []

-H,

--header LINE

要發(fā)送到服務端的自定義請求頭 (H)

-I,

--head

僅顯示響應文檔頭

-h,

--help

顯示幫助

-0,

--http1.0

使用 HTTP 1.0 (H)

 

--ignore-content-length

忽略 HTTP Content-Length 頭

-i,

--include

在輸出中包含協議頭 (H/F)

-k,

--insecure

允許連接到 SSL 站點,而不使用證書 (H)

 

--interface INTERFACE

指定網絡接口/地址

-4,

--ipv4

將域名解析為 IPv4 地址

-6,

--ipv6

將域名解析為 IPv6 地址

-j,

--junk-session-cookies

讀取文件中但忽略會話cookie (H)

 

--keepalive-time SECONDS

keepalive 包間隔

 

--key KEY

私鑰文件名 (SSL/SSH)

 

--key-type TYPE

私鑰文件類型 (DER/PEM/ENG) (SSL)

 

--krb LEVEL

啟用指定安全級別的 Kerberos (F)

 

--libcurl FILE

命令的libcurl等價代碼

 

--limit-rate RATE

限制傳輸速度

-l,

--list-only

只列出FTP目錄的名稱 (F)

 

--local-port RANGE

強制使用的本地端口號

-L,

--location

跟蹤重定向 (H)

 

--location-trusted

類似  --location 并發(fā)送驗證信息到其它主機 (H)

-M,

--manual

顯示全手動

 

--mail-from FROM

從這個地址發(fā)送郵件

 

--mail-rcpt TO

發(fā)送郵件到這個接收人(s)

 

--mail-auth AUTH

原始電子郵件的起始地址

 

--max-filesize BYTES

下載的最大文件大小 (H/F)

 

--max-redirs NUM

最大重定向數 (H)

-m,

--max-time SECONDS

允許的最多傳輸時間

 

--metalink

處理指定的URL上的XML文件

 

--negotiate

使用 HTTP Negotiate 認證 (H)

-n,

--netrc

必須從 .netrc 文件讀取用戶名和密碼

 

--netrc-optional

使用 .netrc 或 URL; 將重寫 -n 參數

 

--netrc-file FILE

設置要使用的 netrc 文件名

-N,

--no-buffer

禁用輸出流的緩存

 

--no-keepalive

禁用 connection 的 keepalive

 

--no-sessionid

禁止重復使用 SSL session-ID (SSL)

 

--noproxy

不使用代理的主機列表

 

--ntlm

使用 HTTP NTLM 認證 (H)

-o,

--output FILE

將輸出寫入文件,而非 stdout

 

--pass PASS

傳遞給私鑰的短語 (SSL/SSH)

 

--post301

在 301 重定向后不要切換為 GET 請求 (H)

 

--post302

在 302 重定向后不要切換為 GET 請求 (H)

 

--post303

在 303 重定向后不要切換為 GET 請求 (H)

-#,

--progress-bar

以進度條顯示傳輸進度

 

--proto PROTOCOLS

啟用/禁用 指定的協議

 

--proto-redir PROTOCOLS

在重定向上 啟用/禁用 指定的協議

-x,

--proxy [PROTOCOL://]HOST[:PORT]

在指定的端口上使用代理

 

--proxy-anyauth

在代理上使用 "any" 認證方法 (H)

 

--proxy-basic

在代理上使用 Basic 認證  (H)

 

--proxy-digest

在代理上使用 Digest 認證 (H)

 

--proxy-negotiate

在代理上使用 Negotiate 認證 (H)

 

--proxy-ntlm

在代理上使用 NTLM 認證 (H)

-U,

--proxy-user USER[:PASSWORD]

代理用戶名及密碼

 

--proxy1.0 HOST[:PORT]

在指定的端口上使用 HTTP/1.0 代理

-p,

--proxytunnel

使用HTTP代理 (用于 CONNECT)

 

--pubkey KEY

公鑰文件名 (SSH)

-Q,

--quote CMD

在傳輸開始前向服務器發(fā)送命令 (F/SFTP)

 

--random-file FILE

讀取隨機數據的文件 (SSL)

-r,

--range RANGE

僅檢索范圍內的字節(jié)

 

--raw

使用原始HTTP傳輸,而不使用編碼 (H)

-e,

--referer

Referer URL (H)

-J,

--remote-header-name

從遠程文件讀取頭信息 (H)

-O,

--remote-name

將輸出寫入遠程文件

 

--remote-name-all

使用所有URL的遠程文件名

-R,

--remote-time

將遠程文件的時間設置在本地輸出上

-X,

--request COMMAND

使用指定的請求命令

 

--resolve HOST:PORT:ADDRESS

將 HOST:PORT 強制解析到 ADDRESS

 

--retry NUM

出現問題時的重試次數

 

--retry-delay SECONDS

重試時的延時時長

 

--retry-max-time SECONDS

僅在指定時間段內重試

-S,

--show-error

顯示錯誤. 在選項 -s 中,當 curl 出現錯誤時將顯示

-s,

--silent

Silent模式。不輸出任務內容

 

--socks4 HOST[:PORT]

在指定的 host + port 上使用 SOCKS4 代理

 

--socks4a HOST[:PORT]

在指定的 host + port 上使用 SOCKSa 代理

 

--socks5 HOST[:PORT]

在指定的 host + port 上使用 SOCKS5 代理

 

--socks5-hostname HOST[:PORT] SOCKS5

代理,指定用戶名、密碼

 

--socks5-gssapi-service NAME

為gssapi使用SOCKS5代理服務名稱

 

--socks5-gssapi-nec

與NEC Socks5服務器兼容

-Y,

--speed-limit RATE

在指定限速時間之后停止傳輸

-y,

--speed-time SECONDS

指定時間之后觸發(fā)限速. 默認 30

 

--ssl

嘗試 SSL/TLS (FTP, IMAP, POP3, SMTP)

 

--ssl-reqd

需要 SSL/TLS (FTP, IMAP, POP3, SMTP)

-2,

--sslv2

使用 SSLv2 (SSL)

-3,

--sslv3

使用 SSLv3 (SSL)

 

--ssl-allow-beast

允許的安全漏洞,提高互操作性(SSL)

 

--stderr FILE

重定向 stderr 的文件位置. - means stdout

 

--tcp-nodelay

使用 TCP_NODELAY 選項

-t,

--telnet-option OPT=VAL

設置 telnet 選項

 

--tftp-blksize VALUE

設備 TFTP BLKSIZE 選項 (必須 >512)

-z,

--time-cond TIME

基于時間條件的傳輸

-1,

--tlsv1

使用 => TLSv1 (SSL)

 

--tlsv1.0

使用 TLSv1.0 (SSL)

 

--tlsv1.1

使用 TLSv1.1 (SSL)

 

--tlsv1.2

使用 TLSv1.2 (SSL)

 

--trace FILE

將 debug 信息寫入指定的文件

 

--trace-ascii FILE

類似  --trace 但使用16進度輸出

 

--trace-time

向 trace/verbose 輸出添加時間戳

 

--tr-encoding

請求壓縮傳輸編碼 (H)

-T,

--upload-file FILE

將文件傳輸(上傳)到指定位置

 

--url URL

指定所使用的 URL

-B,

--use-ascii

使用 ASCII/text 傳輸

-u,

--user USER[:PASSWORD]

指定服務器認證用戶名、密碼

 

--tlsuser USER

TLS 用戶名

 

--tlspassword STRING

TLS 密碼

 

--tlsauthtype STRING

TLS 認證類型 (默認 SRP)

 

--unix-socket FILE

通過這個 UNIX socket 域連接

-A,

--user-agent STRING

要發(fā)送到服務器的 User-Agent (H)

-v,

--verbose

顯示詳細操作信息

-V,

--version

顯示版本號并退出

-w,

--write-out FORMAT

完成后輸出什么

 

--xattr

將元數據存儲在擴展文件屬性中

-q

 

.curlrc 如果作為第一個參數無效

 

五、Linux cURL命令退出碼

下面是linux curl命令的錯誤代碼和她們的相應的錯誤消息,命令執(zhí)行錯誤的時候可以通過錯誤碼來查看出錯原因,方便開發(fā)調試。

退 出 碼

錯誤描述

1

Unsupported protocol. This build of curl has no support for this protocol.

2

Failed to initialize.

3

URL malformed. The syntax was not correct.

5

Couldn't resolve proxy. The given proxy host could not be resolved.

6

Couldn't resolve host. The given remote host was not resolved.

7

Failed to connect to host.

8

FTP weird server reply. The server sent data curl couldn't parse.

9

FTP access denied. The server denied login or denied access to the particular resource or directory you wanted to reach. Most often you tried to change to a directory that doesn't exist on the server.

11

FTP weird PASS reply. Curl couldn't parse the reply sent to the PASS request.

13

FTP weird PASV reply, Curl couldn't parse the reply sent to the PASV request.

14

FTP weird 227 format. Curl couldn't parse the 227-line the server sent.

15

FTP can't get host. Couldn't resolve the host IP we got in the 227-line.

17

FTP couldn't set binary. Couldn't change transfer method to binary.

18

Partial file. Only a part of the file was transferred.

19

FTP couldn't download/access the given file, the RETR (or similar) command failed.

21

FTP quote error. A quote command returned error from the server.

22

HTTP page not retrieved. The requested url was not found or returned another error with the HTTP error code being 400 or above. This return code only appears if -f/--fail is used.

23

Write error. Curl couldn't write data to a local filesystem or similar.

25

FTP couldn't STOR file. The server denied the STOR operation, used for FTP uploading.

26

Read error. Various reading problems.

27

Out of memory. A memory allocation request failed.

28

Operation timeout. The specified time-out period was reached according to the conditions.

30

FTP PORT failed. The PORT command failed. Not all FTP servers support the PORT command, try doing a transfer using PASV instead!

31

FTP couldn't use REST. The REST command failed. This command is used for resumed FTP transfers.

33

HTTP range error. The range "command" didn't work.

34

HTTP post error. Internal post-request generation error.

35

SSL connect error. The SSL handshaking failed.

36

FTP bad download resume. Couldn't continue an earlier aborted download.

37

FILE couldn't read file. Failed to open the file. Permissions?

38

LDAP cannot bind. LDAP bind operation failed.

39

LDAP search failed.

41

Function not found. A required LDAP function was not found.

42

Aborted by callback. An application told curl to abort the operation.

43

Internal error. A function was called with a bad parameter.

45

Interface error. A specified outgoing interface could not be used.

47

Too many redirects. When following redirects, curl hit the maximum amount.

48

Unknown TELNET option specified.

49

Malformed telnet option.

51

The peer's SSL certificate or SSH MD5 fingerprint was not ok.

52

The server didn't reply anything, which here is considered an error.

53

SSL crypto engine not found.

54

Cannot set SSL crypto engine as default.

55

Failed sending network data.

56

Failure in receiving network data.

58

Problem with the local certificate.

59

Couldn't use specified SSL cipher.

60

Peer certificate cannot be authenticated with known CA certificates.

61

Unrecognized transfer encoding.

62

Invalid LDAP URL.

63

Maximum file size exceeded.

64

Requested FTP SSL level failed.

65

Sending the data requires a rewind that failed.

66

Failed to initialize SSL Engine.

67

The user name, password, or similar was not accepted and curl failed to log in.

68

File not found on TFTP server.

69

Permission problem on TFTP server.

70

Out of disk space on TFTP server.

71

Illegal TFTP operation.

72

Unknown TFTP transfer ID.

73

File already exists (TFTP).

74

No such user (TFTP).

75

Character conversion failed.

76

Character conversion functions required.

77

Problem with reading the SSL CA cert (path? access rights?).

78

The resource referenced in the URL does not exist.

79

An unspecified error occurred during the SSH session.

80

Failed to shut down the SSL connection.

82

Could not load CRL file, missing or wrong format (added in 7.19.0).

83

Issuer check failed (added in 7.19.0).

XX

More error codes will appear here in future releases. The existing ones are meant to never change.

六、用法演示:

為節(jié)省篇幅,部分操作不再貼上執(zhí)行結果。

1、查看網頁源碼

直接在curl命令后加上網址,就可以看到網頁源碼。我們以網址www.sina.com為例(選擇該網址,主要因為它的網頁代碼較短):

root@ubuntu:/home/peng# curl www.sohu.com
<html>
<head><title>307 Temporary Redirect</title></head>
<body bgcolor="white">
<center><h1>307 Temporary Redirect</h1></center>
<hr><center>nginx</center>
</body>
</html>

執(zhí)行結果顯示 307 Temporary Redirect,說明該網址需要重定向。

如果要把這個網頁保存下來,可以使用-o參數,這就相當于使用wget命令了。

curl -o [文件名] www.sohu.com

2、自動跳轉

有的網址是自動跳轉的。使用-L參數,curl就會跳轉到新的網址。

curl -L www.sohu.com

鍵入上面的命令,結果就自動跳轉為www.sohu.com.cn。

3、顯示頭信息

-i參數可以顯示http response的頭信息,連同網頁代碼一起。

root@ubuntu:/home/peng/driver/test#  curl -i www.sohu.com
HTTP/1.1 307 Temporary Redirect
Content-Type: text/html
Content-Length: 180
Connection: keep-alive
Server: nginx
Date: Tue, 25 Aug 2020 10:10:54 GMT
Location: https://www.sohu.com/
FSS-Cache: from 9790436.18244590.10468709
FSS-Proxy: Powered by 2384755.3433341.3062915

<html>
<head><title>307 Temporary Redirect</title></head>
<body bgcolor="white">
<center><h1>307 Temporary Redirect</h1></center>
<hr><center>nginx</center>
</body>
</html>

-I參數則是只顯示http response的頭信息。

4、顯示通信過程

-v參數可以顯示一次http通信的整個過程,包括端口連接和http request頭信息。

root@ubuntu:/home/peng/driver/test#  curl -v www.sohu.com
* About to connect() to www.sohu.com port 80 (#0)
*   Trying 240e:83:201:3700::5... connected
> GET / HTTP/1.1
> User-Agent: curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: www.sohu.com
> Accept: */*

< HTTP/1.1 307 Temporary Redirect
< Content-Type: text/html
< Content-Length: 180
< Connection: keep-alive
< Server: nginx
< Date: Tue, 25 Aug 2020 10:11:49 GMT
< Location: https://www.sohu.com/
< FSS-Cache: from 9855973.18375663.10534247
< FSS-Proxy: Powered by 2450292.3564414.3128453

<html>
<head><title>307 Temporary Redirect</title></head>
<body bgcolor="white">
<center><h1>307 Temporary Redirect</h1></center>
<hr><center>nginx</center>
</body>
</html>
* Connection #0 to host www.sohu.com left intact
* Closing connection #0

如果你覺得上面的信息還不夠,那么下面的命令可以查看更詳細的通信過程。

curl --trace output.txt www.sohu.com

或者

curl --trace-ascii output.txt www.sohu.com

運行后,請打開output.txt文件查看。

5、發(fā)送表單信息

發(fā)送表單信息有GET和POST兩種方法。GET方法相對簡單,只要把數據附在網址后面就行。

curl example.com/form.cgi?data=xxx

POST方法必須把數據和網址分開,curl就要用到--data參數。

curl -X POST --data "data=xxx" example.com/form.cgi

如果你的數據沒有經過表單編碼,還可以讓curl為你編碼,參數是--data-urlencode。

curl -X POST--data-urlencode "date=April 1" example.com/form.cgi

6、HTTP動詞

curl默認的HTTP動詞是GET,使用-X參數可以支持其他動詞。

curl -X POST www.example.com
curl -X DELETE www.example.com

7、文件上傳

假定文件上傳的表單是下面這樣:

<form method="POST" enctype='multipart/form-data' action="upload.cgi">
 <input type=file name=upload>
 <input type=submit name=press value="OK">
</form>

你可以用curl這樣上傳文件:

curl --form upload=@localfilename --form press=OK [URL]

8、Referer字段

有時你需要在http request頭信息中,提供一個referer字段,表示你是從哪里跳轉過來的。

curl --referer http://www.example.com http://www.example.com

9、User Agent字段

這個字段是用來表示客戶端的設備信息。服務器有時會根據這個字段,針對不同設備,返回不同格式的網頁,比如手機版和桌面版。

iPhone4的User Agent是

Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7

curl可以這樣模擬:

curl --user-agent "[User Agent]" [URL]

10、cookie

使用--cookie參數,可以讓curl發(fā)送cookie。

curl --cookie "name=xxx" www.example.com

至于具體的cookie的值,可以從http response頭信息的Set-Cookie字段中得到。

-c cookie-file可以保存服務器返回的cookie到文件,-b cookie-file可以使用這個文件作為cookie信息,進行后續(xù)的請求。

curl -c cookies http://example.com
curl -b cookies http://example.com

11、增加頭信息

有時需要在http request之中,自行增加一個頭信息。--header參數就可以起到這個作用。

$ curl --header "Content-Type:application/json" http://example.com

12、認證

使用curl選項 -u 可以完成HTTP或者FTP的認證,可以指定密碼,也可以不指定密碼在后續(xù)操作中輸入密碼:

curl -u user:pwd http://man.linuxde.net
curl -u user http://man.linuxde.net

13、FTP

1)、列出ftp服務器上的目錄列表

curl ftp://www.xxx.com/ --user name:passwd
curl ftp://www.xxx.com/ –u name:passwd #簡潔寫法
curl ftp://name:passwd@www.xxx.com #簡潔寫法2

例如:在IP地址192.168.43.117上搭建FTP服務器,并設置用戶名為user,密碼為123456。

現在我們要顯示服務器上根目錄下的所有文件信息,命令如下:

curl -u user:123456 ftp://192.168.43.117

執(zhí)行結果如下:

一篇文了解Linux命令cURL

簡潔寫法:

curl ftp://user:123456@192.168.43.117

執(zhí)行結果如下:

一篇文了解Linux命令cURL

2)、只列出目錄,不顯示進度條

curl ftp://www.xxx.com –u name:passwd -s

3)、下載一個文件:

命令如下:

curl ftp://www.xxx.com/size.zip –u name:passwd -o size.zip

示例如下:從服務器的根目錄下下載文件test.c,保存到本地,本地文件名也為test.c。【注意】如果沒有-o選項,程序會吧數據流定向到stdout,即直接把文件內容顯示到終端上。

curl ftp://user:123456@192.168.43.117/test.c -o test.c

執(zhí)行結果如下:

一篇文了解Linux命令cURL

簡潔模式

curl -u user:123456 ftp://192.168.43.117/list.h -o list.h

執(zhí)行結果如下:

一篇文了解Linux命令cURL

4)、上載一個文件:

curl –u name:passwd -T size.mp3 ftp://www.xxx.com/mp3/

舉例如下:

curl -u user:123456 ftp://192.168.43.117/ -T list.h

一篇文了解Linux命令cURL

可以看到文件并沒有上傳成功,返回錯誤碼是25,參考第五章

25 FTP couldn't STOR file. The server denied the STOR operation, used for FTP uploading.

可知,是因為服務器沒有賦予存儲的權限,所以設置服務器的write權限即可。

一篇文了解Linux命令cURL

5)、從服務器上刪除文件(使用curl傳遞ftp協議的DELE命令):

curl –u name:passwd ftp://www.xxx.com/ -X 'DELE mp3/size.mp3'

6)、另外curl不支持遞歸下載,不過可以用數組方式下載文件,比如我們要下載1-10.gif連續(xù)命名的文件:**

curl –u name:passwd ftp://www.xxx.com/img/[1-10].gif –O #O字母大寫

7)、要連續(xù)下載多個文件:

curl –u name:passwd ftp://www.xxx.com/img/[one,two,three].jpg –O #O字母大寫

重要聲明:本文來自一口Linux,經授權轉載,版權歸原作者所有,不代表銳成觀點,轉載的目的在于傳遞更多知識和信息。

我的評論

還未登錄?點擊登錄

微信掃碼溝通
微信掃碼溝通

微信掃碼溝通

AI
return head