簡單介紹shell中的curl網路請求的實現

大雄45發表於2022-03-16
導讀 本文主要介紹了 中的curl網路請求的實現,文中透過示例程式碼介紹的非常詳細,具有一定的參考價值,感興趣的小夥伴們可以參考一下

shell中的curl網路請求的實現

curl 是利用URL語法在 行下工作的檔案傳輸工具,1997年首次發行,支援檔案上傳和下載,結合shell 體驗更棒。但按照傳統習慣稱 curl 為下載工具。

curl 支援的通訊協議有 有FTP、FTPS、HTTP、HTTPS、TFTP、SFTP 等等,支援的平臺有  、MacOSX、Darwin、Windows、DOS、FreeBSD等等。

一、curl的作用:
1、檢視網頁原始碼
denglibingdeMacBook-Pro-4: curl 
 
< !DOCTYPE html>
< !--STATUS OK-->< html> < meta content=always name=referrer>< link rel=stylesheet type=text/css href=百度一下,你就知道 < body link=#0000cc> < div id=wrapper> < div id=head> < div class=head_wrapper> < div class=s_form> < div class=s_form_wrapper> < div id=lg> < img hidefocus=true src= ///img/bd_logo1.png width=270 height=129>  < form id=form name=f action=///s class=fm> < input type=hidden name=bdorz_come value=1> < input type=hidden name=ie value=utf-8> < input type=hidden name=f value=8> < input type=hidden name=rsv_bp value=1> < input type=hidden name=rsv_idx value=1> < input type=hidden name=tn value=baidu>< span class="bg s_ipt_wr">< input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus>< span class="bg s_btn_wr">< input type=submit id=su value=百度一下 class="bg s_btn">< a href=新聞 < a href=地圖 < a href=影片 < a href=貼吧 < noscript> < a href=http:///bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2F%2f%3fbdorz_come%3d1 name=tj_login class=lb>登入  < script>document.write('< a href="http:///bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === " rel="external nofollow" " ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登入'); < a href=///more/ name=tj_briicon class=bri style="display: block;">更多產品< div id=ftCon> < div id=ftConw>< a href=關於百度 < a href=©2017 Baidu < a href=http:///duty/>使用百度前必讀  < a href=意見反饋 京ICP證030173號  < img src=///img/gs.gif>      
 
// 儲存整個網頁,使用 -o 處理
denglibingdeMacBook-Pro-4: curl -o baidu 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2381  100  2381    0     0  77899      0 --:--:-- --:--:-- --:--:-- 79366
2、檢視頭資訊
denglibingdeMacBook-Pro-4: denglibing$ curl -i 
HTTP/1.1 200 OK
Server: bfe/1.0.8.18
Date: Mon, 03 Jul 2017 09:12:17 GMT
Content-Type: text/html
Content-Length: 2381
Last-Modified: Mon, 23 Jan 2017 13:28:11 GMT
Connection: Keep-Alive
ETag: "588604eb-94d"
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Pragma: no-cache
Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
Accept-Ranges: bytes
...
...
...
3、傳送網路請求資訊

GET方式請求:

curl example.com/form.cgi?data=xxx  如果這裡的URL指向的是一個檔案或者一幅圖都可以直接下載到本地

POST方式請求:

//資料和網址分開,需要使用 '--data' 或者 '-d' 引數; curl預設使用GET,使用 '-X' 引數可以支援其他動詞, 更多的引數使用 'man curl' 檢視
$ curl -X POST --data "data=xxx" example.com/form.cgi
 
// '--user-agent' 欄位,表表面客戶端的裝置資訊:
$ curl --user-agent "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) Mobile/14F89/mideaConnect MissonWebKit/4021/zh-Hans (AppStore) (4347701760)" 
 
//使用 '--cookie' 引數,可以讓curl傳送cookie
$ curl --cookie "name=xxx" 
 
//新增頭資訊,自行增加一個頭資訊。'--header' 或者 '-H' 引數就可以起到這個作用
$ curl --header "Content-Type:application/json" 
 
 
//提交檔案作為請求資訊 使用 '@檔名' 請求
$ curl -X POST -H "Content-Type: text/xml" -d @denglibing.txt 
 
//denglibing.txt:11622695,D58C6A25-C683-47D6-A18C-B7741284F632
二、例項
denglibingdeMacBook-Pro-4:~ denglibing$ curl 
[
  {
    "login": "mojombo",
    "id": 1,
    "avatar_url": "
    "gravatar_id": "",
    "url": "/mojombo",
    "html_url": "
    "followers_url": "/mojombo/followers",
    "following_url": "/mojombo/following{/other_user}",
    "gists_url": "/mojombo/gists{/gist_id}",
    "starred_url": "/mojombo/starred{/owner}{/repo}",
    "subscriptions_url": "/mojombo/subscriptions",
    "organizations_url": "/mojombo/orgs",
    "repos_url": "/mojombo/repos",
    "events_url": "/mojombo/events{/privacy}",
    "received_events_url": "/mojombo/received_events",
    "type": "User",
    "site_admin": false
  }
]

當然,既然這些請求是在 行中執行,完全可以寫成shell ,來處理一系列的工作,比如多個請求,而shell指令碼在Mac中,可以使用定時任務觸發,進而完成一系列的自動化工作。

原文來自: https://www.linuxprobe.com/shell-curl-linux.html

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69955379/viewspace-2871313/,如需轉載,請註明出處,否則將追究法律責任。

相關文章