如何使用 Python 从 RESTful 服务获取 JSON 数据?

有没有使用 Python 从 RESTful 服务获取 JSON 数据的标准方法?

我需要使用 Kerberos 进行身份验证。

一些片段会有帮助。

178980 次浏览

Well first of all I think rolling out your own solution for this all you need is urllib2 or httplib2 . Anyways in case you do require a generic REST client check this out .

https://github.com/scastillo/siesta

However i think the feature set of the library will not work for most web services because they shall probably using oauth etc .. . Also I don't like the fact that it is written over httplib which is a pain as compared to httplib2 still should work for you if you don't have to handle a lot of redirections etc ..

I would give the requests library a try for this. Essentially just a much easier to use wrapper around the standard library modules (i.e. urllib2, httplib2, etc.) you would use for the same thing. For example, to fetch json data from a url that requires basic authentication would look like this:

import requests


response = requests.get('http://thedataishere.com',
auth=('user', 'password'))
data = response.json()

For kerberos authentication the requests project has the reqests-kerberos library which provides a kerberos authentication class that you can use with requests:

import requests
from requests_kerberos import HTTPKerberosAuth


response = requests.get('http://thedataishere.com',
auth=HTTPKerberosAuth())
data = response.json()

Something like this should work unless I'm missing the point:

import json
import urllib2
json.load(urllib2.urlopen("url"))

You basically need to make a HTTP request to the service, and then parse the body of the response. I like to use httplib2 for it:

import httplib2 as http
import json


try:
from urlparse import urlparse
except ImportError:
from urllib.parse import urlparse


headers = {
'Accept': 'application/json',
'Content-Type': 'application/json; charset=UTF-8'
}


uri = 'http://yourservice.com'
path = '/path/to/resource/'


target = urlparse(uri+path)
method = 'GET'
body = ''


h = http.Http()


# If you need authentication some example:
if auth:
h.add_credentials(auth.user, auth.password)


response, content = h.request(
target.geturl(),
method,
body,
headers)


# assume that content is a json reply
# parse content with the json module
data = json.loads(content)

If you desire to use Python 3, you can use the following:

import json
import urllib.request
req = urllib.request.Request('url')
with urllib.request.urlopen(req) as response:
result = json.loads(response.readall().decode('utf-8'))