Python http.server.BaseHTTPRequestHandler 模块,responses() 实例源码

我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用http.server.BaseHTTPRequestHandler.responses()

项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
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()
项目:fleece    作者:racker    | 项目源码 | 文件源码
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)
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
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()
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
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()
项目:Meiji    作者:GiovanniBalestrieri    | 项目源码 | 文件源码
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)
项目:prophet    作者:MKLab-ITI    | 项目源码 | 文件源码
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)
项目:aio-service-client    作者:alfred82santa    | 项目源码 | 文件源码
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