python调用restapi
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "application/json","Content-type":"application/xml; charset=utf=8"}
conn = httplib.HTTPConnection("www.xxx.com")
conn.request("GET","/get?id=1","",headers)
response = conn.getresponse()
print "Return code:", response.status, " reason:", response.reason
print response.status
if response.status == 200:
usersdata = response.read()
user = json.loads(usersdata)
print user[0]
else:
print "Error sending message,check your account"
json解码方法如下
import json
json_class = json.JSONDecoder()
a = json_class.raw_decode(jstr)
print a
b = json_class.decode(jstr)
print b
c = json.loads(jstr)
print c