英文:
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('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?
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论