Tornado – Escape – URLs

Like that for JSON strings, Tornado also provides methods for escaping and unescaping URLs. Not just that, it also exports some methods to play with URLs.

Let’s see the behavior with the example below

import tornado.web
import tornado.escape
class Main(tornado.web.RequestHandler):
def get(self):
self.write("<br />")
self.write(tornado.escape.linkify("Linked URL:;))
self.write("<br />")
self.write(tornado.escape.linkify("Short URL:;, shorten=True))
self.write("<br />")
self.write(tornado.escape.linkify("Require Protocol:;, require_protocol=True))
self.write("<br />")
self.write(tornado.escape.linkify("Permitted Protocol:;, permitted_protocols=["ftp","http"]))
self.write("<br />")
self.write(tornado.escape.squeeze("Squeezed: http://techno"))
self.write("<br />Escaped URL:")
self.write("<br />Unescaped URL:")
application = tornado.web.Application([
(r"/", Main),
if __name__ == '__main__':

1. tornado.escape.linkify(text) – creates a link for provided text

2. tornado.escape.linkify(text, shorten=Tue) – creates a link and shortens the URL

3. tornado.escape.linkify(text, require_protocol=True) – creates a link only if it includes a protocol like http or https or ftp

4. tornado.escape.linkify(text, permitted_protocols=[]) – creates a link only if URL belongs to permitted protocols list

5. tornado.escape.squeeze(value) – Removes all white spaces and replaces it with one white space

6. tornado.escape.url_escape(value) – Encodes the URL

7. tornado.escape.url_unescape(value,encoding=”) – Decodes given value from URL based on provided encoding


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.