URL Shortener in Python with Google App Engine

According to wikipedia: URL shortening is a technique on the World Wide Web in which a Uniform Resource Locator (URL) may be made substantially shorter in length and still direct to the required page. This is achieved by using an HTTP Redirect on a domain name that is short, which links to the web page that has a long URL. This is especially convenient for messaging technologies such as Twitter and Identi.ca, which severely limit the number of characters that may be used in a message. Normally, a URL shortening service will use the top-level domain of a country that allows foreign sites to use its extension, such as .ly or .to (Libya and Tonga), to redirect worldwide using a short alphanumeric sequence after the provider’s site address in order to point to the long URL. Another use of URL shortening is to disguise the underlying address.

I implemented it in python with Google App Engine. Google App Engine (often referred to as GAE or simply App Engine, and also used by the acronym GAE/J) is a platform as a service (PaaS) cloud computing platform for developing and hosting web applications in Google-managed data centers. It virtualizes applications across multiple servers.

For deploying this project with google appengine, I developed and uploaded Python applications for Google App Engine using the App Engine Python software development kit (SDK).

The Python SDK includes a web server application that simulates the App Engine environment, including a local version of the datastore, Google Accounts, and the ability to fetch URLs and send email directly from our computer using the App Engine APIs.

Python App Engine applications communicate with the web server using the CGI standard. When the server receives a request for our application, it runs the application with the request data in environment variables and on the standard input stream (for POST data). To respond, the application writes the response to the standard output stream, including HTTP headers and content.

App Engine includes a simple web application framework of its own, called webapp. The webapp framework is already installed in the App Engine environment and in the SDK, so you do not need to bundle it with your application code to use it.

A webapp application has three parts:

  • one or more RequestHandler classes that process requests and build responses
  • a WSGIApplication instance that routes incoming requests to handlers based on the URL
  • a main routine that runs the WSGIApplication using a CGI adaptor.

The default datastore for an application is now the High Replication datastore.In my program, this High replication datastore stores the short URL of the corresponding original URL and when we give the same original URL, it gives the same short URL which is same as before.

Unlike a traditional web hosting environment, Google App Engine does not serve files directly out of our application’s source directory unless configured to do so. For that, we use static files. In my url shortener , I used stylesheets, images etc.

For testing the application: If we are not using Google App Engine Launcher, start the web server with the following command, giving it the path to the root directory. Here ‘url’ is my root directory.:

google_appengine/dev_appserver.py url/

The web server is now running, listening for requests on port 8080. You can test the application by visiting the following URL in your web browser:

I registerd this application in Google App Engine using my gmail account.

We can create and manage App Engine web applications from the App Engine Administration Console, at the following URL:

https://appengine.google.com/ and uploaded it by running the following command:

google_appengine appcfg.py update url/

For my url shortener application, the URL for my website is https://urlshortener-vidya.appspot.com/

My urlshortener website snapshot is given below:


The complete code of this program is available in my bitbucket account. Here is the link: https://bitbucket.org/vidyakv/url-shortener 


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

The WordPress.com Blog

The latest news on WordPress.com and the WordPress community.

%d bloggers like this: