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()
python类responses()的实例源码
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', SECRET)
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 = "{0:d}: {1}".format(self.status_code, _message[0])
if message:
error_message = "{0} - {1}".format(error_message, message)
super(HTTPError, self).__init__(error_message)
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()
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()
def __init__(self, name) :
"""
@param name: URL to be opened
@keyword additional_headers: additional HTTP request headers to be added to the call
"""
try :
# Note the removal of the fragment ID. This is necessary, per the HTTP spec
req = Request(url=name.split('#')[0])
req.add_header('Accept', 'text/html, application/xhtml+xml')
self.data = urlopen(req)
self.headers = self.data.info()
if URIOpener.CONTENT_LOCATION in self.headers :
self.location = urljoin(self.data.geturl(),self.headers[URIOpener.CONTENT_LOCATION])
else :
self.location = name
except urllib_HTTPError :
e = sys.exc_info()[1]
from pyMicrodata import HTTPError
msg = BaseHTTPRequestHandler.responses[e.code]
raise HTTPError('%s' % msg[1], e.code)
except Exception :
e = sys.exc_info()[1]
from pyMicrodata import MicrodataError
raise MicrodataError('%s' % e)
def __init__(self, name) :
"""
@param name: URL to be opened
@keyword additional_headers: additional HTTP request headers to be added to the call
"""
try :
# Note the removal of the fragment ID. This is necessary, per the HTTP spec
req = Request(url=name.split('#')[0])
req.add_header('Accept', 'text/html, application/xhtml+xml')
self.data = urlopen(req)
self.headers = self.data.info()
if URIOpener.CONTENT_LOCATION in self.headers :
self.location = urlparse.urljoin(self.data.geturl(),self.headers[URIOpener.CONTENT_LOCATION])
else :
self.location = name
except urllib_HTTPError :
e = sys.exc_info()[1]
from pyMicrodata import HTTPError
msg = BaseHTTPRequestHandler.responses[e.code]
raise HTTPError('%s' % msg[1], e.code)
except Exception :
e = sys.exc_info()[1]
from pyMicrodata import MicrodataError
raise MicrodataError('%s' % e)
def formatMessage(self, record):
try:
record.headers = self._headers_sep.join([self._headers_style.format(_FakeRecord({'name': k, 'value': v}))
for k, v in record.headers.items()])
except AttributeError:
pass
try:
record.elapsed = "{} ms".format(int(record.elapsed.total_seconds() * 1000))
except AttributeError:
pass
try:
record.status_text = BaseHTTPRequestHandler.responses[record.status_code][0]
except KeyError:
record.status_text = 'Unknown'
except AttributeError:
pass
record.full_url = record.url
try:
record.query_params = urlencode(record.params)
record.full_url = "?".join([record.full_url, record.query_params])
except AttributeError:
pass
try:
record.exception_repr = repr(record.exception)
except AttributeError:
pass
s = super(ServiceClientFormatter, self).formatMessage(record)
if record.action == 'REQUEST':
return s + self.format_request_message(record)
elif record.action == 'RESPONSE':
return s + self.format_response_message(record)
elif record.action == 'EXCEPTION':
if hasattr(record, 'body'):
return s + self.format_parse_exception_message(record)
else:
return s + self.format_exception_message(record)
else:
return s