本文整理匯總了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()
示例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)
示例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.