Search code examples
pythongoogle-app-enginewebapp2gae-python27

Not able to taking multiple inputs in Webapp2 in python 2.7?


I am using this code to add two numbers

import webapp2


class HomeHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write('This is the HomeHandler.')


class AddHandler(webapp2.RequestHandler):
    def get(self, num1, num2):
        num3 = int(num2)+int(num1)
        self.response.write('The sum of the numbers is- %d' % num3)


app = webapp2.WSGIApplication([
    webapp2.Route(r'/', handler=HomeHandler, name='home'),

    webapp2.Route(r'/add/<num1:\d+>/<num2:\d+>',
                  handler=AddHandler, name='add')

], debug=True, config=config)

# app.router.add((r'/add/<num1:\d+>/<num2:\d+>', handler=AddHandler, name='add'))

print(app)
# app = webapp2.WSGIApplication([
#     (r'/', HomeHandler),
#     (r'/products', ProductListHandler),
#     (r'/products/(\d+)', ProductHandler),
# ])


def main():
    from paste import httpserver
    httpserver.serve(app, host='127.0.0.1', port='8080')


if __name__ == '__main__':
    main()

I am using this url to add the two numbers http://127.0.0.1:8080/add/3/4

but i want all the inputs to be after single / ex- http://127.0.0.1:8080/divide/a=3&b=3

And after taking the input i have to cast them to integer to add them and is this possible without casting?

How to do this please help?


Solution

  • And after taking the input i have to cast them to integer to add them and is this possible without casting?

    First of all I highly recommend to upgrade to Python 3.X since Python 2.X is already reaching their end of life. I would recommend switching to a library like Flask since webapp2 does not support Python 3.

    Also Google Cloud highly suggest to upgrade your Google App Engine app as stated here:

    The Python community will sunset Python 2 on January 1, 2020, and are encouraging all developers to upgrade to Python 3 as soon as they can. In recognition that customers may need more time to migrate from Python 2 to Python 3, Google Cloud customers will be able to run Python 2 apps and use existing Python 2 client libraries after January 1, 2020.

    As far as the casting is concerned, it seems like you need to cast it every time that you would like to pass an integer into your URL.

    You could something like Regex to parse the value that comes from the URL to check if it is a number.

    In case it is, you could then parse it to be an integer.

    As for the inputs being all after the slash you could use the solution mentioned in this post.

    Is not only more efficient but also is timeproof since they are examples in Python 3 that you could use right away after upgrading your Python 2.X application to Python 3.X.

    Not able to taking multiple inputs in Webapp2 in python 2.7?

    Yes, you can actually do it. I attached a post that maybe is helpful to you with an example on how to retrieve multiple inputs within webapp2.

    I hope it helps.