Is there any standard way of getting JSON data from RESTful service using Python?
有使用Python从RESTful服务获取JSON数据的标准方法吗?
I need to use kerberos for authentication.
我需要使用kerberos进行身份验证。
some snippet would help.
一些片段会有所帮助。
5 个解决方案
#1
72
Something like this should work unless I'm missing the point:
像这样的东西应该有用,除非我没抓住要点:
import json
import urllib2
json.load(urllib2.urlopen("url"))
#2
106
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:
我想尝试一下请求库。基本上,在标准库模块(例如urllib2、httplib2等)周围使用包装器要容易得多,您也可以使用它们。例如,从需要基本身份验证的url获取json数据的方式如下:
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:
对于kerberos身份验证,请求项目具有reqester -kerberos库,该库提供一个kerberos身份验证类,您可以在请求时使用:
import requests
from requests_kerberos import HTTPKerberosAuth
response = requests.get('http://thedataishere.com',
auth=HTTPKerberosAuth())
data = response.json()
#3
24
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:
您基本上需要向服务发出HTTP请求,然后解析响应的主体。我喜欢用httplib2:
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)
#4
7
If you desire to use Python 3, you can use the following:
如果您希望使用Python 3,可以使用以下内容:
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'))
#5
3
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 .
首先,我想你们需要的是urllib2或httplib2。无论如何,如果您确实需要一个通用的REST客户端,请检查一下。
https://github.com/scastillo/siesta
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 ..
但是,我认为库的特性集对大多数web服务都不起作用,因为它们可能会使用oauth等。我也不喜欢它是写在httplib上的这和httplib相比很痛苦如果你不需要处理很多重定向,b2仍然适用。
#1
72
Something like this should work unless I'm missing the point:
像这样的东西应该有用,除非我没抓住要点:
import json
import urllib2
json.load(urllib2.urlopen("url"))
#2
106
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:
我想尝试一下请求库。基本上,在标准库模块(例如urllib2、httplib2等)周围使用包装器要容易得多,您也可以使用它们。例如,从需要基本身份验证的url获取json数据的方式如下:
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:
对于kerberos身份验证,请求项目具有reqester -kerberos库,该库提供一个kerberos身份验证类,您可以在请求时使用:
import requests
from requests_kerberos import HTTPKerberosAuth
response = requests.get('http://thedataishere.com',
auth=HTTPKerberosAuth())
data = response.json()
#3
24
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:
您基本上需要向服务发出HTTP请求,然后解析响应的主体。我喜欢用httplib2:
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)
#4
7
If you desire to use Python 3, you can use the following:
如果您希望使用Python 3,可以使用以下内容:
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'))
#5
3
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 .
首先,我想你们需要的是urllib2或httplib2。无论如何,如果您确实需要一个通用的REST客户端,请检查一下。
https://github.com/scastillo/siesta
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 ..
但是,我认为库的特性集对大多数web服务都不起作用,因为它们可能会使用oauth等。我也不喜欢它是写在httplib上的这和httplib相比很痛苦如果你不需要处理很多重定向,b2仍然适用。