python urllib2 http get、post請求的區別

來源:互聯網
上載者:User

標籤:參數   需要   imp   round   margin   clu   break   xxxx   white   

python urllib2 實現HTTP 的GET POST 請求python 一般可以用模組urllib2 來實現GET POST的http 請求GET  請求
  1.      import os,sys

  2. import os,sys

  3. import urllib2

  4. http_str = 'http://127.0.0.1:12345/apps/' + serviceLine + '/clusters/' + clusterName

  5. url = http_str + '/machine_info'

  6. req = urllib2.Request(url) # url 轉換成發起get 請求的url

  7. result = urllib2.urlopen(req) # 發起GET http服務

  8. res = result.read() #把結果通過.read()函數讀取出來

  9. slave_info = json.loads(res) #把返回結果(string) 轉換成json,方便處理,

  10. print slave_info['content']['slave'] # 列印json 串的dict 資訊


POST 請求 【post 跟get 去請求的區別是部分參數寫在程式裡面,不是在http 請求裡面】

  1.      import os,sys

  2. import os,sys

  3. import urllib2

  4. http_str = 'http://127.0.0.1:12345/apps/' + serviceLine + '/clusters/' + clusterName

  5. url1 = http_str + '/ops/rpm_update' # url 部分

  6. params1 = {"app_rpm_id":"-1","rpm_xxx":rpmxxxxx} #參數部分

  7. params = json.dumps(params1) # http 伺服器要求傳入的params 必須是json 對象,所以這裡需要轉換一下;如果http 伺服器沒有要求,就可以不轉換

  8.       

  9. response = urllib.urlopen(url1,params) #發送POST 請求 print response # 因為http 伺服器已經規定好返回的結果就是一個json 對象,所以不用再轉換成json;

  10. print response.read() # 把結果資料通過read()讀出來;如果http返回的不是json 對象,還需要轉換成json 對象


python urllib2 http get、post請求的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.