[Python] Input in flask route

Discussão em 'Python' iniciado por Stack, Agosto 29, 2025.

  1. Stack

    Stack Membro Participativo

    I am using Flask route to get an user information.

    I wan to use input function or somethink else to retrieve a answer from a user in the terminal.

    Here is the server I use:

    def ask_input(prompt, result_queue):
    try:
    result = input(prompt)
    result_queue.put(result)
    except Exception as e:
    result_queue.put(None) # Error EOFError('EOF when reading a line')
    class FakeInterface:
    def __init__(self):
    self.app = Flask(__name__)
    @self.app.route('/ihm_notification', methods=["POST"])
    def __ihm_notification():
    try:
    prompt = "ok: y/n?"
    timeout = 10

    q = queue.Queue()
    t = threading.Thread(target=ask_input, args=(prompt, q))
    t.daemon = True
    t.start()

    try:
    return q.get(timeout=timeout) # attend max <timeout> secondes
    except queue.Empty:
    return None

    except TimeoutOccurred:
    print(f"User answer too long")

    except Exception as error:
    print("error")

    def run(self, host: str, port: int):
    self.app.run(host=host, port=port)
    fi = FakeInterface()
    fi.run()


    I get the following error: EOFError('EOF when reading a line')

    Edit: I also try to do it with websocket:

    @socketio.on('message')
    def message(data):
    try:
    from inputimeout import inputimeout, TimeoutOccurred
    reponse = input_with_timeout(prompt="ezzer", timeout=5)
    emit('response', {'from': reponse})

    except Exception:
    emit('response', {'from': 'nono'})

    if __name__ == '__main__':
    socketio.run(app, port=8000, debug=True, allow_unsafe_werkzeug =True)
    time.sleep(15)


    When I try with a client, when I set the input before the timeout, the server work properly. But when I forget to set input, the next responses are always "nono" like the input function is stuck somewhere

    Continue reading...

Compartilhe esta Página