什么是最简单的URL缩短应用程序可以在python为Google App Engine写?

像bit.ly这样的服务非常适合缩短您要在Twitter和其他对话中包含的网址.什么是最简单的URL缩短应用程序可以在 python为Google App Engine写?
这听起来像是一个挑战!

from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import run_wsgi_app

class ShortLink(db.Model):
  url = db.TextProperty(required=True)

class CreateLinkHandler(webapp.RequestHandler):
  def post(self):
    link = ShortLink(url=self.request.POST['url'])
    link.put()
    self.response.out.write("%s/%d" % (self.request.host_url, link.key().id())

  def get(self):
    self.response.out.write('<form method="post" action="/create"><input type="text" name="url"><input type="submit"></form>')

class VisitLinkHandler(webapp.RequestHandler):
  def get(self, id):
    link = ShortLink.get_by_id(int(id))
    if not link:
      self.error(404)
    else:
      self.redirect(link.url)

application = webapp.WSGIApplication([
    ('/create', CreateLinkHandler),
    ('/(\d+)', VisitLinkHandler),
])

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()
相关文章
相关标签/搜索