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

root/tags/cherrypy-3.0.0/cherrypy/lib/wsgiapp.py

Revision 1460 (checked in by fumanchu, 2 years ago)

Renamed _cpwsgiserver.py to wsgiserver.py, to make it more clear that it's a public, reusable module. Also renamed _cpwsgi.WSGIServer to _cpwsgi.CPWSGIServer, to match the naming convention of the other subclasses in that module.

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

Hosted by WebFaction

Log in as guest/cpguest to create tickets