Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data passing app in falcon python

Before asking the question i want to mention that i am aware of the fact that i can use django instead to make the app, but i need to use falcon and nothing else.

i am just looking for an approach

lets take a very simple scenario so that i can understand how data flows between various parts of the app.

i have a simple login page using html:

<!DOCTYPE html>
<html>
<body>

<form action="***what-do-i-put-here***">
  <fieldset>
    <legend>Personal information:</legend>
    First name:<br>
    <input type="text" name="firstname" value="Mickey">
    <br>
    Last name:<br>
    <input type="text" name="lastname" value="Mouse">
    <br><br>
    <input type="submit" value="Submit">
  </fieldset>
</form>

</body>
</html>

i run it using simpleHTTpServer present by default in python.

now i create a very basic falcon app with just one responder "on_post()" which just replies back with the data that it recieved from the form,

i am using uWsgi on localserver to host my falcon app. how do i the make these two different pieces of code to interact with each other i mean in the html form ,what we do in case of Php is we define the name of php file under "actions" tag .how do we do this in falcon.

a very simple and small working example is highly appreciated

like image 562
kshitij singh Avatar asked Jan 07 '23 21:01

kshitij singh


1 Answers

Here is a working example!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="http://127.0.0.1:8000" method="post">
        <input type="text" name="name">
        <button type="submit" name="btn">Submit</button>
    </form>
</body>
</html>

Falcon code:

import falcon
from wsgiref import simple_server

class Resource(object):
    def on_post(self, req, resp):
        resp.status = falcon.HTTP_200
        resp.body = req.params['name']

app = api = falcon.API()
app.req_options.auto_parse_form_urlencoded = True
api.add_route('/', Resource())

if __name__ == '__main__':
    http = simple_server.make_server('127.0.0.1', 8000, app)
    http.serve_forever()
like image 196
Prateek Jain Avatar answered Jan 11 '23 22:01

Prateek Jain