Skip to content

Instantly share code, notes, and snippets.

@mundry
Created January 5, 2014 12:28
Show Gist options
  • Save mundry/8267639 to your computer and use it in GitHub Desktop.
Save mundry/8267639 to your computer and use it in GitHub Desktop.
A simple script to obscure an email address with HTML entities.
#!/usr/bin/env python2.7
from random import randint
addr = "[email protected]"
def ascii_to_html_entity(char):
"""
Convert a ASCII-character to its corresponding HTML entity.
char -- The character to convert.
"""
return "&#{};".format(hex(ord(char))[1:] if randint(0, 1) == 1 else str(ord(char)))
def email_link(addr):
"""
Construct a HTML email link for the given address with the
characters of the address replaced by HTML entities.
addr -- The address for the link.
"""
addr_encoded = "".join(( ascii_to_html_entity(char) for char in addr ))
return "".join([ '<a href="', "".join(( ascii_to_html_entity(char) for char in "mailto" )), ':', addr_encoded, '">', addr_encoded, '</a>' ])
print email_link(addr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment