URL Shortener using Python

Introduction:

You must have seen services on the internet to create short URLs from long ones used in various places. Short URLs are easy to remember or type, so they are very popular in the field of digital marketing and promotions. In this article, I’m going to walk you through how to create a URL shortener with Python.

You must have used various URL shorteners and they all do a great job! Even Google forms use URL shortener. So we can see it is widely used on the Internet.

Python has many libraries and APIs available that will help us to build our own URL shortener. In this article, I will be walking you through how to create a URL shortener using python.

Project Prerequisites:

  • It might seem difficult, but the python libraries make it easy without having to delve into complex topics.
  • We will be needing the following libraries for this project:
  • future
  • urllib
  • sys
  • contextlib
  • We don’t have to install the sys library as it is a built-in library.
  • The remaining future, contextlib, and urlib have to be installed using the cmd.

Command for installing future module – pip install future. To know more about the future module you can refer to this link – https://docs.python.org/3/library/__future__.html

The contextlib library can also be install using the command – pip install contextlib2. To know more about the contextlib module you can refer to this link – https://docs.python.org/3/library/contextlib.html

See also  OBJECT ORIENTED PROGRAMMING (OOP’s) IN PYTHON

Command for installing urllib module – pip install urllib3. To know more about the urlib module you can refer to this link – https://docs.python.org/3/library/urllib.html

Getting Started:

We have installed all the required modules. So now let’s start the setup for writing our code.

  • First, we’ll create a directory for our project named “URL_Shortener”.

Opening it we will create our project file named “urlShortener.py”.

Now that, we have completed the setup let’s start coding…

Code Implementation:

As always first I’ll be import all the required modules for this project.

I am using exception handling during the importing of modules to just make sure that there will be no problem during the importing of the required modules.

from __future__ import with_statement

import contextlib

try:

    from urllib.parse import urlencode

except ImportError:

    from urllib import urlencode

try:

    from urllib.request import urlopen

except ImportError:

    from urllib2 import urlopen

import sys

from __future__ import with_statement
import contextlib
try:
    from urllib.parse import urlencode
except ImportError:
    from urllib import urlencode
try:
    from urllib.request import urlopen
except ImportError:
    from urllib2 import urlopen
import sys

It may look like we are importing so many modules but they are just the ones that we mention above.

After importing all the required modules, we will define a function to create and return the shortened URL.

def make_tiny(url):

    request_url = (‘http://tinyurl.com/api-create.php?’ + 

    urlencode({‘url’:url}))

    with contextlib.closing(urlopen(request_url)) as response:

        return response.read().decode(‘utf-8’)

def make_tiny(url):
    request_url = ('http://tinyurl.com/api-create.php?' + 
    urlencode({'url':url}))
    with contextlib.closing(urlopen(request_url)) as response:
        return response.read().decode('utf-8')

We will call the function using the map function and we will loop through the returned value and print it.

See also  Binary Code for I Love You

for tinyurl in map(make_tiny, sys.argv[1:]):

    print(tinyurl)

for tinyurl in map(make_tiny, sys.argv[1:]):
    print(tinyurl)

We need to pass the URL in the terminal as command line arguments to the program.

That will be taken and processed using the function that we defined and will be printed … Make sure you give the URL without the “https://” before it so that the program will work properly.

Source Code:

from __future__ import with_statement

import contextlib

try:

    from urllib.parse import urlencode

except ImportError:

    from urllib import urlencode

try:

    from urllib.request import urlopen

except ImportError:

    from urllib2 import urlopen

import sys

def make_tiny(url):

    request_url = (‘http://tinyurl.com/api-create.php?’ + 

    urlencode({‘url’:url}))

    with contextlib.closing(urlopen(request_url)) as response:

        return response.read().decode(‘utf-8’)

def main():

    for tinyurl in map(make_tiny, sys.argv[1:]):

        print(tinyurl)

if __name__ == ‘__main__’:

    main()

from __future__ import with_statement
import contextlib
try:
    from urllib.parse import urlencode
except ImportError:
    from urllib import urlencode
try:
    from urllib.request import urlopen
except ImportError:
    from urllib2 import urlopen
import sys

def make_tiny(url):
    request_url = ('http://tinyurl.com/api-create.php?' + 
    urlencode({'url':url}))
    with contextlib.closing(urlopen(request_url)) as response:
        return response.read().decode('utf-8')

def main():
    for tinyurl in map(make_tiny, sys.argv[1:]):
        print(tinyurl)

if __name__ == '__main__':
    main()

Output:

When we run the program we must run it in the terminal as we need to pass the URL as a command line argument.

Use the command – py file_name.py url in the terminal to run the program.

You will be getting the out as shown in the below screenshot.

Congratulations, you now know how to create a URL shortener using Python.

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock