2011-08-18 92 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