使用Django发出HTTP请求并反序列化输出 [英] Making HTTP request with Django and deserializing output

查看:163
本文介绍了使用Django发出HTTP请求并反序列化输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对整个Web开发几乎都是全新的,但是已经被投入到使用Django来提取和解析来自Web服务的数据的侧面项目中,并且正在努力理解事情是如何工作的,即使在寻找通过Django文档。



在Django中,我已经完成了所有设置并在基本级别工作(使用模板,显示的页面显示Hello World)。



现在为了从webservice中提取数据,我需要向以下格式的URL发出请求:

  http://wbsapi.withings.net/ [service_name]?action = [action_name]& [parameters] 

在提供的PHP示例中,他们使用cURL执行此操作,然后执行json_decode。



我会是什么?从Django获得类似的功能?提前致谢!

解决方案

您可以使用 urllib2 json 标准模块(或者,优秀的图书馆请求 json ):

  import urllib2 
import json

url ='http://wbsapi.withings.net/[service_name]?action=[action_name]&[parameters]'
serialized_data = urllib2.urlopen(url ).read()

data = json.loads(serialized_data)

 



如果您想在页面上使用它,您需要它在视图,您需要与网址关联



您的 urls.py 将包含类似的内容

 来自django.conf.ur ls导入模式,网址,包括

urlpatterns = patterns('',
(r'^ get_data / $','myapp.views.get_data'),

您的 myapp / views.py 将包含来自django.http的HttpResponse
import urllib2
import json
$ b

$ b def get_data(request):
url ='http://wbsapi.withings.net/[service_name]?action=[action_name]&[parameters]'
serialized_data = urllib2.urlopen( url).read()

data = json.loads(serialized_data)

html =< html>< body>< pre>数据:%s。 < /预>< /体>< / HTML>中%json.dumps(data,indent = 2)

返回HttpResponse(html)

当然,我不知道您希望如何显示数据,所以我再次将其序列化:)


So I'm almost totally new to web development as a whole, but have been thrown into a side project using Django to pull and parse data from a web service, and am struggling to understand exactly how things work, even while looking through the Django documentation.

In Django, I have everything set up and working at a basic level (using templates, a page is displayed saying "Hello World").

Now in order to pull the data from the webservice, I need to make a request to a URL of the following format:

http://wbsapi.withings.net/[service_name]?action=[action_name]&[parameters]

In the provided PHP example, they do this using cURL, and then json_decode.

What would I do to get similar functionality out of Django? Thanks in advance!

解决方案

You would use urllib2 and json standard modules (or, alternatively, the excellent library requests and json):

import urllib2
import json

url = 'http://wbsapi.withings.net/[service_name]?action=[action_name]&[parameters]'
serialized_data = urllib2.urlopen(url).read()

data = json.loads(serialized_data)

 

If you want it on a page, you want it in a view, which you will need to associate with an url.

Your urls.py will contain something like

from django.conf.urls import patterns, url, include

urlpatterns = patterns('',
    (r'^get_data/$', 'myapp.views.get_data'),
)

And your myapp/views.py will contain something like

from django.http import HttpResponse
import urllib2
import json

def get_data(request):
    url = 'http://wbsapi.withings.net/[service_name]?action=[action_name]&[parameters]'
    serialized_data = urllib2.urlopen(url).read()

    data = json.loads(serialized_data)

    html = "<html><body><pre>Data: %s.</pre></body></html>" % json.dumps(data, indent=2)

    return HttpResponse(html)

Of course, I don't know how you wanted your data to be displayed, so I just serialized it again :)

这篇关于使用Django发出HTTP请求并反序列化输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