Errors.py :  » Web-Frameworks » Python-Publishing-Accessories » PPA-0.2.7 » PPA » HTTP » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Python Publishing Accessories 
Python Publishing Accessories » PPA 0.2.7 » PPA » HTTP » Errors.py
# $Id: Errors.py,v 1.1.1.1 2004/04/09 13:18:11 ods Exp $

import cgi

statusCodes = {
    # Generated from RFC 2616
    # Informational
    100: 'Continue',
    101: 'Switching Protocols',
    # Successful
    200: 'OK',
    201: 'Created',
    202: 'Accepted',
    203: 'Non-Authoritative Information',
    204: 'No Content',
    205: 'Reset Content',
    206: 'Partial Content',
    # Redirection
    300: 'Multiple Choices',
    301: 'Moved Permanently',
    302: 'Found',
    303: 'See Other',
    304: 'Not Modified',
    305: 'Use Proxy',
    307: 'Temporary Redirect',
    # Client Error
    400: 'Bad Request',
    401: 'Unauthorized',
    402: 'Payment Required',
    403: 'Forbidden',
    404: 'Not Found',
    405: 'Method Not Allowed',
    406: 'Not Acceptable',
    407: 'Proxy Authentication Required',
    408: 'Request Timeout',
    409: 'Conflict',
    410: 'Gone',
    411: 'Length Required',
    412: 'Precondition Failed',
    413: 'Request Entity Too Large',
    414: 'Request-URI Too Long',
    415: 'Unsupported Media Type',
    416: 'Requested Range Not Satisfiable',
    417: 'Expectation Failed',
    # Server Error
    500: 'Internal Server Error',
    501: 'Not Implemented',
    502: 'Bad Gateway',
    503: 'Service Unavailable',
    504: 'Gateway Timeout',
    505: 'HTTP Version Not Supported'
}


class EndOfRequest(Exception):

    def __init__(self, status=None):
        self.status = status

    def handle(self, request, response):
        if self.status is not None:
            response.setStatus(self.status)

    _body_template = '''\
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>%(code)s - %(desc)s</title>
</head>
<body>
<h1>%(code)s - %(desc)s</h1>
%(message)s
</body>
</html>'''
    _body_content_type = 'text/html'
    
    def sendBody(self, response, message):
        response.setContentType(self._body_content_type)
        response.write(self._body_template % {
                            'code': self.status,
                            'desc': statusCodes.get(self.status, 'Unknown'),
                            'message': message})


class Redirect(EndOfRequest):

    def __init__(self, uri, status):
        EndOfRequest.__init__(self, status)
        self.uri = uri

    def handle(self, request, response):
        EndOfRequest.handle(self, request, response)
        response.headers['Location'] = self.uri
        quoted_uri = cgi.escape(self.uri, 1)
        self.sendBody(response, 
            'Document moved here: <a href="%s">%s</a>.' % (quoted_uri,
                                                           quoted_uri))


class MovedPermanently(Redirect):

    def __init__(self, uri):
        Redirect.__init__(self, uri, 301)


class Found(Redirect):

    def __init__(self, uri):
        Redirect.__init__(self, uri, 302)


class SeeOther(Redirect):

    def __init__(self, uri):
        Redirect.__init__(self, uri, 303)


class TemporaryRedirect(Redirect):

    def __init__(self, uri):
        Redirect.__init__(self, uri, 307)


class ClientError(EndOfRequest):

    def __init__(self, status, message):
        EndOfRequest.__init__(self, status)
        self.message = message

    def handle(self, request, response):
        EndOfRequest.handle(self, request, response)
        self.sendBody(response, self.message)


class NotFound(ClientError):

    def __init__(self):
        EndOfRequest.__init__(self, 404)

    def handle(self, request, response):
        EndOfRequest.handle(self, request, response)
        self.sendBody(response,
                      'The requested URL %s was not found on this server.' % \
                                                cgi.escape(request.uri(), 1))



class ServerError(EndOfRequest):

    def __init__(self, status, message):
        EndOfRequest.__init__(self, status)
        self.message = message

    def handle(self, request, response):
        EndOfRequest.handle(self, request, response)
        self.sendBody(response, self.message)


class InternalServerError(ServerError):

    def __init__(self, message='The server encountered an internal error '\
                               'and was unable to complete your request.'):
        ServerError.__init__(self, 500, message)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.