views:

122

answers:

2

I read through the tutorial on the cherrypy website, and I'm still having some trouble understanding how it can be implemented in a modular, scalable way.

Could someone show me an example of how to have cherrypy receive a simple http post to its root, process the variable in some way, and respond dynamically using that data in the response?

A: 

Are you asking for an example like this?

http://www.cherrypy.org/wiki/CherryPyTutorial#ReceivingdatafromHTMLforms

It receives input from forms.

You can return any text you want from a CherryPy method function, so dynamic text based on the input is really trivial.

S.Lott
A: 
from cherrypy import expose

class Adder:
    @expose
    def index(self):
        return '''<html>
                  <body>
                  <form action="add">
                      <input name="a" /> + <input name="b"> = 
                      <input type="submit" />
                  </form>
                  </body>
                  </html>'''

    @expose
    def add(self, a, b):
        return str(int(a) + int(b))


if __name__ == "__main__":
    from cherrypy import quickstart
    quickstart(Adder())

Run the script and then open a browser on http://localhost:8080

lazy1