Python HTTPServer异常导致它失败

时间:2011-11-09 21:28:24

标签: python http error-handling

我尝试了很多不同的方法为Python编写一个简单的HTTP服务器(在托管环境中使用IronPython),每当出现错误(例如运行时错误)时,Python环境就会挂起,而不是关闭和报告错误。

更新

有一个comment on an answer to another SO question表明在请求处理程序中调用“self.server.shutdown()”也会导致Python Web服务器在Windows中挂起。

因此,运行时异常可能会导致同样的问题。

1 个答案:

答案 0 :(得分:2)

您可能应该采取以下措施:

try:
    httpd.serve_forever()
except RuntimeError:
    httpd.shutdown()
    sys.exit()

顺便说一句,这是针对python 3的,它假定您已导入sys模块。