一、初識curl
curl 是一個功能強大的命令行工具,用于傳輸數據,支持多種協議(如 HTTP、HTTPS、FTP 等)。
分析以下curl:
curl "https://$HOST/mon/adm/au/opera" --header "Authorization: $AUTH" -X POST -H 'Content-Type: application/json' --data '{"reportFormUid":"01", "operationType":"mute", "action": "ON"}'
這是一個典型的 ?HTTPS POST 請求?,用于向服務器發送 JSON 格式的操作指令。
1. 請求基礎信息?
?URL?:
https://$HOST/mon/adm/au/opera
$HOST
?是變量,需替換為實際主機地址(如?api.example.com
)。- 路徑?
/mon/adm/au/opera
?可能指向管理后臺的某個操作接口。
?方法?:
POST
明確指定為 POST 請求,通常用于提交數據。
2. 請求頭(Headers)?
- ?
Authorization: $AUTH
?- 用于
Authorization(授權)
身份驗證,$AUTH
?需替換為有效的憑證(如?Bearer token
?或?Basic base64編碼
)。
- 用于
- ?
Content-Type: application/json
?
聲明請求體為 JSON 格式。
3. 請求體(Body)?
- ?JSON 數據?:
{"reportFormUid":"01", "operationType":"mute", "action": "ON"}
reportFormUid: "01"
:可能標識操作目標的表單或資源。operationType: "mute"
:操作類型為“靜音”。action: "ON"
:執行開啟靜音的操作。
4.一些細節
同樣是發送 JSON 數據:
1、curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://api.example.com
2、curl "https://$HOST/mon/adm/au/opera" --header "Authorization: $AUTH" -X POST -H 'Content-Type: application/json' --data '{"reportFormUid":"01", "operationType":"mute", "action": "ON"}'
5. 文件操作?
?下載文件?:
curl -O https://example.com/file.zip
-O
?使用服務器原始文件名保存。?斷點續傳?:
curl -C - -O https://example.com/largefile.zip
-C -
?自動恢復未完成的下載。?上傳文件?:
curl -F "file=@localfile.txt" https://example.com/upload
-F
?用于表單文件上傳。
6. 調試與高級功能?
?顯示請求詳情?:
curl -v https://example.com
-v
?輸出請求和響應的頭部信息。?跟隨重定向?:
curl -L https://example.com/short-url
-L
?自動跳轉。?使用代理?:
curl -x http://proxy.example.com:8080 https://target.com
-x
?指定代理服務器。僅獲取響應頭?:
curl -I https://example.com
-I
?只顯示 HTTP 頭部。?監控網站狀態?(狀態碼):
curl -s -o /dev/null -w "%{http_code}" https://example.com
返回 HTTP 狀態碼。
7. 認證與安全?
?Basic 認證?:
curl -u username:password https://secure.com
-u
?傳遞用戶名和密碼。?忽略 SSL 證書驗證?:
curl -k https://self-signed.example.com
-k
?跳過證書檢查(不推薦生產環境使用)。
二、curl信息轉化為python發送請求
import requestsurl = "https://HOST/mon/adm/au/opera"
headers = {"Authorization": "AUTH_TOKEN","Content-Type": "application/json"
}
data = {"reportFormUid": "01","operationType": "mute","action": "ON"
}response = requests.post(url, headers=headers, json=data)
#response = requests.request(url, headers=headers, json=data)
print(response.status_code, response.text)