我想在 App Engine 上构建 REST 网络服务。目前我有这个:
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
class UsersHandler(webapp.RequestHandler):
def get(self, name):
self.response.out.write('Hello '+ name+'!')
def main():
util.run_wsgi_app(application)
#Map url like /rest/users/johnsmith
application = webapp.WSGIApplication([(r'/rest/users/(.*)',UsersHandler)]
debug=True)
if __name__ == '__main__':
main()
我想在访问路径/rest/users 时检索例如我的所有用户。我想象我可以通过构建另一个处理程序来做到这一点,但我想知道是否可以在这个处理程序内部完成它。
最佳答案
当然,您可以 -- 将处理程序的 get
方法更改为
def get(self, name=None):
if name is None:
"""deal with the /rest/users case"""
else:
# deal with the /rest/users/(.*) case
self.response.out.write('Hello '+ name+'!')
和你的申请
application = webapp.WSGIApplication([(r'/rest/users/(.*)', UsersHandler),
(r'/rest/users', UsersHandler)]
debug=True)
换句话说,将您的处理程序映射到您希望它处理的所有 URL 模式,并确保处理程序的 get
方法可以轻松区分它们(通常通过其参数)。
关于python - 使用 App Engine 和 Webapp 休息 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2534947/