Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the simplest URL shortener application one could write in python for the Google App Engine?

Services like bit.ly are great for shortening URL’s that you want to include in tweets and other conversations. What is the simplest URL shortener application one could write in python for the Google App Engine?

like image 343
Chris Avatar asked Sep 10 '09 14:09

Chris


People also ask

What is the latest Python version that can be used for App Engine?

The Python 3.10 runtime is capable of running any framework, library, or binary. Optimized to scale nearly instantaneously to handle huge traffic spikes.

How do you create a web app using python flask and Google App Engine?

Step 1: Building the App structure. Step 2: Creating the Main App code with the API request. Step 3: Creating the 2 pages for the App (Main and Result) with Jinja, HTML, and CSS. Step 4: Deploying and testing on your local laptop.


2 Answers

That sounds like a challenge!

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()
like image 131
Nick Johnson Avatar answered Oct 26 '22 18:10

Nick Johnson


similar, complete with GAE project boilerplate: https://github.com/dustingetz/vanity-redirect

like image 39
Dustin Getz Avatar answered Oct 26 '22 18:10

Dustin Getz