Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

root/trunk/cherrypy/lib/wsgiapp.py

Revision 1763 (checked in by fumanchu, 1 year ago)

Fix for #700 (Deprecate the wsgiapp Tool).

  • Property svn:eol-style set to native
Line 
1 """A CherryPy tool for hosting a foreign WSGI application."""
2
3 import sys
4 import warnings
5
6 import cherrypy
7
8
9 # is this sufficient for start_response?
10 def start_response(status, response_headers, exc_info=None):
11     cherrypy.response.status = status
12     headers_dict = dict(response_headers)
13     cherrypy.response.headers.update(headers_dict)
14
15 def make_environ():
16     """grabbed some of below from wsgiserver.py
17     
18     for hosting WSGI apps in non-WSGI environments (yikes!)
19     """
20    
21     request = cherrypy.request
22    
23     # create and populate the wsgi environ
24     environ = dict()
25     environ["wsgi.version"] = (1,0)
26     environ["wsgi.url_scheme"] = request.scheme
27     environ["wsgi.input"] = request.rfile
28     environ["wsgi.errors"] = sys.stderr
29     environ["wsgi.multithread"] = True
30     environ["wsgi.multiprocess"] = False
31     environ["wsgi.run_once"] = False
32     environ["REQUEST_METHOD"] = request.method
33     environ["SCRIPT_NAME"] = request.script_name
34     environ["PATH_INFO"] = request.path_info
35     environ["QUERY_STRING"] = request.query_string
36     environ["SERVER_PROTOCOL"] = request.protocol
37     environ["SERVER_NAME"] = request.local.name
38     environ["SERVER_PORT"] = request.local.port
39     environ["REMOTE_HOST"] = request.remote.name
40     environ["REMOTE_ADDR"] = request.remote.ip
41     environ["REMOTE_PORT"] = request.remote.port
42     # then all the http headers
43     headers = request.headers
44     environ["CONTENT_TYPE"] = headers.get("Content-type", "")
45     environ["CONTENT_LENGTH"] = headers.get("Content-length", "")
46     for (k, v) in headers.iteritems():
47         envname = "HTTP_" + k.upper().replace("-","_")
48         environ[envname] = v
49     return environ
50
51
52 def run(app, env=None):
53     """Run the given WSGI app and set response.body to its output."""
54     warnings.warn("This module is deprecated and will be removed in "
55                   "Cherrypy 3.2. See http://www.cherrypy.org/ticket/700 "
56                   "for more information.")
57    
58     try:
59         environ = cherrypy.request.wsgi_environ.copy()
60         environ['SCRIPT_NAME'] = cherrypy.request.script_name
61         environ['PATH_INFO'] = cherrypy.request.path_info
62     except AttributeError:
63         environ = make_environ()
64    
65     if env:
66         environ.update(env)
67    
68     # run the wsgi app and have it set response.body
69     response = app(environ, start_response)
70     try:
71         cherrypy.response.body = [x for x in response]
72     finally:
73         if hasattr(response, "close"):
74             response.close()
75    
76     return True
77
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets