wsgi.py :  » Web-Frameworks » Karrigell » Karrigell-3.1 » karrigell » apache » mod_wsgi » 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 » Karrigell 
Karrigell » Karrigell 3.1 » karrigell » apache » mod_wsgi » wsgi.py
import sys
import datetime
import email

from wsgiref import simple_server,util

import os,sys
this_dir = os.path.dirname(__file__)
if not this_dir in sys.path:
    sys.path.append(this_dir)

import wsgi_config_loader

import HTTP

class WSGIHandlerClass(simple_server.WSGIRequestHandler):
    """Logging is managed by Karrigell"""

    def log_message(self,*args):
        return

class Server:
    """Fake server class for interface compatibility"""

    def __init__(self,host,port):
        self.host = host
        self.port = port

class k_handler(HTTP.HTTP):

    def __init__(self,environ):
        """Build Karrigell-specific attributes from the environ
        prepared by the WSGI server"""
        self.server = Server(environ["SERVER_NAME"],environ["SERVER_PORT"])
        self.server_version = environ["SERVER_SOFTWARE"]

        self.client_address = (environ["REMOTE_ADDR"],0)
        self.rfile = environ['wsgi.input'] # for POST requests

        # headers
        self.headers = email.message_from_string('')
        if 'CONTENT_TYPE' in environ:
            self.headers['Content-type'] = environ['CONTENT_TYPE']
        if 'CONTENT_LENGTH' in environ:
            self.headers['Content-length'] = environ['CONTENT_LENGTH']
        for k in environ:
            if k.startswith("HTTP_"):
                header = k[5:].replace('_','-')
                self.headers[header] = environ[k]

        self.url = environ["PATH_INFO"]
        if environ["QUERY_STRING"]:
            self.url += '?'+environ["QUERY_STRING"]
        self.request_version = environ["SERVER_PROTOCOL"]
        self.protocol = environ["SERVER_PROTOCOL"]
        self.method = environ["REQUEST_METHOD"]

        # initialize log info
        self.info = datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S - ")
        self.info += "%s - %s %s %s" %(self.client_address[0],
            self.method,self.url,self.protocol)

    def send_status(self,code,msg):
        """Initialize response with status line"""
        self.response = "%s %s" %(code,msg)

    def send_headers(self):
        self.resp_headers["Server"] = self.version_string()
        self.resp_headers["Date"] = self.date_time_string()
        for morsel in self.cookies.values():
            self.resp_headers['Set-Cookie'] = morsel.output(header='').lstrip()

    def send_result(self):
        # managed by start_response
        return

class File:
    """Class to manage file objects as iterators
    Used as the return value of WSGI application, to avoid having to
    store the file content in memory"""

    blocksize = 2 << 17

    def __init__(self,fileobj):
        self.fileobj = fileobj
        self.fileobj.seek(0)

    def __iter__(self):
        return self
        
    def next(self):
        buf = self.fileobj.read(self.blocksize)
        if not buf:
            raise StopIteration
        return buf

def application(environ,start_response):
    handler = k_handler(environ)
    handler.process_request() # processing by Karrigell
    resp_headers = [(k,str(v)) for (k,v) in handler.resp_headers.items() ]
    start_response(handler.response, resp_headers)
    return File(handler.output)

if __name__ == "__main__":
    httpd = simple_server.make_server('', 8000, application,
        handler_class=WSGIHandlerClass)
    sa = httpd.socket.getsockname()
    print "Serving HTTP on", sa[0], "port", sa[1], "..."
    httpd.serve_forever()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.