问题
Is it possible to modify the 404 response page sent from pythons basehttpserver library?
回答1:
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>'''
来源:https://stackoverflow.com/questions/5587525/python-basehttpserver-can-i-modify-the-404-response