像bit.ly这样的服务可以很好地缩短你想要包含在推文和其他对话中的网址。用python为Google App Engine编写的最简单的URL缩短应用程序是什么?
发布于 2009-09-10 15:21:01
这听起来像是一个挑战!
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()发布于 2011-02-18 15:20:59
类似的,完整的GAE项目样板:https://github.com/dustingetz/vanity-redirect
发布于 2009-12-04 16:01:14
github上有一个django应用,github.com/nileshk/url-shortener。我对它进行了分支,使其更像一个包罗万象的站点,http://github.com/voidfiles/url-shortener。
https://stackoverflow.com/questions/1405786
复制相似问题