当前位置: 首页>>代码示例>>Python>>正文


Python BaseHTTPRequestHandler.responses方法代码示例

本文整理汇总了Python中http.server.BaseHTTPRequestHandler.responses方法的典型用法代码示例。如果您正苦于以下问题:Python BaseHTTPRequestHandler.responses方法的具体用法?Python BaseHTTPRequestHandler.responses怎么用?Python BaseHTTPRequestHandler.responses使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在http.server.BaseHTTPRequestHandler的用法示例。


在下文中一共展示了BaseHTTPRequestHandler.responses方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: handle_get

# 需要导入模块: from http.server import BaseHTTPRequestHandler [as 别名]
# 或者: from http.server.BaseHTTPRequestHandler import responses [as 别名]
def handle_get(self):
        """Handle a single HTTP GET request.

        Default implementation indicates an error because
        XML-RPC uses the POST method.
        """

        code = 400
        message, explain = BaseHTTPRequestHandler.responses[code]

        response = http.server.DEFAULT_ERROR_MESSAGE % \
            {
             'code' : code,
             'message' : message,
             'explain' : explain
            }
        response = response.encode('utf-8')
        print('Status: %d %s' % (code, message))
        print('Content-Type: %s' % http.server.DEFAULT_ERROR_CONTENT_TYPE)
        print('Content-Length: %d' % len(response))
        print()
        sys.stdout.flush()
        sys.stdout.buffer.write(response)
        sys.stdout.buffer.flush() 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:26,代码来源:server.py

示例2: __init__

# 需要导入模块: from http.server import BaseHTTPRequestHandler [as 别名]
# 或者: from http.server.BaseHTTPRequestHandler import responses [as 别名]
def __init__(self, status=None, message=None):
        """Initialize class."""
        responses = BaseHTTPRequestHandler.responses

        # Add some additional responses that aren't included...
        responses[418] = ("I'm a teapot", TOTALLY_NORMAL_CODE)
        responses[422] = (
            "Unprocessable Entity",
            "The request was well-formed but was"
            " unable to be followed due to semantic errors",
        )

        self.status_code = status or self.default_status

        # Don't explode if provided status_code isn't found.
        _message = responses.get(self.status_code, [""])
        error_message = f"{self.status_code:d}: {_message[0]}"
        if message:
            error_message = f"{error_message} - {message}"

        super(HTTPError, self).__init__(error_message) 
开发者ID:rackerlabs,项目名称:fleece,代码行数:23,代码来源:httperror.py

示例3: valid_status

# 需要导入模块: from http.server import BaseHTTPRequestHandler [as 别名]
# 或者: from http.server.BaseHTTPRequestHandler import responses [as 别名]
def valid_status(status):
    """Return legal HTTP status Code, Reason-phrase and Message.

    The status arg must be an int, a str that begins with an int
    or the constant from ``http.client`` stdlib module.

    If status has no reason-phrase is supplied, a default reason-
    phrase will be provided.

    >>> import http.client
    >>> from http.server import BaseHTTPRequestHandler
    >>> valid_status(http.client.ACCEPTED) == (
    ...     int(http.client.ACCEPTED),
    ... ) + BaseHTTPRequestHandler.responses[http.client.ACCEPTED]
    True
    """

    if not status:
        status = 200

    code, reason = status, None
    if isinstance(status, str):
        code, _, reason = status.partition(' ')
        reason = reason.strip() or None

    try:
        code = int(code)
    except (TypeError, ValueError):
        raise ValueError('Illegal response status from server '
                         '(%s is non-numeric).' % repr(code))

    if code < 100 or code > 599:
        raise ValueError('Illegal response status from server '
                         '(%s is out of range).' % repr(code))

    if code not in response_codes:
        # code is unknown but not illegal
        default_reason, message = '', ''
    else:
        default_reason, message = response_codes[code]

    if reason is None:
        reason = default_reason

    return code, reason, message


# NOTE: the parse_qs functions that follow are modified version of those
# in the python3.0 source - we need to pass through an encoding to the unquote
# method, but the default parse_qs function doesn't allow us to.  These do. 
开发者ID:cherrypy,项目名称:cherrypy,代码行数:52,代码来源:httputil.py


注:本文中的http.server.BaseHTTPRequestHandler.responses方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。