pycgi.py :  » Database » PyDO » skunkweb-3.4.4 » SkunkWeb » Services » 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 » Database » PyDO 
PyDO » skunkweb 3.4.4 » SkunkWeb » Services » pycgi.py
# Time-stamp: <02/07/29 12:41:03 smulloni>

########################################################################
#  
#  Copyright (C) 2002 Jacob Smullyan <smulloni@smullyan.org>
#  
#      You may distribute under the terms of either the GNU General
#      Public License or the SkunkWeb License, as specified in the
#      README file.
########################################################################

from SkunkWeb import ServiceRegistry,Configuration
from SkunkWeb.LogObj import DEBUG,logException
import AE.Cache
import vfs
import os, sys
import cStringIO
import rfc822

ServiceRegistry.registerService('pycgi')
PYCGI=ServiceRegistry.PYCGI

# use templating's 404 handler if it is already imported,
# or is about to be loaded, to the extent possible to determine.
# this is cut-and-pasted from rewrite.py, which is unfortunate;
# I should put this is a separate place, but where?

if sys.modules.has_key('templating') \
       or 'templating' in Configuration.services:
    import templating
    fourOhFourHandler=templating.Handler.fourOhFourHandler

else:
    def fourOhFourHandler(connection, sessionDict):
        connection.setStatus(404)
        connection.responseHeaders['Content-Type']='text/html'
        connection.write(
            'Sorry the requested document (<tt>%s</tt>) is not available' \
            % connection.uri)
        return  connection.response()

def _swap_streams(conn, saved=None):
    if not saved:
        saved=sys.stdout, sys.stderr, sys.stdin
        sys.stdout=sys.stderr=cStringIO.StringIO()
        sys.stdin=cStringIO.StringIO(conn._stdin)
        DEBUG(PYCGI, conn._stdin)
        return saved
    else:
        new=(sys.stdout, sys.stderr, sys.stdin)
        (sys.stdout, sys.stderr, sys.stdin)=saved
        return new
    

def _processRequest(conn, sessionDict):
    DEBUG(PYCGI, "pycgi processing request")
    try:
        env=_fix(conn.env, conn.uri)
    except vfs.FileNotFoundException:
        DEBUG(PYCGI, "file not found!")
        return fourOhFourHandler(conn, sessionDict)
    DEBUG(PYCGI, "file evidently exists")
    oldpwd=os.getcwd()
    os.environ.update(env)
    # what to do with argv?
    save_argv=sys.argv
    sys.argv=[env['SCRIPT_NAME']]
    saved=_swap_streams(conn)
    try:
        try:
            (directory, file) = os.path.split(env['SCRIPT_FILENAME'])
            os.chdir(directory)
            DEBUG(PYCGI, "about to execfile %s" % file)
            execfile(file)
        except SystemExit:
            pass
        except:
            logException()
            raise
    finally:
        os.chdir(oldpwd)
        sys.argv=save_argv
        new=_swap_streams(conn, saved)
    new[0].seek(0)
    respp = rfc822.Message(new[0])
    for k,v in respp.items():
        conn.responseHeaders[k] = v
    conn.write(new[0].read())        
    return conn.response()

    
def _fix(dict, uri):
    DEBUG(PYCGI, "in _fix")
    nd = {}
    for k,v in dict.items():
        nd[str(k)] = str(v)
    translated=AE.Cache._fixPath(Configuration.documentRoot, uri)
    fullpath, extra=Configuration.documentRootFS.split_extra(translated)
    if not fullpath:
        raise vfs.FileNotFoundException, translated
    # file exists
    if extra:
        nd['PATH_INFO']='/%s' % extra
    nd['PATH_TRANSLATED']=translated
    nd['SCRIPT_FILENAME']=fullpath
    nd['SCRIPT_NAME']=fullpath[len(Configuration.documentRoot):]
    return nd

def __initHooks():
    import web.protocol
    import SkunkWeb.constants as co
    jobGlob=co.PYCGI_JOB
    web.protocol.HandleConnection[jobGlob]=_processRequest

__initHooks()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.