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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tornado.web | |
import tornado.escape | |
class Main(tornado.web.RequestHandler): | |
def get(self): | |
self.write("<br />") | |
self.write(tornado.escape.linkify("Linked URL: http://technobeans.com/2012/08/22/tornado-database-mysql-client-wrapper/")) | |
self.write("<br />") | |
self.write(tornado.escape.linkify("Short URL: http://technobeans.com/2012/08/22/tornado-database-mysql-client-wrapper/", shorten=True)) | |
self.write("<br />") | |
self.write(tornado.escape.linkify("Require Protocol: http://www.technobeans.com/talks", require_protocol=True)) | |
self.write("<br />") | |
self.write(tornado.escape.linkify("Permitted Protocol: http://www.technobeans.com/articles", permitted_protocols=["ftp","http"])) | |
self.write("<br />") | |
self.write(tornado.escape.squeeze("Squeezed: http://techno beans.com")) | |
self.write("<br />Escaped URL:") | |
self.write(tornado.escape.url_escape("http://techno beans.com")) | |
self.write("<br />Unescaped URL:") | |
self.write(tornado.escape.url_unescape("http%3A%2F%2Ffacebook.com")) | |
application = tornado.web.Application([ | |
(r"/", Main), | |
],debug=True) | |
if __name__ == '__main__': | |
application.listen(8888) | |
tornado.ioloop.IOLoop.instance().start() |
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
Output