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?
The Python 3.10 runtime is capable of running any framework, library, or binary. Optimized to scale nearly instantaneously to handle huge traffic spikes.
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.
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()
similar, complete with GAE project boilerplate: https://github.com/dustingetz/vanity-redirect
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With