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

标签 python google-app-engine

像 bit.ly 这样的服务非常适合缩短您想包含在推文和其他对话中的 URL。可以用 python 为 Google App Engine 编写的最简单的 URL 缩短器应用程序是什么?

最佳答案

这听起来像是一个挑战!

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()

关于python - 可以用 python 为 Google App Engine 编写的最简单的 URL 缩短器应用程序是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1405786/

相关文章:

python - 从 Django 1.8 获取 TemplateDoesNotExist

python - 从 '_io.BytesIO' 转换为 python3.6 中的类字节对象?

python - 使用 Python 的 Google App Engine 中的图像属性?

python - 谷歌应用引擎 : Random error "ImportError: cannot import name nodes"

python - 如何使用 python 忽略许多 XML 文件中的标签

python - Pandas groupby 使用 agg 并同时申请

Java GAE 后端在 3 分钟内重新启动错误 202

python - 在不知道祖先的情况下从数据存储表中删除实体

python - 如何使用 Google App Engine Blobstore 保存网页图片

python - Pandas:用 np.nan 替换包含 0 的列子集中的所有单元格