如何在flask web app中接受Xively发布的数据?

2024-04-20 00:41:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一个Flask web应用程序(托管在Heroku上),它将接受从Xively发布到它的数据点。你知道吗

下面的代码旨在获取数据点,对其进行修改(添加2),然后将其发送回xively提要:

import os
from flask import Flask
from flask import request
import xively


app = Flask(__name__)

@app.route('/', methods=['GET','POST'])
def take_xively_post_data():

    if request.method == 'POST':

        if not request.json or not 'current_value' in request.json:
            abort(400)
        else:
            key = 'f5vezDTq4VphhXyCn......'  
            feedid = '21094....'

            lev = request.json['current_value']

                    #the following code just adds 2 to the POSTed value and 
                    #sends it back to xively 

            lev = lev + 2

            client1 = xively.Client(key)  
            datastream = xively.Datastream(id="DatastreamID", current_value= lev)  

        return client1.put('/v2/feeds/'+feedid, data={'datastreams': [datastream]}

    else:
        return 'Hello'

但是当Xively发布到应用程序的URL时,什么也没有发生。(它应该将修改后的数据发送回Xively提要。)

Heroku日志显示500个错误:

2014-05-29T14:00:55.844275+00:00 heroku[router]: at=info method=POST path=/ host=....herokuapp.com request_id=... fwd="..." dyno=web.1 connect=1ms service=17ms status=500 bytes=454

我认为代码有问题,但不知道是什么。。。你知道吗

我尝试过将client1.put(…)语句向上移动,但没有返回任何内容,如下所示,但发生了相同的情况:

import os
from flask import Flask
from flask import request
import xively


app = Flask(__name__)

@app.route('/', methods=['GET','POST'])
def take_xively_post_data():

    if request.method == 'POST':

        if not request.json or not 'current_value' in request.json:
            abort(400)
        else:
            key = 'f5vezDTq4VphhXyCn......'  
            feedid = '21094....'

            lev = request.json['current_value']

            lev = lev + 2

            client1 = xively.Client(key)  
            datastream = xively.Datastream(id="DatastreamID", current_value= lev)  

                    #moved the client1.put function up and returning nothing

            client1.put('/v2/feeds/'+feedid, data={'datastreams': [datastream]}

        return
    else:
        return 'Hello'

我试过了申请表()而不是请求.json()但这似乎也不管用。你知道吗

如果有人能指出编码错误,我将不胜感激。你知道吗


Tags: fromimportjsonappflaskdataifvalue
1条回答
网友
1楼 · 发布于 2024-04-20 00:41:07

不知道你是否解决了你的问题,但我想我能帮上忙。你知道吗

您请求当前值的方式很可能不正确。这将导致你的钥匙找不到,这肯定会通过一个500。你知道吗

我假设您正在使用Xively触发器来发出POST请求。如果是这样的话,从lev = request.json['current_value']改为lev = request.json['triggering_datastream']['value']['value']开始。这至少应该为您提供发送触发器的数据流的实际当前值。你知道吗

然后当然要把你的return值改成200这样的值,就像其他人建议的那样,没有身体。你知道吗

看看这是否有效,然后我们可以试着调试把你送回Xively。在我看来那代码很正确。你知道吗

相关问题 更多 >