你如何使用mod_python抛出HTTP错误

时间:2010-03-26 01:36:46

标签: python apache mod-python http-error

我有一个设置,我使用mod_python发布者提供简单的python页面。在某些方面,我希望python函数引发标准的apache错误 - 例如,如果缺少必需的文件,则抛出500错误。如何从mod_python脚本中抛出apache错误?

2 个答案:

答案 0 :(得分:2)

我不是蟒蛇专家,但是来自this documentation,你似乎可以做到:

raise apache.SERVER_RETURN, apache.HTTP_INTERNAL_SERVER_ERROR

以下是链接腐烂时文档的引用:

  

处理程序函数将始终传递对请求的引用   宾语。 (在本手册中,通常会引用请求对象   通过req变量。)

     

每个处理程序都可以返回:

     

apache.OK,意思是请求的这个阶段由此处理   处理程序,没有发生错误。

     

apache.DECLINED,意思是此处理程序尚未处理此阶段   请求完成,Apache需要寻找另一个处理程序   在随后的模块中。

     

apache.HTTP_ERROR,表示发生了HTTP错误。 HTTP_ERROR可以   以下任何一项:

HTTP_CONTINUE                     = 100
HTTP_SWITCHING_PROTOCOLS          = 101
HTTP_PROCESSING                   = 102
HTTP_OK                           = 200
HTTP_CREATED                      = 201
HTTP_ACCEPTED                     = 202
HTTP_NON_AUTHORITATIVE            = 203
HTTP_NO_CONTENT                   = 204
HTTP_RESET_CONTENT                = 205
HTTP_PARTIAL_CONTENT              = 206
HTTP_MULTI_STATUS                 = 207
HTTP_MULTIPLE_CHOICES             = 300
HTTP_MOVED_PERMANENTLY            = 301
HTTP_MOVED_TEMPORARILY            = 302
HTTP_SEE_OTHER                    = 303
HTTP_NOT_MODIFIED                 = 304
HTTP_USE_PROXY                    = 305
HTTP_TEMPORARY_REDIRECT           = 307
HTTP_BAD_REQUEST                  = 400
HTTP_UNAUTHORIZED                 = 401
HTTP_PAYMENT_REQUIRED             = 402
HTTP_FORBIDDEN                    = 403
HTTP_NOT_FOUND                    = 404
HTTP_METHOD_NOT_ALLOWED           = 405
HTTP_NOT_ACCEPTABLE               = 406
HTTP_PROXY_AUTHENTICATION_REQUIRED= 407
HTTP_REQUEST_TIME_OUT             = 408
HTTP_CONFLICT                     = 409
HTTP_GONE                         = 410
HTTP_LENGTH_REQUIRED              = 411
HTTP_PRECONDITION_FAILED          = 412
HTTP_REQUEST_ENTITY_TOO_LARGE     = 413
HTTP_REQUEST_URI_TOO_LARGE        = 414
HTTP_UNSUPPORTED_MEDIA_TYPE       = 415
HTTP_RANGE_NOT_SATISFIABLE        = 416
HTTP_EXPECTATION_FAILED           = 417
HTTP_UNPROCESSABLE_ENTITY         = 422
HTTP_LOCKED                       = 423
HTTP_FAILED_DEPENDENCY            = 424
HTTP_INTERNAL_SERVER_ERROR        = 500
HTTP_NOT_IMPLEMENTED              = 501
HTTP_BAD_GATEWAY                  = 502
HTTP_SERVICE_UNAVAILABLE          = 503
HTTP_GATEWAY_TIME_OUT             = 504
HTTP_VERSION_NOT_SUPPORTED        = 505
HTTP_VARIANT_ALSO_VARIES          = 506
HTTP_INSUFFICIENT_STORAGE         = 507
HTTP_NOT_EXTENDED                 = 510
     

作为返回HTTP错误代码的替代方法,处理程序可以发出信号   通过引发apache.SERVER_RETURN异常并提供错误   HTTP错误代码作为异常值,例如:

raise apache.SERVER_RETURN, apache.HTTP_FORBIDDEN

答案 1 :(得分:1)

我相信它是:

def my_action(req):
  # all the status code constants are defined in the apache module 
  req.status = apache.HTTP_INTERNAL_SERVER_ERROR 
  req.content_type = some_mime_type
  req.write(content)
  raise apache.HTTP_SERVER_RETURN, apache.DONE

引发apache.DONE告诉Apache不要写出自己的错误页面。