mod_python_handler.py :  » Web-Frameworks » Karrigell » Karrigell-3.1 » karrigell » apache » mod_python » 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_python » mod_python_handler.py
import os
import sys
import email
import datetime

from mod_python import apache,Cookie

import config_loader

import HTTP

class Server:

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

class mp_handler(HTTP.HTTP):

    def __init__(self,request):
        self.request = request
        self.client_address = request.connection.remote_addr
        #self.sock = sock
        self.rfile = request
        self.wfile = request
        self.keep_alive = True
        self.server = Server(request.server)

    def read_request(self):
        # parse request line
        self.request_line = self.request.the_request
        self.method = self.request.method
        self.url = self.request.unparsed_uri
        self.protocol = self.request.protocol
        # initialize log info
        info = datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S - ")
        info += str(self.client_address[0]) + (" - ")
        info += self.request_line.strip()
        self.info = info
        # request headers
        self.headers = email.message_from_string("")
        for (k,v) in self.request.headers_in.items():
            self.headers[k] = v

        self.post_data_source = self.rfile

    def send_result(self):
        """send result"""
        
        for (k,v) in self.resp_headers.items():
            self.request.headers_out[k] = str(self.resp_headers[k])
        self.request.headers_out["Date"] = self.date_time_string()
        for morsel in self.cookies.values():
            cookie = Cookie.Cookie(morsel.key,morsel.value)
            cookie.path = morsel["path"]
            Cookie.add_cookie(self.request,cookie)
        self.request.content_type = self.resp_headers["Content-type"] \
            or "text/html"
        self.output.seek(0)
        sent = 0
        while True:
            buff = self.output.read(HTTP.buf_size)
            if not buff:
                break
            try:
                self.wfile.write(buff)
                sent += len(buff)
            except socket.error:
                self.sock.close()
                return

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

def handler(req):
    k_handler = mp_handler(req)
    k_handler.read_request()
    k_handler.process_request()
    req.status = k_handler.code
    return apache.OK
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.