代码之家  ›  专栏  ›  技术社区  ›  Brandon Deo

Python REST消费

  •  1
  • Brandon Deo  · 技术社区  · 9 年前

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

    urllib 怎么走?这个 requests 单元 django (我完全不知道)?

    这不是一个充满意见的主观答案,而是一个简单的介绍和正确的使用方向 乌里布 (或其他)结合REST API。

    如何使用Python使用REST服务?

    4 回复  |  直到 9 年前
        1
  •  3
  •   gvir    9 年前

    有许多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()
    

    您可以查看此链接 http://stackandqueue.com/?p=75 有关详细信息

        2
  •  1
  •   biobirdman    9 年前

    嗨,我假设通过陈述 REST Consumption 您打算使用REST api作为客户端(执行GET请求或PUSH)

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

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

    使用返回JSON结果的REST API非常棒,因为JSON可以很容易地解码(加载)到python字典中。(python字典的结构与json-sort-of相同)

    我将用一个 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 
    
        3
  •  0
  •   Community CDub    7 年前

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

    requests模块是JSON响应的更好选择,因为urllib2没有提供请求所具有的本地JSON序列化程序。对于XML响应,您必须使用外部XML解析器(如minidom)。关于进行http调用,请求和urllib2实际上没有太大区别。这里有一个比较( What are the differences between the urllib, urllib2, and requests module? )但实际上它们是可以交换的。

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

        4
  •  0
  •   Stuart Grierson    6 年前

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

    希望有帮助。