您現在的位置是:首頁 > 單機遊戲首頁單機遊戲

Linux下curl用法詳解

簡介$ curl -A‘’https:google

linux如何由a檔案生成o檔案

Linux下curl用法詳解

Curl(CommandLine Uniform Resource Locator),即在命令列中利用URL進行資料或者檔案傳輸,它是Linux下強大的http命令列工具,其功能十分強大,我們來看下幫助頁面,引數非常豐富。

[root@mx ~]

# curl -h

Usage: curl [options。。。]

Options: (H) means HTTP/HTTPS only, (F) means FTP only

——anyauth Pick

“any”

authentication method (H)

-a, ——append Append to target file when uploading (F/SFTP)

——basic Use HTTP Basic Authentication (H)

——cacert FILE CA certificate to verify peer against (SSL)

——capath DIR CA directory to verify peer against (SSL)

-E, ——cert CERT[:PASSWD] Client certificate file and password (SSL)

——cert-type TYPE Certificate file

type

(DER/PEM/ENG) (SSL)

——ciphers LIST SSL ciphers to use (SSL)

——compressed Request compressed response (using deflate or gzip)

-K, ——config FILE Specify

which

config file to

read

——connect-timeout SECONDS Maximum time allowed

for

connection

-C, ——

continue

-at OFFSET Resumed transfer offset

-b, ——cookie STRING/FILE String or file to

read

cookies from (H)

-c, ——cookie-jar FILE Write cookies to this file after operation (H)

——create-dirs Create necessary

local

directory hierarchy

——crlf Convert LF to CRLF

in

upload

——crlfile FILE Get a CRL list

in

PEM format from the given file

-d, ——data DATA HTTP POST data (H)

——data-ascii DATA HTTP POST ASCII data (H)

——data-binary DATA HTTP POST binary data (H)

——data-urlencode DATA HTTP POST data url encoded (H)

——delegation STRING GSS-API delegation permission

——digest Use HTTP Digest Authentication (H)

——

disable

-eprt Inhibit using EPRT or LPRT (F)

——

disable

-epsv Inhibit using EPSV (F)

-D, ——dump-header FILE Write the headers to this file

——egd-file FILE EGD socket path

for

random data (SSL)

——engine ENGINGE Crypto engine (SSL)。

“——engine list”

for

list

-f, ——fail Fail silently (no output at all) on HTTP errors (H)

-F, ——form CONTENT Specify HTTP multipart POST data (H)

——form-string STRING Specify HTTP multipart POST data (H)

——ftp-account DATA Account data string (F)

——ftp-alternative-to-user COMMAND String to replace

“USER [name]”

(F)

——ftp-create-dirs Create the remote

dirs

if

not present (F)

——ftp-method [MULTICWD/NOCWD/SINGLECWD] Control CWD usage (F)

——ftp-pasv Use PASV/EPSV instead of PORT (F)

-P, ——ftp-port ADR Use PORT with given address instead of PASV (F)

——ftp-skip-pasv-ip Skip the IP address

for

PASV (F)

——ftp-pret Send PRET before PASV (

for

drftpd) (F)

——ftp-ssl-ccc Send CCC after authenticating (F)

——ftp-ssl-ccc-mode ACTIVE/PASSIVE Set CCC mode (F)

——ftp-ssl-control Require SSL/TLS

for

ftp login, clear

for

transfer (F)

-G, ——get Send the -d data with a HTTP GET (H)

-g, ——globoff Disable URL sequences and ranges using {} and []

-H, ——header LINE Custom header to pass to server (H)

-I, ——head Show document info only

-h, ——

help

This

help

text

——hostpubmd5 MD5 Hex encoded MD5 string of the host public key。 (SSH)

-0, ——http1。0 Use HTTP 1。0 (H)

——ignore-content-length Ignore the HTTP Content-Length header

-i, ——include Include protocol headers

in

the output (H/F)

-k, ——insecure Allow connections to SSL sites without certs (H)

——interface INTERFACE Specify network interface/address to use

-4, ——ipv4 Resolve name to IPv4 address

-6, ——ipv6 Resolve name to IPv6 address

