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

root/branches/cp3-wsgi-remix/lib/wsgiapp.py

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

Replaced request.remote_addr, remote_port, and remote_host with a single "remote" attribute, an instance of lib.http.Host, which has "ip", "port" and "name" attributes. Added a similar request.local attribute. Changed request() signature to (local, remote, scheme). This allows requests run behind multiple HTTP servers to know the address info for their particular connection.

Line 
1 """a WSGI application tool for CherryPy"""
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 _cpwsgiserver.py
16     
17     for hosting WSGI apps in non-WSGI environments (yikes!)
18     """
19    
20     # create and populate the wsgi environment
21     environ = dict()
22     environ["wsgi.version"] = (1,0)
23     environ["wsgi.url_scheme"] = cherrypy.request.scheme
24     environ["wsgi.input"] = cherrypy.request.rfile
25     environ["wsgi.errors"] = sys.stderr
26     environ["wsgi.multithread"] = True
27     environ["wsgi.multiprocess"] = False
28     environ["wsgi.run_once"] = False
29     environ["REQUEST_METHOD"] = cherrypy.request.method
30     environ["SCRIPT_NAME"] = cherrypy.request.script_name
31     environ["PATH_INFO"] = cherrypy.request.path_info
32     environ["QUERY_STRING"] = cherrypy.request.query_string
33     environ["SERVER_PROTOCOL"] = cherrypy.request.protocol
34     environ["SERVER_NAME"] = cherrypy.request.local.name
35     environ["SERVER_PORT"] = cherrypy.request.local.port
36     environ["REMOTE_HOST"] = cherrypy.request.remote.name
37     environ["REMOTE_ADDR"] = cherrypy.request.remote.ip
38     environ["REMOTE_PORT"] = cherrypy.request.remote.port
39     # then all the http headers
40     headers = cherrypy.request.headers
41     environ["CONTENT_TYPE"] = headers.get("Content-type", "")
42     environ["CONTENT_LENGTH"] = headers.get("Content-length", "")
43     for (k, v) in headers.iteritems():
44         envname = "HTTP_" + k.upper().replace("-","_")
45         environ[envname] = v
46     return environ
47
48
49 def run(app, env=None):
50     """Run the (WSGI) app and set response.body to its output"""
51     try:
52         environ = cherrypy.request.wsgi_environ
53         environ['SCRIPT_NAME'] = cherrypy.request.script_name
54         environ['PATH_INFO'] = cherrypy.request.path_info
55     except AttributeError:
56         environ = make_environ()
57    
58     if env:
59         environ.update(env)
60    
61     # run the wsgi app and have it set response.body
62     cherrypy.response.body = app(environ, start_response)
63    
64     return True
65
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets