Use mod_wsgi plugin to Apache.
You can do this to see how an existing script might be transformed into a WSGI application. This is a starting point, to show how the WSGI interface works.
import sys
def myWSGIApp( environ, start_response ):
with file( "temp", "w" ) as output:
sys.stdout= output
execfile( "some script.py" )
sys.stdout= __stdout__
status = '200 OK'
headers = [('Content-type', 'text/plain')]
start_response(status, headers)
result= file( "temp", "r" )
return result
Note that you can easily rewrite your scripts to conform to the WSGI
standard, also. This is still not quite the best approach.
If you had this
if __name__ == "__main__":
main()
You simply have to add something like this to each script.
def myWSGIApp( environ, start_response ):
with file( "temp", "w" ) as output:
sys.stdout= output
main()
sys.stdout= __stdout__
status = '200 OK'
headers = [('Content-type', 'text/plain')]
start_response(status, headers)
result= file( "temp", "r" )
return result
Then each script is callable as a WSGI application and can be plugged
into a WSGI-based framework.
The best approach is to rewrite your scripts so they do not use sys.stdout
, but write to a file that's passed to them as an argument.
A test version of your server can be this simple.
from wsgiref.simple_server import make_server
httpd = make_server('', 8000, myWSGIApp)
Once you have WSGI applications for your scripts, you can create an smarter
WSGI application that
Parses the URL. Updates the environ with the name of the script to run.
Runs your WSGI application with an appropriate environment.
Look at http://docs.python.org/library/wsgiref.html for information.
You can then configure Apache to use your WSGI server via mod_wsgi
.
Look at http://code.google.com/p/modwsgi/ for details.