简体   繁体   English

Flask:使用POST方法存储值并使用GET方法检索它

[英]Flask: store value with POST method and retrieve it with GET method

I've been working with Flask for quite a bit. 我和Flask一直在合作。

I have an output, just a number which increases or decreases constantly, I want to capture it with flask POST method and, retrieve immediately. 我有一个输出,只是一个不断增加或减少的数字,我想用烧瓶POST方法捕获它,并立即检索。

I've created an application to retrieve the latest POST in a GET method using the same context: 我创建了一个应用程序,使用相同的上下文在GET方法中检索最新的POST:

cumulative = ['x']
@app.route('/xxx', methods=['GET', 'POST'])
def refresh():
    aexample = request.get_json()
    cumulative.append(aexample)
    return str(cumulative[1]['thing2'])

It works, but if you refresh the page sometimes this error appears in the logs: 它可以工作,但如果你刷新页面有时这个错误会出现在日志中:

TypeError: 'NoneType' object is not subscriptable

In this line: 在这一行:

cumulative.append(aexample)

I've tried using: 我尝试过使用:

cumulative[0] = aexample

But that doesn't work, it says the value is "None". 但这不起作用,它说价值是“无”。 That's why I made it incremental (just for test purposes). 这就是为什么我增加它(仅用于测试目的)。

All of this makes me think, storing the latest POST values in a list isn't the smart way to do this. 所有这些让我觉得,将最新的POST值存储在列表中并不是一种聪明的方法。

I've been thinking on using some sort of cache, the posted value changes every minute and I would like to retrieve only the latest posted value. 我一直在考虑使用某种缓存,发布的值每分钟都会更改,我只想检索最新发布的值。 I'm not interested in storing the values permanently. 我对永久存储这些值不感兴趣。

Is there a good way to do this? 有没有办法做到这一点?

You're probably getting the error from missing request json, on a GET request (ie cumulative[1] is None , so you can't get ['thing2'] ). 你可能在GET请求中得到了缺少请求json的错误(即cumulative[1]None ,所以你不能得到['thing2'] )。

To persist between requests, and trusting your data size is not too large, you may be able to store it in session . 要在请求之间保持不变并且信任您的数据大小不是太大,您可以将其存储在session

Otherwise you may want to look at something more scalable like Redis 否则你可能想看看像Redis这样更具伸缩性的东西

Ok, first thanks to @dylanj.nz for pointing my error with the requests. 好的,首先要感谢@ dylanj.nz指出我的错误请求。

Finally I've achieved what I wanted, I've created conditionals for both requests, this is the code on flask: 最后我实现了我想要的,我已经为两个请求创建了条件,这是烧瓶上的代码:

# 'Some Kind of Real Time Stuff'
cumulative = ['x']
@app.route('/xxx', methods=['GET', 'POST'])
def refresh():
    aexample = request.get_json()
    if flask.request.method == 'POST':
        cumulative[0] = str(aexample)
        return 'data received'
    if flask.request.method == 'GET':
        return str(cumulative[0])

[ Using CURL to send data ] Now, sending data to the method: [使用CURL发送数据]现在,将数据发送到方法:

curl -XPOST -H "Content-Type: application/json" http://someProject/xxx -d '{"thing2":"sdpofkdsofk"}'

[ Apache - Log ]The POST were received successfully: [Apache - Log] POST收到成功:

myapacheserver:80 76.5.153.20 - - [23/Jul/2019:11:02:40 -0400] "POST /xxx HTTP/1.1" 200 182 "-" "curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3"

[ Apache - Log ] GET works! [Apache - Log] GET有效! presenting the latest value: 呈现最新价值:

myapacheserver:80 76.220.156.100 - - [23/Jul/2019:11:03:52 -0400] "GET /xxx HTTP/1.1" 200 327 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0"

This is displayed on the page http://myapacheserver/xxx : 这显示在页面http:// myapacheserver / xxx上

{'thing2': 'sdpofkdsofk'}

Yes, I'm storing the dict as an string, but it works, I'll deal later with the data type. 是的,我将dict存储为字符串,但它可以工作,稍后我会处理数据类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM