標籤:openstack requests python 用戶端 linux curl
提交http請求之python與curl
由於Openstack是python實現wsgi的REST ful架構,在學習和調試的過程中,常常會遇到http請求的提交,於是順手整理下python和curl命令的提交方法。
1.Python篇
在python中有過爬蟲經驗當然很簡單,一個requests庫的問題,比urllib這些好用。
import requestsurl="https://www.baidu.com/s"headers={‘user-agent‘:"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36"}post_data={‘username‘:"qujun","passwd":"xixi"}parameters={‘wd‘:"abc"}#提交get請求P_get=request.get(url,params=parameters)#提交post請求P_post=reuests.post(url,headers=headers,data=post_data)
如果涉及到cookies的話,可以使用requests.Seesion()方法
2.shell中curl
-A:隨意指定自己這次訪問所宣稱的自己的瀏覽器資訊-b/--cookie <name=string/file> cookie字串或檔案讀取位置,使用option來把上次的cookie資訊追加到http request裡面去。-c/--cookie-jar <file> 操作結束後把cookie寫入到這個檔案中-d/ --data <data> HTTP POST方式傳送-F/--form <name=content> 類比http表單提交資料-H/--header <header> 指定要求標頭參數-s/--slient 減少輸出的資訊,比如進度-v/--verbose 小寫v參數,用於列印更多資訊,包括髮送的請求資訊,這在調試指令碼是特別有用。-o/--output <file> 指定輸出檔案名稱--retry <num> 指定重試次數-x/--proxy <proxyhost[:port]> 指定Proxy 伺服器地址和連接埠,連接埠預設為1080--connect-timeout <seconds> 指定嘗試串連的最大時間長度
#curl發送get,curl請求本身屬於get,也沒找到指定發送參數的參數。。curl https://www.baidu.com/s?wd=xixi#curl發送post,curl發送post有兩個參數-d和-F跟據man的結果,區別在於:-d 效果相當與在HTML form填好了資訊按下了submit鍵,他會使用content-type “application/x-www-form-urlencoded”,按照它的使用例子,應該是普通內容post過去。-F 類比使用者按下submit鍵後傳輸填好的form。使用Content-Type multi‐part/form-data,按照它的例子一般是在傳送檔案的時候使用。curl -d "username=qujun&passwd=xixi" https://www.baidu.comcurl -F "[email protected];type=text/html" example.comcurl -F [email protected] https://example.com/upload.cgi
本文出自 “DanielQu” 部落格,請務必保留此出處http://qujunorz.blog.51cto.com/6378776/1871276
Openstack命令列用戶端類比HTTP請求