Time for action – writing our own template for a URL redirector

Now, let's have a look at an example URL redirector program in Python, which can be extended to fit any scenario:

#!/usr/bin/env python import sys def redirect_url(line, concurrent): list = line.split(' ') # 1st or 2nd element of the list # is the URL depending on concurrency if concurrent: old_url = list[1] else: old_url = list[0] # Do remember that the new_url # should contain a '\n' at the end. new_url = '\n' # Take the decision and modify the url if needed if old_url.endswith('.avi'): # Rewrite example new_url = 'http://example.com/' + new_url elif old_url.endswith('.exe'): # Redirect example new_url = '302:http://google.co.in/' + new_url return new_url def main(concurrent = True): ...

Get Squid Proxy Server 3.1 Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.