Python休息消耗

Python休息消耗,python,django,python-2.7,python-3.x,urllib,Python,Django,Python 2.7,Python 3.x,Urllib,我开始扩展我的python知识,并尝试使用RESTAPI(例如,使用带有XML有效负载的http POST请求从数据库更新和接收数据)。我很清楚如何使用RESTAPI,但有点不确定具体针对Python使用什么库 urllib是正确的选择吗?请求模块django(我对此完全不了解) 这不是一个充满观点的主观回答,而是一个简单的介绍,以及如何结合RESTAPI使用urllib(或其他)的正确方向 如何使用Python使用REST服务?您好,我假设通过说明REST消费您打算使用REST api作为客户

我开始扩展我的python知识,并尝试使用RESTAPI(例如,使用带有XML有效负载的http POST请求从数据库更新和接收数据)。我很清楚如何使用RESTAPI,但有点不确定具体针对Python使用什么库

urllib
是正确的选择吗?
请求
模块
django
(我对此完全不了解)

这不是一个充满观点的主观回答,而是一个简单的介绍,以及如何结合RESTAPI使用
urllib
(或其他)的正确方向


如何使用Python使用REST服务?

您好,我假设通过说明
REST消费
您打算使用REST api作为客户端(执行GET请求或推送)

这可能是我个人的偏好,但我总是使用库来进行http调用

一旦得到响应,根据结果的类型,我将使用内置或

使用返回JSON结果的RESTAPI非常好,因为JSON可以很容易地解码(加载)到python字典中。(python字典具有与json相同的结构-排序)

我将给您一个带有JSON响应的
GET
请求的示例,因为它更容易,但您也可以找到
POST
请求GET的示例

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 

您好,我假设通过声明
REST消费
您打算使用restapi作为客户端(执行GET请求或PUSH)

这可能是我个人的偏好,但我总是使用库来进行http调用

一旦得到响应,根据结果的类型,我将使用内置或

使用返回JSON结果的RESTAPI非常好,因为JSON可以很容易地解码(加载)到python字典中。(python字典具有与json相同的结构-排序)

我将给您一个带有JSON响应的
GET
请求的示例,因为它更容易,但您也可以找到
POST
请求GET的示例

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和请求都将完成这项工作。如果它只是一个web服务API,只需进行http调用

对于JSON响应,requests模块是更好的选择,因为urllib2没有提供请求具有的本机JSON序列化程序。对于XML响应,必须使用外部XML解析器(如minidom)。关于进行http调用,请求和urllib2实际上没有那么大的不同。这里有一个比较()但实际上它们是可以互换的


Django是一个web服务框架,处于完全不同的级别。Django应用程序既可以是RESTAPI的服务提供者,也可以是客户端,但Django不是本机附带的。您仍然需要使用其他工具构建功能。您可以使用django rest框架构建自己的rest API,也可以使用与请求相同的方式调用第三方。

urllib2和请求都可以完成这项工作。如果它只是一个web服务API,只需进行http调用

对于JSON响应,requests模块是更好的选择,因为urllib2没有提供请求具有的本机JSON序列化程序。对于XML响应,必须使用外部XML解析器(如minidom)。关于进行http调用,请求和urllib2实际上没有那么大的不同。这里有一个比较()但实际上它们是可以互换的


Django是一个web服务框架,处于完全不同的级别。Django应用程序既可以是RESTAPI的服务提供者,也可以是客户端,但Django不是本机附带的。您仍然需要使用其他工具构建功能。您可以使用django rest框架构建您自己的rest API,或者以与请求相同的方式调用第三方。

有许多python库可用于进行rest调用,其中著名的是请求

示例获取呼叫

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()
邮电样本

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()

您可以查看此链接以了解更多详细信息

有许多python库可用于进行REST调用,其中著名的是请求

示例获取呼叫

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()
邮电样本

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()

您可以查看此链接以了解更多详细信息

对于REST客户端,我也会推荐请求,我会对biobirdman的回答发表评论,但没有足够的代表

如果您使用的是json,则不需要导入json模块,您可以发送一个直接的dict对象并将json响应解析回dict,如下所示:例如

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()

希望这能有所帮助。

对于REST客户机,我也会推荐请求,我会对biobirdman的回答发表评论,但没有足够的代表

如果您使用的是json,则不需要导入json模块,您可以发送一个直接的dict对象并将json响应解析回dict,如下所示:例如

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()

希望能有帮助。

这有帮助吗?@PaulRooney不是特别有帮助,因为我仍然不确定如何使用图书馆来消费它。你说的“消费”是什么意思?你能更具体地说明你想做什么吗?你应该能够使用
请求。获取
请求。发布
等。你需要什么?做一个http调用并处理响应或其他事情?这有帮助吗?@PaulRooney并不特别,因为我仍然不确定如何使用库来使用它。你说的“使用”是什么意思?你能更具体地说明你想做什么吗?你应该能够使用
请求。获取
请求。发布
等。你需要什么?进行http调用并处理响应还是其他?