无法在Python 2.7中的Webapp2中接受多个输入吗?

huangapple go评论77阅读模式
英文:

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('/', handler=HomeHandler, name='home'),
    webapp2.Route('/add/<num1:\d+>/<num2:\d+>',
                  handler=AddHandler, name='add')
], debug=True, config=config)

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 a single '/'. For example: http://127.0.0.1:8080/divide/a=3&b=3

And after taking the input, I have to cast them to integers to add them. Is this possible without casting?

How to do this, please help?

英文:

I am using this code to add two numbers

import webapp2


class HomeHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write(&#39;This is the HomeHandler.&#39;)


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


app = webapp2.WSGIApplication([
    webapp2.Route(r&#39;/&#39;, handler=HomeHandler, name=&#39;home&#39;),

    webapp2.Route(r&#39;/add/&lt;num1:\d+&gt;/&lt;num2:\d+&gt;&#39;,
                  handler=AddHandler, name=&#39;add&#39;)

], debug=True, config=config)

# app.router.add((r&#39;/add/&lt;num1:\d+&gt;/&lt;num2:\d+&gt;&#39;, handler=AddHandler, name=&#39;add&#39;))

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


def main():
    from paste import httpserver
    httpserver.serve(app, host=&#39;127.0.0.1&#39;, port=&#39;8080&#39;)


if __name__ == &#39;__main__&#39;:
    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?

答案1

得分: 1

> 在获取输入后,我必须将它们转换为整数以进行加法运算,是否可以在不进行转换的情况下实现这一点?

首先,我强烈建议升级到Python 3.X,因为Python 2.X已经接近生命周期的尽头。我建议切换到像Flask这样的库,因为webapp2不支持Python 3

此外,Google Cloud强烈建议根据此处所述(https://cloud.google.com/appengine/docs/standard/python/quickstart)升级您的Google App Engine应用程序:

> Python社区将于2020年1月1日停用Python 2,并鼓励所有开发人员尽快升级到Python 3。鉴于客户可能需要更多时间从Python 2迁移到Python 3,Google Cloud客户将能够在2020年1月1日后运行Python 2应用程序并使用现有的Python 2客户端库。

就转换而言,似乎您需要每次想要将整数传递到您的URL中时进行转换。

您可以使用类似于正则表达式来解析来自URL的值,以检查是否为数字

如果是数字,然后可以将其解析为整数。

至于所有输入都在斜杠之后,您可以使用此帖子中提到的解决方案。

这不仅更高效,而且还具有时间性,因为在将Python 2.X应用程序升级到Python 3.X之后,您可以立即使用Python 3中的示例。

> 在Python 2.7的Webapp2中无法接受多个输入吗?

是的,您实际上可以做到。我附上了一个帖子,也许对您有帮助,其中包含了如何在webapp2中检索多个输入的示例。

希望能对您有所帮助。

英文:

> 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.

huangapple
  • 本文由 发表于 2020年1月3日 17:29:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576021.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定