ns_setup.py :  » Network » PyWX » PyWX-1.0b2 » pywx-lib » 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 » Network » PyWX 
PyWX » PyWX 1.0b2 » pywx lib » ns_setup.py
# $Id: ns_setup.py,v 1.2 2002/12/12 19:49:02 titus Exp $

import sys, os, string, types
import Ns

PyWX_Version = '1.0b2' # also set in ns_pywx.cc

# first things first... Break link and clear environment (except PATH):
orig_environ = os.environ
os.environ = {'PATH':os.environ['PATH']}

def setup_env_strings():
    conn = Ns.GetConn()

    # os.environ is set (on first import) to be a new dictionary, which
    # breaks the connection between os.environ and the UNIX environment
    #
    # Here, we empty it of all variables but PATH and refill it; this
    # prevents a situation where interpreter reuse could lead to a
    # process getting an environment with leftover elements in it.
    #
    # Note that we don't want to reassign os.environ to a new dictionary,
    # because then modules that are using os.environ already will not
    # get the modifications; we want to modify in place.

    env = os.environ                    # nickname
    create_cgi_environ(conn, env)

#
# create_cgi_environ
#

def create_cgi_environ(conn, env):
    env.clear()
    
    # I don't understand the reason for this change (MRH): ###
    env['PATH'] = Ns.PageRoot(conn.Server()) 
    #env['PATH'] = orig_environ['PATH']  # keep PATH

    # The default search path now is whatever AOLserver set it to.
    Ns.Log(Ns.Debug,
           'PyWX: os.environ[\'PATH\'] unchanged (%s)' % (env['PATH'],))

    # Set some variables in the namespace for potential script use
    env['PYWX_VERSION'] = PyWX_Version
    env['DOCUMENT_ROOT'] = Ns.PageRoot(conn.Server())

    # Include info about our PyWX module
    env['SERVER_SOFTWARE'] = '%s/%s-PyWX/%s' % (
        Ns.InfoServerName(),
        Ns.InfoServerVersion(),
        PyWX_Version,
        )

    # Server Build date
    env['SERVER_BUILDDATE'] = Ns.InfoBuildDate()

    # Script Name:
    try:
        path_trans = Ns.UrlToFile(conn.Server(), conn.request.url)
        env['PATH_TRANSLATED'] = path_trans
        env['SCRIPT_NAME'] = conn.request.url
        env['PATH_INFO'] = ''
    except RuntimeError:
        env['SCRIPT_NAME'] = ''
        env['PATH_INFO'] = ''
        env['PATH_TRANSLATED'] = ''

    # Set the HTTP_* header environment variables:
    headers = conn.Headers()
    for i in range(headers.Size()):
        key = string.replace(headers.Key(i), '-', '_')
        key = 'HTTP_' + string.upper(key)
        env[key] = headers.Value(i)

    # Connection Protocol:
    env['SERVER_PROTOCOL'] = 'HTTP/%4.2f' % (conn.request.version,)
    env['AUTH_TYPE'] = 'Basic'
    env['GATEWAY_INTERFACE'] = 'CGI/1.1'

    # Content Length/Content Type:
    content = headers.IGet('Content-type')
    if content is None:
        if conn.request.method == 'POST':
            content = 'application/x-www-form-urlencoded'
        else:
            content = ''
    env['CONTENT_TYPE'] = content

    if conn.ContentLength() <= 0:
        env['CONTENT_LENGTH'] = ''
    else:
        env['CONTENT_LENGTH'] = '%u' % (conn.ContentLength(),)

    env['REQUEST_METHOD'] = conn.request.method
    env['QUERY_STRING'] = conn.request.query or ''

    # Remote Address:
    connData = conn.Peer()
    if connData is not None:
        env['REMOTE_ADDR'] = connData
        env['REMOTE_HOST'] = Ns.GetHostByAddr(connData) or connData

    hostaddr = conn.Host()
    env['SERVER_NAME'] = Ns.GetHostByAddr(hostaddr) or hostaddr

    # Server port (from AOLserver nscgi module logic)
    connData = conn.Location()
    port = None
    if connData is not None:
        try:
            port = Ns.ParseUrl(connData)[2]
        except RuntimeError:
            pass
    # If no port found, use default of '80':
    env['SERVER_PORT'] = port or '80'

    # Remote User
    if conn.authUser is not None:
        env['REMOTE_USER'] = conn.authUser

#
#
# N.B.
#
# It seems that multiple threads share sys.argv but multiple
# interpreters do not, so there is no problem with sys.argv
# being set.
#

def setup_cmd_ln_args(filename):
    """Break up the query string and use it to set sys.argv."""

    import sys

    sys.argv = [filename]

    arg = Ns.GetConn().request.query

    Ns.Log(Ns.Debug, "PyWX: request.query is %s (%s)"
           % (arg, Ns.GetConn().request.method))

    # If there are no parameters in the query, and this is not a FORM
    # submission, do not set any further arguments.
    if not arg:
        return

    sys.argv.append(arg)

    Ns.Log(Ns.Debug, "PyWX: CGI argument is %s" % (arg))

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