I have an url
http://127.0.0.1:5000/feed/arm1?camera_id=1&command=update&value=3
.
I have another url
http://127.0.0.1:5000/wait/arm1
I want to get the params of the second url when I request the first url. How can I do it?
I have an url
http://127.0.0.1:5000/feed/arm1?camera_id=1&command=update&value=3
.
I have another url
http://127.0.0.1:5000/wait/arm1
I want to get the params of the second url when I request the first url. How can I do it?
Based on your clarifications, here is a response which
/feed/<entity>
into a database/wait/<entity>
from that databaseSince you only asked for the server side I didn't include proper JavaScript code or anything to do the polling on the client calling /wait
, instead it uses meta http-equiv="refresh"
. The code also uses records for its brevity – you could also just write to a file or come up with another idea of persisting your data. This is just one example.
Without further ado, here is the code (explanations below -- I left out the doc strings to keep it concise):
from flask import Flask, request
from records import Database
app = Flask(__name__)
def db():
return Database('sqlite:///camera_commands.sqlite')
@app.route('/feed/<entity>')
def feed(entity):
args = ['camera_id', 'command', 'value']
result = db().query('INSERT INTO commands (entity, camera_id, command, value) '
'VALUES (:entity, :camera_id, :command, :value)',
entity=entity,
camera_id=int(request.args.get('camera_id')),
command=request.args.get('command'),
value=int(request.args.get('value')))
return 'OK'
@app.route('/wait/<entity>')
def wait(entity):
result = db().query('SELECT * FROM commands WHERE entity = :entity ORDER BY id DESC LIMIT 1', entity=entity)
output = result.export('json')
return f"""\
<!DOCTYPE html>
<html>
<head>
<title>Wait</title>
<meta http-equiv="refresh" content="1"/>
</head>
<body>
<pre>{output}</pre>
</body>
</html>
"""
def setup_db(db):
db.query('DROP TABLE IF EXISTS commands')
db.query('CREATE TABLE commands (id INTEGER PRIMARY KEY, entity TEXT, camera_id INTEGER, command TEXT, value INTEGER)')
if __name__ == '__main__':
setup_db(db())
app.run()
app
as usual.db
function returns a new database connection to a local sqlite database (it creates the file camera_commands.sqlite
if it doesn't exist).feed
function stores the arguments to /feed/<entity>
inside the database and returns OK
. Notice how I use prepared statements here!wait
function retrieves the latest command and returns it inside a simple html website, which reloads itself every second.setup_db
function clears the sqlite file and creates the table.When running your program, open http://127.0.0.1:5000/wait/arm1 (and optionally http://127.0.0.1:5000/wait/arm2) in your browser. Now call e.g.
http://127.0.0.1:5000/feed/arm1?camera_id=1&command=update&value=3 or http://127.0.0.1:5000/feed/arm1?camera_id=1&command=start&value=1 etc.
Your browser tab with /wait/arm1 receives the updates while /wait/arm2 stays silent. When you change the target of the feed, e.g. to http://127.0.0.1:5000/feed/arm2?camera_id=1&command=update&value=5, /wait/arm1 does not change but /wait/arm2 does.
Of course, all of this is not fleshed out but it should give you enough ideas on how to persist data on the server and poll it on a client.