Changeset 20
- Timestamp:
- 12/05/05 12:37:58
- Files:
-
- modpython_gateway.py (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
modpython_gateway.py
r19 r20 1 """ 2 WSGI wrapper for mod_python. Requires Python 2.2 or greater. 3 4 5 Example httpd.conf section for a CherryPy app called "mcontrol": 6 7 <Directory D:\htdocs\mcontrol> 8 SetHandler python-program 9 PythonHandler wsgiref.modpy_wrapper::handler 10 PythonOption application cherrypy.wsgiapp::wsgiApp 11 PythonOption import mcontrol.cherry::startup 12 </Directory> 13 14 """ 1 15 2 16 import sys … … 6 20 7 21 class InputWrapper(object): 8 22 9 23 def __init__(self, req): 10 24 self.req = req 11 25 12 26 def close(self): 13 27 pass 14 28 15 29 def read(self, size=-1): 16 30 return self.req.read(size) 17 31 18 32 def readline(self): 19 33 return self.req.readline() 20 34 21 35 def readlines(self, hint=-1): 22 36 return self.req.readlines(hint) 23 37 24 38 def __iter__(self): 25 39 line = self.readline() … … 32 46 33 47 class ErrorWrapper(object): 34 48 35 49 def __init__(self, req): 36 50 self.req = req 37 51 38 52 def flush(self): 39 53 pass 40 54 41 55 def write(self, msg): 42 56 self.req.log_error(msg) 43 57 44 58 def writelines(self, seq): 45 59 self.write(''.join(seq)) … … 47 61 48 62 bad_value = ("You must provide a PythonOption '%s', either 'on' or 'off', " 49 "when running a version of mod_python <3.1")63 "when running a version of mod_python < 3.1") 50 64 51 65 class Handler(BaseCGIHandler): 52 66 53 67 def __init__(self, req): 54 68 options = req.get_options() 55 69 56 70 # Threading and forking 57 71 try: … … 65 79 else: 66 80 raise ValueError(bad_value % "multithread") 67 81 68 82 forked = options.get('multiprocess', '').lower() 69 83 if forked == 'on': … … 76 90 threaded = q(apache.AP_MPMQ_IS_THREADED) 77 91 forked = q(apache.AP_MPMQ_IS_FORKED) 78 92 79 93 env = dict(apache.build_cgi_env(req)) 80 94 81 95 if req.headers_in.has_key("authorization"): 82 96 env["HTTP_AUTHORIZATION"] = req.headers_in["authorization"] 83 97 84 98 BaseCGIHandler.__init__(self, 85 99 stdin=InputWrapper(req), … … 92 106 self.request = req 93 107 self._write = req.write 94 108 95 109 def _flush(self): 96 110 pass 97 111 98 112 def send_headers(self): 99 113 self.cleanup_headers() 100 114 self.headers_sent = True 101 115 # Can't just return 200 or the page will hang until timeout 102 s = int(self.status[:3]) 103 if s == 200: 104 self.finalstatus = apache.OK 105 else: 106 self.finalstatus = s 116 self.request.status = int(self.status[:3]) 107 117 # the headers.Headers class doesn't have an iteritems method... 108 118 for key, val in self.headers.items(): 109 119 if key.lower() == 'content-length': 110 self.request.set_content_length(int(val)) 120 if val is not None: 121 self.request.set_content_length(int(val)) 111 122 elif key.lower() == 'content-type': 112 123 self.request.content_type = val 113 self.request.headers_out[key] = val 124 else: 125 self.request.headers_out[key] = val 114 126 115 127 … … 134 146 config = req.get_config() 135 147 debug = int(config.get("PythonDebug", 0)) 136 148 137 149 options = req.get_options() 138 150 … … 144 156 # be called per request, must be designed to run setup code only on the 145 157 # first request (a global 'first_request' flag is usually enough). 146 atoms = options['import'].split('::', 1) 147 modname = atoms.pop(0) 148 module = __import__(modname, globals(), locals(), ['']) 149 if atoms: 150 func = getattr(module, atoms[0]) 151 func(req) 158 import_opt = options.get('import') 159 if import_opt: 160 atoms = import_opt.split('::', 1) 161 modname = atoms.pop(0) 162 module = __import__(modname, globals(), locals(), ['']) 163 if atoms: 164 func = getattr(module, atoms[0]) 165 func(req) 152 166 153 167 # Import the wsgi 'application' callable and pass it to Handler.run … … 155 169 module = __import__(modname, globals(), locals(), ['']) 156 170 app = getattr(module, objname) 157 171 172 ## modname, objname = options['application'].split('::') 173 ## module = apache.import_module(modname, autoreload=False, log=debug) 174 ## app = apache.resolve_object(module, objname, arg=None, silent=False) 175 176 # You can un-comment the next line to get the value of req.interpreter. 177 # If you use a PythonImport directive as recommended, its interpreter_name 178 # value must EXACTLY match the value of req.interpreter (case-sensitive). 179 ## raise StandardError(repr(req.interpreter)) 180 158 181 h = Handler(req) 159 182 ## from paste import lint 160 183 ## app = lint.middleware(app) 161 184 h.run(app) 185 186 # status was set in Handler.send_headers(); always return apache.OK 187 return apache.OK 162 188 163 # finalstatus was set in Handler.send_headers()164 return h.finalstatus165
