【问题标题】:Rest Web Service with App Engine and Webapp使用 App Engine 和 Webapp 休息 Web 服务
【发布时间】:2011-02-01 20:37:18
【问题描述】:

我想在应用引擎上构建一个 REST Web 服务。目前我有这个:

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 时,我想检索我的所有用户。我想我可以通过构建另一个处理程序来做到这一点,但我想知道是否可以在这个处理程序内部做到这一点。

【问题讨论】:

标签: python google-app-engine rest web-applications


【解决方案1】:

当然,您可以 -- 将处理程序的 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 方法可以轻松区分它们(通常通过其参数)。

【讨论】:

  • 您还可以使用两个处理程序 - 一个用于“/rest/users/”,一个用于“/rest/users/(.+)”。
  • @Nick,当然,但是 OP 知道,正如他所说“我可以通过构建另一个处理程序来做到这一点,但我想知道是否可以在这个处理程序内部做到这一点” -所以我没有重复他刚才说的话;-)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
  • 2020-12-31
  • 2011-12-16
  • 1970-01-01
相关资源
最近更新 更多