wsgiapp.py :  » Web-Server » CherryPy » CherryPy-3.1.2 » cherrypy » 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 » Web Server » CherryPy 
CherryPy » CherryPy 3.1.2 » cherrypy » lib » wsgiapp.py
"""A CherryPy tool for hosting a foreign WSGI application."""

import sys
import warnings

import cherrypy


# is this sufficient for start_response?
def start_response(status, response_headers, exc_info=None):
    cherrypy.response.status = status
    headers_dict = dict(response_headers)
    cherrypy.response.headers.update(headers_dict)

def make_environ():
    """grabbed some of below from wsgiserver.py
    
    for hosting WSGI apps in non-WSGI environments (yikes!)
    """
    
    request = cherrypy.request
    
    # create and populate the wsgi environ
    environ = dict()
    environ["wsgi.version"] = (1,0)
    environ["wsgi.url_scheme"] = request.scheme
    environ["wsgi.input"] = request.rfile
    environ["wsgi.errors"] = sys.stderr
    environ["wsgi.multithread"] = True
    environ["wsgi.multiprocess"] = False
    environ["wsgi.run_once"] = False
    environ["REQUEST_METHOD"] = request.method
    environ["SCRIPT_NAME"] = request.script_name
    environ["PATH_INFO"] = request.path_info
    environ["QUERY_STRING"] = request.query_string
    environ["SERVER_PROTOCOL"] = request.protocol
    environ["SERVER_NAME"] = request.local.name
    environ["SERVER_PORT"] = request.local.port
    environ["REMOTE_HOST"] = request.remote.name
    environ["REMOTE_ADDR"] = request.remote.ip
    environ["REMOTE_PORT"] = request.remote.port
    # then all the http headers
    headers = request.headers
    environ["CONTENT_TYPE"] = headers.get("Content-type", "")
    environ["CONTENT_LENGTH"] = headers.get("Content-length", "")
    for (k, v) in headers.iteritems():
        envname = "HTTP_" + k.upper().replace("-","_")
        environ[envname] = v
    return environ


def run(app, env=None):
    """Run the given WSGI app and set response.body to its output."""
    warnings.warn("This module is deprecated and will be removed in "
                  "Cherrypy 3.2. See http://www.cherrypy.org/ticket/700 "
                  "for more information.")
    
    try:
        environ = cherrypy.request.wsgi_environ.copy()
        environ['SCRIPT_NAME'] = cherrypy.request.script_name
        environ['PATH_INFO'] = cherrypy.request.path_info
    except AttributeError:
        environ = make_environ()
    
    if env:
        environ.update(env)
    
    # run the wsgi app and have it set response.body
    response = app(environ, start_response)
    try:
        cherrypy.response.body = [x for x in response]
    finally:
        if hasattr(response, "close"):
            response.close()
    
    return True

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