如何使用 mod_python 抛出 HTTP 错误

发布于 2024-08-26 19:36:20 字数 138 浏览 3 评论 0原文

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

I have a setup where I'm serving simple python pages using the mod_python publisher. At some points I'd like to have the python function raise a standard apache error - for example throwing a 500 error if a required file is missing. How can I throw an apache error from within a mod_python script?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伏妖词 2024-09-02 19:36:20

我不是Python专家,而是来自本文档 ,看来您可以这样做:

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_处理= 102
HTTP_OK = 200
HTTP_创建 = 201
HTTP_接受 = 202
HTTP_NON_AUTHORITATIVE = 203
HTTP_NO_内容 = 204
HTTP_重置_内容 = 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_未授权 = 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_冲突 = 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_锁定 = 423
HTTP_FAILED_DEPENDENCY = 424
HTTP_INTERNAL_SERVER_ERROR = 500
HTTP_NOT_IMPLMENTED = 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 错误代码作为异常值,例如:

引发 apache.SERVER_RETURN, apache.HTTP_FORBIDDEN

I am not a python expert but from this documentation, it would appear that you can do:

raise apache.SERVER_RETURN, apache.HTTP_INTERNAL_SERVER_ERROR

Here is a quote of the documentation in case of link rot:

A handler function will always be passed a reference to a request
object. (Throughout this manual, the request object is often referred
to by the req variable.)

Every handler can return:

apache.OK, meaning this phase of the request was handled by this
handler and no errors occurred.

apache.DECLINED, meaning this handler has not handled this phase of
the request to completion and Apache needs to look for another handler
in subsequent modules.

apache.HTTP_ERROR, meaning an HTTP error occurred. HTTP_ERROR can be
any of the following:

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

As an alternative to returning an HTTP error code, handlers can signal
an error by raising the apache.SERVER_RETURN exception, and providing
an HTTP error code as the exception value, e.g.:

raise apache.SERVER_RETURN, apache.HTTP_FORBIDDEN
咿呀咿呀哟 2024-09-02 19:36:20

我相信它是:

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 不要写出自己的错误页面。

I believe it's:

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

raising apache.DONE tells Apache not to write out its own error page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文