Setting up Python on Windows/ Apache?

20,129

Solution 1

Stay away from mod_python. One common misleading idea is that mod_python is like mod_php, but for python. That is not true. Wsgi is the standard to run python web applications, defined by PEP 333. So use mod_wsgi instead.

Or alternatively, use some web framework that has a server. Cherrypy's one is particulary good. You will be able to run your application both standalone and through mod_wsgi.

An example of Hello World application using cherrypy:

import cherrypy

class HelloWorld(object):
    def index(self):
        return "Hello World!"
    index.exposed = True

application = HelloWorld()
if __name__ == '__main__':
    cherrypy.engine.start()
    cherrypy.engine.block()

Very easy huh? Running this application directly on python will start a webserver. Configuring mod_wsgi to it will make it run inside apache.

Solution 2

You do not NEED mod_python to run Python code on the web, you could use simple CGI programming to run your python code, with the instructions in the following link: http://www.imladris.com/Scripts/PythonForWindows.html

That should give you some of the configuration options you need to enable Python with CGI, and a google search should give you reams of other info on how to program in it and such.

Mod_python is useful if you want a slightly more "friendly" interface, or more control over the request itself. You can use it to create request filters and other things for the Apache server, and with the publisher handler you get a simpler way of handling webpage requests via python.

The publisher handler works by mapping URLs to Python objects/functions. This means you can define a function named 'foo' in your python file, and any request to http://localhost/foo would call that function automatically. More info here: http://www.modpython.org/live/current/doc-html/hand-pub-alg-trav.html

As for the Apache config to make things work, something like this should serve you well

<Directory /var/www/html/python/>
  SetHandler mod_python
  PythonHandler mod_python.publisher
  PythonDebug On
</Directory>

If you have /var/www/html/ set up as your web server's root and have a file called index.py in the python/ directory in there, then any request to http://localhost/python/foo should call the foo() function in index.py, or fail with a 404 if it doesn't exist.

Share:
20,129
Philipp Lenssen
Author by

Philipp Lenssen

Dev at Anyland &amp; Manyland

Updated on August 01, 2022

Comments

  • Philipp Lenssen
    Philipp Lenssen almost 2 years

    I want to get a simple Python "hello world" web page script to run on Windows Vista/ Apache but hit different walls. I'm using WAMP. I've installed mod_python and the module shows, but I'm not quite sure what I'm supposed to do in e.g. http.conf (things like AddHandler mod_python .py either bring me to a file not found, or a forbidden, or module not found errors when accessing http://localhost/myfolder/index.py). I can get mod_python.publisher to work but do I "want" this/ need this?

    Can anyone help?

    Thanks!

  • Mohan Gulati
    Mohan Gulati over 14 years
    Running mod_wsgi application on Apache is fairly straight forward. I definitely recommend using CherryPy and it works well with Apache and mod_wsgi, even though it has its own light-wieght server.
  • Shahid Karimi
    Shahid Karimi over 12 years
    Tell in simplest way how to run python files from apache
  • Shahid Karimi
    Shahid Karimi over 12 years
    I got this error: Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, [email protected] and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log. Apache/2.2.4 (Win32) PHP/5.2.1 Server at localhost Port 80
  • Sami
    Sami about 7 years
    Same Error as @MarthaJames and log says that system cannot find file at d:/wamp/www/test.py but it exists there