lib/wsgi.py
author Tero Marttila <terom@fixme.fi>
Fri, 06 Feb 2009 22:48:00 +0200
changeset 150 817a8bb1cdc6
parent 149 0538176eb172
child 30 a86a25a9f75b
permissions -rw-r--r--
and it works, a lot better than before
149
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
"""
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
    WSGI application implementation
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
"""
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
# for error reporting
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
import sys, traceback
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
# for Request/Response
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
import http
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
# for the request -> response bit :)
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
import handler
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
def request_handler (env, start_response) :
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
    """
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
        The actual request handling code
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
    """
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
    # build Request object
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
    request = http.Request(env)
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
    
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
    try :
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
        # request -> response
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
        response = handler.handle_request(request)
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
    except http.ResponseError, err :
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
        # just use the generated response
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
        response = err.get_response()
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
    # send response
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
    assert response, "No response"
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
    
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
    # send response status/headers
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
    start_response(response.get_status(), response.get_headers())
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
    # send respones data
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
    yield response.get_data()
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
def app (env, start_response) :
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
    """
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
        Wraps request_handler to trap errors
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
    """
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
    try :
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
        # passthrough request_handler
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
        for chunk in request_handler(env, start_response) :
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
            yield chunk
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
    except :
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
        # execption info
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
        info = sys.exc_info()
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
        # try and send 500 ISE to browser, if no headers yet...
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
        start_response("500 Internal Server Error", [('Content-type', "text/plain; charset=utf8")], info)
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
        # send traceback
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
        yield traceback.format_exc()
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59