2011-08-18 37 views

回答

3

你有什麼麻煩? JSON或POST?

對於JSON,自2.5版以來,json模塊已包含在Python中。只需執行json.dumps(my_data)即可將數據變量轉換爲JSON。

對於POST,標準庫中有各種模塊,但最好的選擇是安裝第三方庫requests

+1

+1「PIP安裝要求」

有使用與這兩種請求圖書館和其他GitHub的API幾個例子請求庫 –

0

這裏是我已經用了後,發送GET請求

import httplib 
connection = httplib.HTTPConnection('192.168.38.38:6543') 
body_content = 'abcd123456xyz' 
connection.request('POST', '/foo/bar/baa.html', body_content) 
postResult = connection.getresponse() 

connection.request('GET', '/foo/bar/baa.html') 
response = connection.getresponse() 
getResult = response.read() 

爲CLI的這個命令序列它做相同的:

curl -X POST -d "abcd123456xyz" 192.168.38.38:6543/foo/bar/baa.html 
curl 192.168.38.38:6543/foo/bar/baa.html 
+0

漂亮的代碼。它有什麼作用? – 2011-08-18 19:18:34

+0

在我的應用程序中,我比較了body_content和getResult以檢查數據是否在服務器上更新。 它的確如此: curl -X POST -d「abcd123456xyz」http://192.168.38.38:6543/foo/bar/baa.html 然後: curl http://192.168.38.38:6543/富/酒吧/ baa.html – mdob