-j, ——junk-session-cookies Ignore session cookies

read

from file (H)

——keepalive-time SECONDS Interval between keepalive probes

——key KEY Private key file name (SSL/SSH)

——key-type TYPE Private key file

type

(DER/PEM/ENG) (SSL)

——krb LEVEL Enable Kerberos with specified security level (F)

——libcurl FILE Dump libcurl equivalent code of this

command

line

——

limit

-rate RATE Limit transfer speed to this rate

-l, ——list-only List only names of an FTP directory (F)

——

local

-port RANGE Force use of these

local

port numbers

-L, ——location Follow redirects (H)

——location-trusted like ——location and send auth to other hosts (H)

-M, ——manual Display the full manual

——mail-from FROM Mail from this address

——mail-rcpt TO Mail to this receiver(s)

——mail-auth AUTH Originator address of the original email

——max-filesize BYTES Maximum file size to download (H/F)

——max-redirs NUM Maximum number of redirects allowed (H)

-m, ——max-time SECONDS Maximum time allowed

for

the transfer

——metalink Process given URLs as metalink XML file

——negotiate Use HTTP Negotiate Authentication (H)

-n, ——netrc Must

read

。netrc

for

user name and password

——netrc-optional Use either 。netrc or URL; overrides -n

——netrc-file FILE Set up the netrc filename to use

-N, ——no-buffer Disable buffering of the output stream

——no-keepalive Disable keepalive use on the connection

——no-sessionid Disable SSL session-ID reusing (SSL)

——noproxy List of hosts

which

do

not use proxy

——ntlm Use HTTP NTLM authentication (H)

-o, ——output FILE Write output to instead of stdout

——pass PASS Pass phrase

for

the private key (SSL/SSH)

——post301 Do not switch to GET after following a 301 redirect (H)

——post302 Do not switch to GET after following a 302 redirect (H)

——post303 Do not switch to GET after following a 303 redirect (H)

-

#, ——progress-bar Display transfer progress as a progress bar

——proto PROTOCOLS Enable/

disable

specified protocols

——proto-redir PROTOCOLS Enable/

disable

specified protocols on redirect

