python: server is not receiving input from client flask

Chris Angelico rosuav at gmail.com
Tue Jun 29 16:32:14 EDT 2021


On Wed, Jun 30, 2021 at 6:21 AM Jerry Thefilmmaker
<jerrythefilmmaker at gmail.com> wrote:
> @app.route("/check_answer/<ans>", methods = ['POST'])
> def check_answer(ans):
>
> <form action="check_answer/ans" method="POST">
>                         Enter your answer: <input type="text" name="answers">
>                          <input type="submit" value="submit">
>         </form>

What you're creating here is a route with a placeholder. The
check_answer function will handle POST requests to /check_answer/foo,
/check_answer/spam, /check_answer/barney, /check_answer/whatever. In
each case, the variable "ans" will be given the part of the URL that
<ans> represents - the string "foo" or "spam" or "barney" or whatever.
It's never going to carry a dictionary around.

I'll stress this again: *Your requests must be independently handled*.
You can't think of passing information from one request to another,
because there might not have been a previous request. Moving to the
web means thinking differently about things; you no longer have
control flow from one part of the program to another, you instead have
independent handlers that have to think exclusively about their own
requests.

This might mean carrying some extra information through the form. For
instance, you could include a question ID of some sort (using <input
type=hidden> for that purpose), and then you could have a global
constant with all of your prewritten questions. There are other ways
to carry information around, but they have to go via the request
itself; you can't keep variables from one function to another.

> Also, where do I see my print statement in this case? Mind you the codes worked fine before I attempted to use flask.

Hmm, that depends on how you're running your code. Ideally, you should
have a server running somewhere - a terminal window or equivalent -
and if you're using Flask's default server (werkzeug), it will be
reporting every request as it goes through. It might look something
like this:

INFO:geventwebsocket.handler:127.0.0.1 - - [2021-06-30 06:30:51] "GET
/ HTTP/1.1" 200 10267 1.569960
INFO:geventwebsocket.handler:127.0.0.1 - - [2021-06-30 06:30:52] "GET
/api/twitch_schedule?channelid=49497888 HTTP/1.1" 200 593 1.403456
INFO:geventwebsocket.handler:127.0.0.1 - - [2021-06-30 06:31:06] "GET
/ HTTP/1.1" 200 10267 1.724949
INFO:geventwebsocket.handler:127.0.0.1 - - [2021-06-30 06:31:07] "GET
/api/twitch_schedule?channelid=49497888 HTTP/1.1" 200 593 1.277206

Your print calls will insert messages into that log, prior to their
corresponding summary lines (the summary is written when you return a
response, so that's at the very end of your function).

ChrisA


More information about the Python-list mailing list