Python REST 消费
Python REST Consumption
我开始扩展我的 python 知识并尝试使用(例如,使用具有 XML 有效负载的 http POST 请求更新和从数据库接收数据)REST API。我很清楚如何使用 REST API,但我有点不确定具体要使用哪些库 Python。
urllib
是正确的选择吗? requests
模块? django
(我对此完全天真)?
这不是一个充满意见的主观答案,而是一个简单的介绍和关于如何将 urllib
(或其他)与 REST 结合使用的正确方向的要点 API.
如何使用 Python 的 REST 服务?
您好,我假设通过声明 REST Consumption
您打算使用 REST api 作为客户端(执行 GET 请求或 PUSH)
这可能是我个人的喜好,但我总是使用 requests 库来进行 http 调用
收到回复后,根据结果的类型,我将使用 built-in json library or with beautifulsoup
来解析它
使用 REST API returns JSON 结果很棒,因为 json 可以轻松解码(加载)到 python 字典中。 (python 字典与 json 具有相同的结构 - 有点)
我将给你一个 GET
请求和 JSON 响应的例子,因为它更容易,但你也可以轻松找到 POST
请求的例子
import requests
import json
dest = 'https://github.com/timeline.json'
## make the get request and store response
res = requests.get(dest)
## you might want to check if respond code is correct by accessing attribute status_code
## Get the body of the respond as text
body = res.text
## load json string ( text ) into dictionary
res_dict = json.loads(body)
## json is now a dict
assert 'messages' in res_dict
assert 'documentation_url' in res_dict
urllib2 和 requests 都可以完成工作。如果它只是一个 web 服务 API,只需进行一个 http 调用即可。
requests 模块是 JSON 响应的更好选择,因为 urllib2 没有 requests 自带的 JSON 序列化器。对于 XML 响应,您将不得不使用外部 XML 解析器(例如 minidom)。关于进行 http 调用,requests 和 urllib2 确实没有太大区别。比较在这里(What are the differences between the urllib, urllib2, and requests module?)但实际上它们是可以互换的。
Django 是一个 Web 服务框架,处于完全不同的层次。 Django 应用程序既可以是 REST APIs 的服务提供者,也可以是客户端,但 Django 本身并不自带。您仍然需要使用其他工具构建功能。您可以使用 django-rest-framework 构建您自己的 REST API 或以与请求相同的方式调用第 3 方。
您可以使用许多 python 库来进行 REST 调用,其中著名的是 Requests。
示例 GET 调用
import requests
def consumeGETRequestSync():
params = {'test1':'param1','test2':'param2'}
url = 'http://httpbin.org/get'
headers = {"Accept": "application/json"}
# call get service with headers and params
response = requests.get(url, headers = headers,data = params)
print "code:"+ str(response.status_code)
print "******************"
print "headers:"+ str(response.headers)
print "******************"
print "content:"+ str(response.text)
consumeGETRequestSync()
样本POST调用
import requests
def consumePOSTRequestSync():
params = {'test1':'param1','test2':'param2'}
url = 'http://httpbin.org/post'
headers = {"Accept": "application/json"}
# call post service with headers and params
response = requests.post(url,headers= headers,data = params)
print "code:"+ str(response.status_code)
print "******************"
print "headers:"+ str(response.headers)
print "******************"
print "content:"+ str(response.text)
# call
consumePOSTRequestSync()
您可以查看此 link http://stackandqueue.com/?p=75 了解更多详情
对于 REST 客户端,我也会推荐请求,我会把它作为对 biobirdman 的回答的评论,但没有足够的代表。
如果您正在使用 json,则不需要导入 json 模块,您可以发送一个直接的 dict 对象并将 json 响应解析回一个像这样听写:例如
import requests
uri = 'https://myendpoint/get_details_from_id'
json_body={'id' : '123', 'attribute' : 'first_name'}
resp = requests.post(uri,
json=json_body)
response_as_dict=resp.json()
希望对您有所帮助。
我开始扩展我的 python 知识并尝试使用(例如,使用具有 XML 有效负载的 http POST 请求更新和从数据库接收数据)REST API。我很清楚如何使用 REST API,但我有点不确定具体要使用哪些库 Python。
urllib
是正确的选择吗? requests
模块? django
(我对此完全天真)?
这不是一个充满意见的主观答案,而是一个简单的介绍和关于如何将 urllib
(或其他)与 REST 结合使用的正确方向的要点 API.
如何使用 Python 的 REST 服务?
您好,我假设通过声明 REST Consumption
您打算使用 REST api 作为客户端(执行 GET 请求或 PUSH)
这可能是我个人的喜好,但我总是使用 requests 库来进行 http 调用
收到回复后,根据结果的类型,我将使用 built-in json library or with beautifulsoup
来解析它使用 REST API returns JSON 结果很棒,因为 json 可以轻松解码(加载)到 python 字典中。 (python 字典与 json 具有相同的结构 - 有点)
我将给你一个 GET
请求和 JSON 响应的例子,因为它更容易,但你也可以轻松找到 POST
请求的例子
import requests
import json
dest = 'https://github.com/timeline.json'
## make the get request and store response
res = requests.get(dest)
## you might want to check if respond code is correct by accessing attribute status_code
## Get the body of the respond as text
body = res.text
## load json string ( text ) into dictionary
res_dict = json.loads(body)
## json is now a dict
assert 'messages' in res_dict
assert 'documentation_url' in res_dict
urllib2 和 requests 都可以完成工作。如果它只是一个 web 服务 API,只需进行一个 http 调用即可。
requests 模块是 JSON 响应的更好选择,因为 urllib2 没有 requests 自带的 JSON 序列化器。对于 XML 响应,您将不得不使用外部 XML 解析器(例如 minidom)。关于进行 http 调用,requests 和 urllib2 确实没有太大区别。比较在这里(What are the differences between the urllib, urllib2, and requests module?)但实际上它们是可以互换的。
Django 是一个 Web 服务框架,处于完全不同的层次。 Django 应用程序既可以是 REST APIs 的服务提供者,也可以是客户端,但 Django 本身并不自带。您仍然需要使用其他工具构建功能。您可以使用 django-rest-framework 构建您自己的 REST API 或以与请求相同的方式调用第 3 方。
您可以使用许多 python 库来进行 REST 调用,其中著名的是 Requests。
示例 GET 调用
import requests
def consumeGETRequestSync():
params = {'test1':'param1','test2':'param2'}
url = 'http://httpbin.org/get'
headers = {"Accept": "application/json"}
# call get service with headers and params
response = requests.get(url, headers = headers,data = params)
print "code:"+ str(response.status_code)
print "******************"
print "headers:"+ str(response.headers)
print "******************"
print "content:"+ str(response.text)
consumeGETRequestSync()
样本POST调用
import requests
def consumePOSTRequestSync():
params = {'test1':'param1','test2':'param2'}
url = 'http://httpbin.org/post'
headers = {"Accept": "application/json"}
# call post service with headers and params
response = requests.post(url,headers= headers,data = params)
print "code:"+ str(response.status_code)
print "******************"
print "headers:"+ str(response.headers)
print "******************"
print "content:"+ str(response.text)
# call
consumePOSTRequestSync()
您可以查看此 link http://stackandqueue.com/?p=75 了解更多详情
对于 REST 客户端,我也会推荐请求,我会把它作为对 biobirdman 的回答的评论,但没有足够的代表。
如果您正在使用 json,则不需要导入 json 模块,您可以发送一个直接的 dict 对象并将 json 响应解析回一个像这样听写:例如
import requests
uri = 'https://myendpoint/get_details_from_id'
json_body={'id' : '123', 'attribute' : 'first_name'}
resp = requests.post(uri,
json=json_body)
response_as_dict=resp.json()
希望对您有所帮助。