python - 使用 App Engine 和 Webapp 休息 Web 服务

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

我想在 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/

相关文章:

python - 使用 Python 在 quantlib 中为 float 债券定价

python:将字符串转换为变量名

ios - Bittrex API 返回 APIKEY_NOT_PROVIDED

api - 我该如何使用MindMeister API的 "Personal access tokens"?

python - Tensorflow 数据集 API 中的过采样功能

python - 使用 python 中的 matplotlib 制作具有指定离散颜色映射的热图

google-app-engine - GAE 搜索 API 排序

python - App Engine 上后台线程的日志在哪里?

java - 查询以获取 2 个给定日期之间的所有日期

ruby - 在 Ruby 中设置请求 header