如何使用Google App Engine和Python创建REST服务? [英] How-to create a REST service with Google App Engine and Python?

查看:98
本文介绍了如何使用Google App Engine和Python创建REST服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个RESTFUL Web服务,通​​过访问的URL获取请求,然后返回该客户端的相应文档。例如,如果它是一个天气应用程序,我想通过网络浏览器获取亚特兰大的天气,那么我可以访问 http://weatherapp.appspot.com/temperature/Atlanta ,它将返回一个包含亚特兰大信息的HTML文档。我不想要任何关系到数据库的东西,因为我只是想通过屏幕抓取来包装另一个网站。有没有人有任何关于如何从url中获取参数的例子?

使用webapp框架,你可以捕获正则表达式组并将它们传递给您的处理程序,如下所示:

$ p $ WeatherHandler(webapp.RequestHandler):
def get(self,位置):
#为位置

申请= webapp.WSGIApplication([
('/temperature/(.*)',WeatherHandler),
] )

def main():
run_wsgi_app(应用程序)
$ b如果__name__ ==__main__:
main()

正则表达式中的任何圆括号组将被收集并作为位置参数传递给处理程序上的get / post / etc方法。

I want to create a RESTFUL web service that gets a request via the URL that is accessed and then returns the appropriate document for that client. For example, if it was a weather app and I wanted to get the weather for Atlanta through a web browser, I would access http://weatherapp.appspot.com/temperature/Atlanta and it would return an HTML document with the information for Atlanta. I don't want anything that ties into a database as I am just trying to wrap another website via screen-scraping. Does anyone have any examples on how to get arguments from the url?

解决方案

Using the webapp framework, you can capture regular expression groups and pass them to your handler like this:

class WeatherHandler(webapp.RequestHandler):
  def get(self, location):
    # Do something for location

application = webapp.WSGIApplication([
    ('/temperature/(.*)', WeatherHandler),
])

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()

Any parenthesized groups in the regular expression are collected and passed as positional arguments to the get/post/etc methods on your handler.

这篇关于如何使用Google App Engine和Python创建REST服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