def write_error(sock, status_int, reason, mesg): html = textwrap.dedent("""\ <html> <head> <title>%(reason)s</title> </head> <body> <h1><p>%(reason)s</p></h1> %(mesg)s </body> </html> """) % {"reason": reason, "mesg": _compat.html_escape(mesg)} http = textwrap.dedent("""\ HTTP/1.1 %s %s\r Connection: close\r Content-Type: text/html\r Content-Length: %d\r \r %s""") % (str(status_int), reason, len(html), html) write_nonblock(sock, http.encode('latin1'))