Sivaram Kannan | 8 Oct 06:24

Python - Apache2 doubt

Hi all,

I have a doubt in accessing python through Apache. I have the
following configuration in my apache2/site-default/default file.

 <Directory /var/www/python>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
                AddHandler mod_python .py
                PythonHandler first
                PythonDebug on
</Directory>

My /var/www/first.py file looks like this.

    def handler(req):
    req.content_type = "text/plain"
    req.write("Hello World!")
    return apache.OK

def get_time():

        html = """
                <html><head>
                <title>get_time function</title>
                </head>
                <body>
                        <h1>get_time function</h1>
(Continue reading)

Bhuvaneswaran A | 9 Oct 17:32

Re: Python - Apache2 doubt

On Wed, Oct 8, 2008 at 9:57 AM, Sivaram Kannan <siva.devel@...> wrote:
> Hi all,
> When I give the following url "http://localhost/first.py" the function
> handler is getting executed. But when I give the url
> "http://localhost/first.py/get_time" the function get_time is not
> getting called. What should I do to call anyfunction in a python
> script through mod_python?

Instead of using your own handler, i think you should use
mod_python.publisher as your handler. In addition, whatever you want
to print, you should return it from your method and the publisher
handler would do the rest to display in the browser. I've not used
publisher class, but I think it works this way.

If you rewrite your http configuration snippet and code as follows,
you should be able to access get_time() function using
http://localhost/first.py/get_time url:

HTTP CONFIGURATION SNIPPET
    <Directory "/var/www/python/">
       Options Indexes FollowSymLinks MultiViews
       AllowOverride None
       Order allow,deny
       allow from all
       SetHandler mod_python
       PythonHandler mod_python.publisher
       PythonDebug on
    </Directory>

REWRITTEN first.py SCRIPT
(Continue reading)


Gmane