Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

URL Shortener

Here are examples of a URL shortener you can use for inspiration:

  • TinyURL: Shorten that long URL into a tiny URL
  • bitly: Custom URL Shortener, Link Management & Branded Links

Here are resources that you can use to build your URL shortener:

00:00 URL Shortener. URLs can be extremely long and not very user friendly. When people

00:06 share links—or even try to remember a URL—it can be difficult because most of them are filled with unusual characters and may not form meaningful words. This is where the URL shortener comes in.

00:18 A URL shortener reduces the characters or letters in a URL, making them easier to read and easier to remember. A long URL, like the one you see onscreen now, can be shortened drastically to something short and simple to remember. With this kind of URL shortener, they become a joy to work with.

00:36 Let’s take a look at a couple of implementations of URL shorteners.

00:41 Firstly, here’s TinyURL. It offers a simple interface where you can enter a long URL and a custom alias if you want to make it more memorable. Clicking Make TinyURL will create that short URL, and then you can share that much more easily.

00:58 Here’s another similar service, Bitly, which offers user accounts and more configuration.

01:05 Let’s look at some of the technical challenges you’ll face in creating a URL shortener. Firstly, content access. requests has already been seen earlier on in this course, but it’s a vital tool to allow checking that the URL that’s input by the user is valid at the time of the link creation.

01:23 It could also be used to check that a link is still valid when it’s clicked upon and allow the URL shortener to show an error page instead of sending the user to a now-dead URL. Secondly, creation of URLs using random and string.

01:38 You will need a method to create a random URL string, generally to be as short as possible as a link instead of the original URL. Creating these automatically should be straightforward, but you’ll still need to ensure that each one is unique.

01:53 Thirdly, database, using sqlite3 or the framework ORM. As with other web

01:59 projects, a database is essential for storage and recall of the URL information you’re creating, as well as for adding some of the extra challenges that you may attempt later on. Now, let’s look at some of those extra challenges.

02:14 Firstly, custom URLs. While a random URL is useful, a custom URL can be much more symbolic and memorable. Giving the user the option to enter a custom URL at the time of creation is a desirable extra feature.

02:28 Secondly, user accounts. While many URL shortening services avoid the overhead of having user accounts, being able to identify users and allow them to manage their custom URLs—and also to see statistics on their use—is a significant upgrade and undertaking for this project. Thirdly, URL updates.

02:49 Generally, URL shortening services are a one-off system; the incoming and outgoing URLs are fixed at the time of creation. However, being able to alter these after the event may help to keep the URLs relevant, such as allowing the link to the latest version of a book on Amazon from the same video.

Become a Member to join the conversation.