python basehttpserver: can i modify the 404 response?

限于喜欢 提交于 2020-01-25 10:06:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!