2008/12/10 wmiller <[EMAIL PROTECTED]>:
>
> Is it possible configure mod_wsgi to recognize WSGI application script
> files without explicitly declaring the application object within each
> script file?  If not, is there a elegant/crude work-around?
>
> Along the same lines, if that were possible, it would be nice to also
> assume defaults for the  return variables of the start_response
> function so it (start_response) wouldn't have to be declared
> explicitly.  Then, any changes to the defaults or to the environ
> settings could optionally be set/declared and retrieved in the script
> file:
>
> <%
> # default status = '200 OK'
> start_response('403 Forbidden', [('Content-Type', 'text/html')])
> var = "Access Denied"
> %>
> <html>
> <body>
> {var}
> </body>
> </html>
>
> The above example also assumes there would be no need to explicitly
> return content because the processed script file itself would serve as
> the return content.  Admittedly, the goal is a bit unconventional
> having the template file serve also as the wsgi application script
> file with several assumptions built-in.  The motivation if it's not
> already obvious is to return to a one file per URL application
> environment along the lines of PHP (performance and security issues
> permitting).

I have been asked something similar a number of times. I don't know
why but it is always to me direct and not on the mailing list. Not
sure if it means the others were embarrassed about their yearnings for
something more like PHP or not. ;-)

Anyway, what you are wanting is not WSGI. That said, it doesn't mean
that you cannot write a WSGI application which acts as an intermediary
or proxy to interpret the files contents however you want. In effect
you are just creating a variant of a Python templating system. Rather
than you independently having to implement all the URL dispatch
yourself though, one can use aspects of Apache configuration to allow
Apache to do it for you. The result isn't strictly a pure WSGI
application as it will only work under Apache/mod_wsgi, but if you
don't care, then that is not an issue.

Quoting some bits from prior mails I have sent about this ....

The Apache configuration you want is:

 Action pyhp-scripts /pyhp-interpreter
 WSGIScriptAlias /pyhp-interpreter /some/path/pyhp.wsgi
 AddHandler pyhp-scripts .pyhp

You would then put your .pyhp files where ever you want in directories
covered by the AddHandler. When ever a request comes in for a file
with .pyhp extension, Apache will actually invoke WSGI application at
/pyhp-interpreter to handle the request.

The WSGI environ for the WSGI application would be something like:

DOCUMENT_ROOT: '/Library/WebServer/Documents'
GATEWAY_INTERFACE: 'CGI/1.1'
HTTP_ACCEPT: 
'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'
HTTP_ACCEPT_ENCODING: 'gzip, deflate'
HTTP_ACCEPT_LANGUAGE: 'en-us'
HTTP_CONNECTION: 'keep-alive'
HTTP_HOST: 'localhost'
HTTP_USER_AGENT: 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_5;
en-us) AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.2
Safari/525.20.1'
PATH: '/usr/bin:/bin:/usr/sbin:/sbin'
PATH_INFO: '/~grahamd/foo.pyhp'
PATH_TRANSLATED: '/Users/grahamd/Sites/foo.pyhp'
QUERY_STRING: ''
REDIRECT_HANDLER: 'pyhp-scripts'
REDIRECT_SCRIPT_URI: 'http://localhost/~grahamd/foo.pyhp'
REDIRECT_SCRIPT_URL: '/~grahamd/foo.pyhp'
REDIRECT_STATUS: '200'
REDIRECT_URL: '/~grahamd/foo.pyhp'
REMOTE_ADDR: '::1'
REMOTE_PORT: '50491'
REQUEST_METHOD: 'GET'
REQUEST_URI: '/~grahamd/foo.pyhp'
SCRIPT_FILENAME: '/Users/grahamd/Sites/echo.wsgi'
SCRIPT_NAME: '/pyhp-interpreter'
SCRIPT_URI: 'http://localhost/~grahamd/foo.pyhp'
SCRIPT_URL: '/~grahamd/foo.pyhp'
SERVER_ADDR: '::1'
SERVER_ADMIN: '[EMAIL PROTECTED]'
SERVER_NAME: 'localhost'
SERVER_PORT: '80'
SERVER_PROTOCOL: 'HTTP/1.1'
SERVER_SIGNATURE: ''
SERVER_SOFTWARE: 'Apache/2.2.9 (Unix) mod_ssl/2.2.9 OpenSSL/0.9.7l
DAV/2 mod_wsgi/3.0-TRUNK Python/2.5.1'
mod_wsgi.application_group: 'graham-dumpletons-imac-2.local|/pyhp-interpreter'
mod_wsgi.callable_object: 'application'
mod_wsgi.listener_host: ''
mod_wsgi.listener_port: '80'
mod_wsgi.process_group: '~grahamd'
mod_wsgi.script_reloading: '1'
mod_wsgi.version: (3, 0)
wsgi.errors: <mod_wsgi.Log object at 0x1007888d0>
wsgi.file_wrapper: <built-in method file_wrapper of mod_wsgi.Adapter
object at 0x100726a80>
wsgi.input: <mod_wsgi.Input object at 0x10077fcb0>
wsgi.multiprocess: False
wsgi.multithread: True
wsgi.run_once: False
wsgi.url_scheme: 'http'
wsgi.version: (1, 0)

The actual file which was the matched target of the request would be
in 'PATH_TRANSLATED'. The WSGI application would then read and process
that file however it wants. This might include it parsing file,
converting it into executable Python code, performing caching and only
rereading files from disk when target file changes etc etc. What you
do is really up to you at this point, but at least Apache has done the
URL dispatching to file based resources for you so you do not have to
worry about that.

Graham

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"modwsgi" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/modwsgi?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to