kas*_*ere 4 google-app-engine python-2.7 webapp2 path-parameter
我从一个Java 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
}
Run Code Online (Sandbox Code Playgroud)
我已经走了过来例子在谷歌(https://developers.google.com/appengine/docs/python/gettingstartedpython27/usingwebapp).但我不知道如何扩展简单
app = webapp2.WSGIApplication([('/', MainPage),
('/sign', Guestbook)],
debug=True)
Run Code Online (Sandbox Code Playgroud)
看看webapp2中的URI路由.在这里,您可以匹配/路由URI并获取参数.这些关键字参数传递到您的处理程序:http://webapp2.readthedocs.io/en/latest/guide/routing.html#the-url-template
这是一个带有一个参数{action}的helloworld示例:
#!/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)
Run Code Online (Sandbox Code Playgroud)
还有你的app.yaml:
application: helloworld
version: 1
runtime: python27
api_version: 1
threadsafe: false
handlers:
- url: (.*)
script: helloworld.app
libraries:
- name: webapp2
version: latest
Run Code Online (Sandbox Code Playgroud)
当我尝试时,这在SDK中工作正常
http://localhost:8080/start # result: Action, start
or
http://localhost:8080 # result: Hello, webapp2 World!
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4133 次 |
| 最近记录: |