-1

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?

Arghya Saha
  • 5,599
  • 4
  • 26
  • 48
  • I'd like to help but I do not get your point? Requests are stateless. Still you want to get something of the first request while the second is processed. If yes, then this should be handled by the client, not the server. – M.Rau Aug 13 '18 at 08:10
  • If I understand it right that you want to redirect from `feed` to `wait`, then there are a few options, for example https://stackoverflow.com/a/18102521 and https://stackoverflow.com/a/17061023. – Sebastian Höffner Aug 13 '18 at 08:46
  • maybe i describe it unclearly "feed": i can request by using browser "wait": i am waiting here if "feed" transfer message i can receive it by "wait" if get_message_by_feeding() is not None: wait_to_get_message and do something else: do nothing can you help me ,thank you very much – chengkaibing521 Aug 13 '18 at 10:55
  • So do you mean you have one URL (feed) where the parameters are sent to by one client (your camera), and one URL (wait) where the parameters are retrieved? – Sebastian Höffner Aug 13 '18 at 11:16
  • yeah,yeah,yeah i sent my params by using browser at the same time one client are waiting here if i wait something i will do something so i want to know how to write server code – chengkaibing521 Aug 13 '18 at 11:23

1 Answers1

0

Based on your clarifications, here is a response which

  • persists data received via /feed/<entity> into a database
  • retrieves the data via /wait/<entity> from that database

Since 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()
  1. We create a Flask app as usual.
  2. The db function returns a new database connection to a local sqlite database (it creates the file camera_commands.sqlite if it doesn't exist).
  3. The feed function stores the arguments to /feed/<entity> inside the database and returns OK. Notice how I use prepared statements here!
  4. The wait function retrieves the latest command and returns it inside a simple html website, which reloads itself every second.
  5. The setup_db function clears the sqlite file and creates the table.
  6. Inside the main section the database is set up and the app started.

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.

Sebastian Höffner
  • 1,864
  • 2
  • 26
  • 37
  • maybe i describe it unclearly "feed": i can request by using browser "wait": i am waiting here if "feed" transfer message i can receive it by "wait" if get_message_by_feeding() is not None: wait_to_get_message and do something else: do nothing can you help me ,thank you very much – chengkaibing521 Aug 13 '18 at 10:32
  • I updated my answer according to you clarifications, does it help you now? – Sebastian Höffner Aug 15 '18 at 15:07