UserHTML is a Python module which sanitizes user-supplied HTML fragments (like blog comments or forum posts) so that they can be safely embedded in a Web page without fear of XSS and similar issues. It also handles issues like turning linebreaks into and , and turning instances of one element into instances of another.
Example usage:
>>> from userhtml import sanitize_html
>>> sanitize_html(u"
Click here!")
u"
Click here!
"