Is it possible to modify the 404 response page sent from pythons basehttpserver library?
1 Answer
To modify the default error document displayed by BaseHTTPRequestHandler, you may customize the error_message_format attribute. It's a string in which you can use the following tags that will be replaced with their value when rendered:
- %(code)d is the numeric error code (eg. 404)
- %(message)s is a string representation of the error
- %(explain)s is a string with more explanations about the error
Of course you can use HTML. For instance:
yourBaseServerInstance.error_message_format = '''
<body>
<h1>Error!</h1>
<p>Error code %(code)d.</p>
<p>Message: %(message)s.</p>
<p>Error code explanation: %(code)s = %(explain)s.</p>
</body>'''
1 Comment
Alois Mahdal
By default the content will be sent as
text/html. So I'd say your text will be handled as HTML. OTOH, if you want to send other type, change error_content_type attribute. (text/plain for plain text but virtually any MIME type if you are brave enough ;))