當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。