-x, ——proxy [PROTOCOL://]HOST[:PORT] Use proxy on given port

——proxy-anyauth Pick

“any”

proxy authentication method (H)

——proxy-basic Use Basic authentication on the proxy (H)

——proxy-digest Use Digest authentication on the proxy (H)

——proxy-negotiate Use Negotiate authentication on the proxy (H)

——proxy-ntlm Use NTLM authentication on the proxy (H)

-U, ——proxy-user USER[:PASSWORD] Proxy user and password

——proxy1。0 HOST[:PORT] Use HTTP/1。0 proxy on given port

-p, ——proxytunnel Operate through a HTTP proxy tunnel (using CONNECT)

——pubkey KEY Public key file name (SSH)

-Q, ——quote CMD Send

command

(s) to server before transfer (F/SFTP)

——random-file FILE File

for

reading random data from (SSL)

-r, ——range RANGE Retrieve only the bytes within a range

——raw Do HTTP

“raw”

, without any transfer decoding (H)

-e, ——referer Referer URL (H)

-J, ——remote-header-name Use the header-provided filename (H)

-O, ——remote-name Write output to a file named as the remote file

——remote-name-all Use the remote file name

for

all URLs

-R, ——remote-time Set the remote file

‘s time on the local output

-X, ——request COMMAND Specify request command to use

——resolve HOST:PORT:ADDRESS Force resolve of HOST:PORT to ADDRESS

——retry NUM Retry request NUM times if transient problems occur

——retry-delay SECONDS When retrying, wait this many seconds between each

——retry-max-time SECONDS Retry only within this period

-S, ——show-error Show error。 With -s, make curl show errors when they occur

-s, ——silent Silent mode。 Don’

t output anything

——socks4 HOST[:PORT] SOCKS4 proxy on given host + port

——socks4a HOST[:PORT] SOCKS4a proxy on given host + port

——socks5 HOST[:PORT] SOCKS5 proxy on given host + port

——socks5-basic Enable username/password auth

for

SOCKS5 proxies

——socks5-gssapi Enable GSS-API auth

for

SOCKS5 proxies

——socks5-hostname HOST[:PORT] SOCKS5 proxy, pass host name to proxy

——socks5-gssapi-service NAME SOCKS5 proxy service name

for

gssapi

——socks5-gssapi-nec Compatibility with NEC SOCKS5 server

-Y, ——speed-limit RATE Stop transfers below speed-limit

for

‘speed-time’

secs

-y, ——speed-time SECONDS Time

for

trig speed-limit abort。 Defaults to 30

——ssl Try SSL/TLS (FTP, IMAP, POP3, SMTP)

——ssl-reqd Require SSL/TLS (FTP, IMAP, POP3, SMTP)

-2, ——sslv2 Use SSLv2 (SSL)

-3, ——sslv3 Use SSLv3 (SSL)

——ssl-allow-beast Allow security flaw to improve interop (SSL)

——stderr FILE Where to redirect stderr。 - means stdout

——tcp-nodelay Use the TCP_NODELAY option

-t, ——telnet-option OPT=VAL Set telnet option

——tftp-blksize VALUE Set TFTP BLKSIZE option (must be >512)

-z, ——time-cond TIME Transfer based on a time condition

-1, ——tlsv1 Use => TLSv1 (SSL)

——tlsv1。0 Use TLSv1。0 (SSL)

——tlsv1。1 Use TLSv1。1 (SSL)

——tlsv1。2 Use TLSv1。2 (SSL)

——tlsv1。3 Use TLSv1。3 (SSL)

——tls-max VERSION Use TLS up to VERSION (SSL)

——trace FILE Write a debug trace to the given file

——trace-ascii FILE Like ——trace but without the hex output

——trace-time Add time stamps to trace/verbose output

——tr-encoding Request compressed transfer encoding (H)

-T, ——upload-file FILE Transfer FILE to destination

——url URL URL to work with

-B, ——use-ascii Use ASCII/text transfer

-u, ——user USER[:PASSWORD] Server user and password

——tlsuser USER TLS username

——tlspassword STRING TLS password

——tlsauthtype STRING TLS authentication

type

(default SRP)

——unix-socket FILE Connect through this UNIX domain socket

-A, ——user-agent STRING User-Agent to send to server (H)

-v, ——verbose Make the operation more talkative

-V, ——version Show version number and quit

-w, ——write-out FORMAT What to output after completion

——xattr Store metadata

in

extended file attributes

-q If used as the first parameter disables 。curlrc

[root@mx ~]

#

curl在Mac和Linux下是自帶的命令列工具,如果你也想在Windows上使用,這裡推薦使用“git for windows”這個工具,你可以使用此關鍵字在網路上檢索下載。

安裝完成後,點選桌面的“Git Bash”後,就可以在Windows下使用curl命令列工具了。

基本用法

curl是http工具,最簡單的使用方法就是直接搭配網址使用,比如請求百度的內容:

[root@mx ~]

# curl www。baidu。com

-s 選項:

加上此選項,不顯示請求內容的進度資訊。

-o 選項:

將獲取內容儲存到本地檔案,-o後接檔名,如把請求的內容儲存到1。txt中

# 搭配-s選項一起使用

[root@mx ~]

# curl -s https://www。baidu。com -o 1。txt

# 不使用-s時,顯示進度資訊

[root@mx ~]

# curl https://www。baidu。com -o 2。txt

% Total % Received % Xferd Average Speed Time Time Time Current

Dload Upload Total Spent Left Speed

100 2443 100 2443 0 0 14183 0 ——:——:—— ——:——:—— ——:——:—— 14203

[root@mx ~]

# cat 1。txt

<!DOCTYPE html>

<!——STATUS OK——>

type

=text/css href=https://ss1。bdstatic。com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu。min。css>百度一下,你就知道

……

[root@mx ~]

#

詳細用法

-A 選項:

-A引數指定客戶端的使用者代理標頭,即User-Agent。curl 的預設使用者代理字串是curl/[version]。下面命令將User-Agent改成 Chrome 瀏覽器。

$ curl -A

‘Mozilla/5。0 (Windows NT 10。0; Win64; x64) AppleWebKit/537。36 (KHTML, like Gecko) Chrome/76。0。3809。100 Safari/537。36’

https://google。com

下面命令會移除User-Agent標頭。

$ curl -A

‘’

https://google。com

也可以透過-H引數直接指定標頭,更改User-Agent。

$ curl -H

‘User-Agent: php/1。0’

https://google。com

-b 選項:

-b引數用來向伺服器傳送 Cookie。

$ curl -b

‘foo=bar’

https://google。com

上面命令會生成一個標頭Cookie: foo=bar,向伺服器傳送一個名為foo、值為bar的 Cookie。

$ curl -b

‘foo1=bar;foo2=bar2’

https://google。com

上面命令傳送兩個 Cookie。

$ curl -b cookies。txt https://www。google。com

上面命令讀取本地檔案cookies。txt,裡面是伺服器設定的 Cookie(參見-c引數),將其傳送到伺服器。

-c 選項:

-c引數將伺服器設定的 Cookie 寫入一個檔案。

$ curl -c cookies。txt https://www。google。com

上面命令將伺服器的 HTTP 迴應所設定 Cookie 寫入文字檔案cookies。txt。

-d 選項:

-d引數用於傳送 POST 請求的資料體。

$ curl -d

‘login=emma&password=123’

-X POST https://google。com/login

#或者

$ curl -d

‘login=emma’

-d

‘password=123’

-X POST https://google。com/login

使用-d引數以後,HTTP 請求會自動加上標頭Content-Type : application/x-www-form-urlencoded。並且會自動將請求轉為 POST 方法,因此可以省略-X POST。

-d引數可以讀取本地文字檔案的資料,向伺服器傳送。

$ curl -d

‘@data。txt’

https://google。com/login

上面命令讀取data。txt檔案的內容,作為資料體向伺服器傳送。

——data-urlencode——data-urlencode引數等同於-d,傳送 POST 請求的資料體,區別在於會自動將傳送的資料進行 URL 編碼。

$ curl ——data-urlencode

‘comment=hello world’

https://google。com/login

上面程式碼中,傳送的資料hello world之間有一個空格,需要進行 URL 編碼。

-e 選項:

-e引數用來設定 HTTP 的標頭Referer,表示請求的來源。

curl -e

‘https://google。com?q=example’

https://www。example。com

上面命令將Referer標頭設為https://google。com?q=example。

-H引數可以透過直接新增標頭Referer,達到同樣效果。

curl -H

‘Referer: https://google。com?q=example’

https://www。example。com

-F 引數:

-F引數用來向伺服器上傳二進位制檔案。

$ curl -F

‘file=@photo。png’

https://google。com/profile

上面命令會給 HTTP 請求加上標頭Content-Type: multipart/form-data,然後將檔案photo。png作為file欄位上傳。

-F引數可以指定 MIME 型別。

$ curl -F

‘file=@photo。png;type=image/png’

https://google。com/profile

上面命令指定 MIME 型別為image/png,否則 curl 會把 MIME 型別設為application/octet-stream。

-F引數也可以指定檔名。

$ curl -F

‘file=@photo。png;filename=me。png’

https://google。com/profile

上面命令中,原始檔名為photo。png,但是伺服器接收到的檔名為me。png。

-G 選項:

-G引數用來構造 URL 的查詢字串。

$ curl -G -d

‘q=kitties’

-d

‘count=20’

https://google。com/search

上面命令會發出一個 GET 請求,實際請求的 URL 為https://google。com/search?q=kitties&count=20。如果省略——G,會發出一個 POST 請求。

如果資料需要 URL 編碼,可以結合——data——urlencode引數。

$ curl -G ——data-urlencode

‘comment=hello world’

https://www。example。com

-H 選項:

-H引數新增 HTTP 請求的標頭。

$ curl -H

‘Accept-Language: en-US’

https://google。com

上面命令新增 HTTP 標頭Accept-Language: en-US。

$ curl -H

‘Accept-Language: en-US’

-H

‘Secret-Message: xyzzy’

https://google。com

上面命令新增兩個 HTTP 標頭。

$ curl -d

‘{“login”: “emma”, “pass”: “123”}’

-H

‘Content-Type: application/json’

https://google。com/login

上面命令新增 HTTP 請求的標頭是Content-Type: application/json,然後用-d引數傳送 JSON 資料。

-i 選項:

-i引數打印出伺服器迴應的 HTTP 標頭。

$ curl -i https://www。example。com

上面命令收到伺服器迴應後,先輸出伺服器迴應的標頭,然後空一行,再輸出網頁的原始碼。

-I 選項:

-I引數向伺服器發出 HEAD 請求,然會將伺服器返回的 HTTP 標頭打印出來。

$ curl -I https://www。example。com

上面命令輸出伺服器對 HEAD 請求的迴應。

——head引數等同於-I。

$ curl ——head https://www。example。com

-k 引數:

-k引數指定跳過 SSL 檢測。

$ curl -k https://www。example。com

上面命令不會檢查伺服器的 SSL 證書是否正確。

-L 引數:

-L引數會讓 HTTP 請求跟隨伺服器的重定向。curl 預設不跟隨重定向。

$ curl -L -d

‘tweet=hi’

https://api。twitter。com/tweet

——limit-rate 選項:

——limit-rate用來限制 HTTP 請求和迴應的頻寬,模擬慢網速的環境。

$ curl ——

limit

-rate 200k https://google。com

上面命令將頻寬限制在每秒 200K 位元組。

-o 選項:

-o引數將伺服器的迴應儲存成檔案,等同於wget命令。

$ curl -o example。html https://www。example。com

上面命令將www。example。com儲存成example。html。

-O 選項:

-O引數將伺服器迴應儲存成檔案,並將 URL 的最後部分當作檔名。

$ curl -O https://www。example。com/foo/bar。html

上面命令將伺服器迴應儲存成檔案,檔名為bar。html。

-s 選項:

-s引數將不輸出錯誤和進度資訊。

$ curl -s https://www。example。com

上面命令一旦發生錯誤,不會顯示錯誤資訊。不發生錯誤的話,會正常顯示執行結果。

如果想讓 curl 不產生任何輸出,可以使用下面的命令。

$ curl -s -o /dev/null https://google。com

-S 選項:

-S引數指定只輸出錯誤資訊,通常與-s一起使用。

$ curl -s -o /dev/null https://google。com

上面命令沒有任何輸出,除非發生錯誤。

-u 選項:

-u引數用來設定伺服器認證的使用者名稱和密碼。

$ curl -u

‘bob:12345’

https://google。com/login

上面命令設定使用者名稱為bob,密碼為12345,然後將其轉為 HTTP 標頭Authorization: Basic Ym9iOjEyMzQ1。

curl 能夠識別 URL 裡面的使用者名稱和密碼。

$ curl https://bob:12345@google。com/login

上面命令能夠識別 URL 裡面的使用者名稱和密碼,將其轉為上個例子裡面的 HTTP 標頭。

$ curl -u

‘bob’

https://google。com/login

上面命令只設置了使用者名稱,執行後,curl 會提示使用者輸入密碼。

-v 選項:

-v引數輸出通訊的整個過程,用於除錯。

$ curl -v https://www。example。com

——trace引數也可以用於除錯,還會輸出原始的二進位制資料。

$ curl ——trace - https://www。example。com

-x 選項:

-x引數指定 HTTP 請求的代理。

$ curl -x socks5://james:cats@myproxy。com:8080 https://www。example。com

上面命令指定 HTTP 請求透過myproxy。com:8080的 socks5 代理發出。

如果沒有指定代理協議,預設為 HTTP。

$ curl -x james:cats@myproxy。com:8080 https://www。example。com

上面命令中,請求的代理使用 HTTP 協議。

-X 選項:

-X引數指定 HTTP 請求的方法。

$ curl -X POST https://www。example。com

上面命令對https://www。example。com發出 POST 請求。

參考:

參考作者:阮一峰的日誌

地址連結:

www。ruanyifeng。com/blog/2019/0…

Top