1

Is it possible to modify the 404 response page sent from pythons basehttpserver library?

1 Answer 1

3

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>'''
Sign up to request clarification or add additional context in comments.

1 Comment

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 ;))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.