我来自{REST}背景,Python
上的Google App Engine's
。我需要使用带有路径参数的webapp2
帮助。下面是Java如何读取请求的示例。有人请将代码翻译成python如何用webapp2
读取它吗?
// URL: my_dogs/user_id/{user_id}/dog_name/{a_name}/breed/{breed}/{weight}
@Path("my_dogs/user_id/{user_id}/dog_name/{a_name}/breed/{breed}/{weight}")
public Response getMyDog(
@PathParam("user_id") Integer id,
@PathParam("a_name") String name,
@PathParam("breed") String breed,
@PathParam("weight") String weight
){
//the variables are: id, name, breed, weight.
///use them somehow
}
我已经在google(https://developers.google.com/appengine/docs/python/gettingstartedpython27/usingwebapp)上查看了这些示例。但我不知道如何扩展简单的
app = webapp2.WSGIApplication([('/', MainPage),
('/sign', Guestbook)],
debug=True)
答案 0 :(得分:5)
查看webapp2中的URI路由。在这里,您可以匹配/路由URI并获取参数。这些关键字参数将传递给您的处理程序:http://webapp2.readthedocs.io/en/latest/guide/routing.html#the-url-template
这是一个helloworld示例,其中包含一个参数{action}:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import webapp2
class ActionPage(webapp2.RequestHandler):
def get(self, action):
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Action, ' + action)
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hello, webapp2 World!')
app = webapp2.WSGIApplication([
webapp2.Route(r'/<action:(start|failed)>', handler=ActionPage),
webapp2.Route(r'/', handler=MainPage),
], debug=True)
你的app.yaml:
application: helloworld
version: 1
runtime: python27
api_version: 1
threadsafe: false
handlers:
- url: (.*)
script: helloworld.app
libraries:
- name: webapp2
version: latest
当我尝试
时,这在SDK中运行良好http://localhost:8080/start # result: Action, start
or
http://localhost:8080 # result: Hello, webapp2 World!