Configuring apache to execute python scripts using mod_python handler

Discussion in 'Python' started by joe jacob, Aug 13, 2007.

  1. joe jacob

    joe jacob Guest

    I configured apache to execute python scripts using mod_python
    handler. I followed below mentioned steps to configure apache.

    1. In http.conf I added

    <Directory "D:/softwares/Apache2.2/htdocs">
    AddHandler mod_python .py
    PythonHandler mptest
    PythonDebug On
    </Directory>

    2. Then I added the line "LoadModule python_module modules/
    mod_python.so" to http.conf.

    Then I tried execute the python script mentioned below from browser.

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

    Then I am getting the following error

    Traceback (most recent call last):

    File "D:\softwares\Python25\Lib\site-packages\mod_python
    \importer.py", line 1537, in HandlerDispatch
    default=default_handler, arg=req, silent=hlist.silent)

    File "D:\softwares\Python25\Lib\site-packages\mod_python
    \importer.py", line 1202, in _process_target
    module = import_module(module_name, path=path)

    File "D:\softwares\Python25\Lib\site-packages\mod_python
    \importer.py", line 304, in import_module
    return __import__(module_name, {}, {}, ['*'])

    ImportError: No module named mptest

    I am using Apache 2.2.4, python 2.5 and mod_python-3.3.1.win32-py2.5-
    Apache2.2.

    I am able to execute python scripts by configuring apache to execute
    the cgi scripts. But I want to execute it using mod_python as it is
    faster compared to cgi mode. Someone please help me on this issue.
     
    joe jacob, Aug 13, 2007
    #1
    1. Advertising

  2. joe jacob

    Guest

    Hi Joe

    You'd probably have better luck posting this to the mod python mailing
    list. Did you name your python script mptest.py and did you remember
    to restart Apache when you edited the httpd.conf file? If so then I
    don't see any reason why it shouldn't work although I've never tried
    mod_python under a Windows environment.

    Regards

    Jeffrey van Aswegen

    joe jacob wrote:
    > I configured apache to execute python scripts using mod_python
    > handler. I followed below mentioned steps to configure apache.
    >
    > 1. In http.conf I added
    >
    > <Directory "D:/softwares/Apache2.2/htdocs">
    > AddHandler mod_python .py
    > PythonHandler mptest
    > PythonDebug On
    > </Directory>
    >
    > 2. Then I added the line "LoadModule python_module modules/
    > mod_python.so" to http.conf.
    >
    > Then I tried execute the python script mentioned below from browser.
    >
    > from mod_python import apache
    > def handler(req):
    > req.content_type = 'text/plain'
    > req.write("Hello World!")
    > return apache.OK
    >
    > Then I am getting the following error
    >
    > Traceback (most recent call last):
    >
    > File "D:\softwares\Python25\Lib\site-packages\mod_python
    > \importer.py", line 1537, in HandlerDispatch
    > default=default_handler, arg=req, silent=hlist.silent)
    >
    > File "D:\softwares\Python25\Lib\site-packages\mod_python
    > \importer.py", line 1202, in _process_target
    > module = import_module(module_name, path=path)
    >
    > File "D:\softwares\Python25\Lib\site-packages\mod_python
    > \importer.py", line 304, in import_module
    > return __import__(module_name, {}, {}, ['*'])
    >
    > ImportError: No module named mptest
    >
    > I am using Apache 2.2.4, python 2.5 and mod_python-3.3.1.win32-py2.5-
    > Apache2.2.
    >
    > I am able to execute python scripts by configuring apache to execute
    > the cgi scripts. But I want to execute it using mod_python as it is
    > faster compared to cgi mode. Someone please help me on this issue.
     
    , Aug 13, 2007
    #2
    1. Advertising

  3. joe jacob

    7stud Guest

    On Aug 13, 5:16 am, joe jacob <> wrote:
    > I configured apache to execute python scripts using mod_python
    > handler. I followed below mentioned steps to configure apache.
    >
    > 1. In http.conf I added
    >
    > <Directory "D:/softwares/Apache2.2/htdocs">
    > AddHandler mod_python .py
    > PythonHandler mptest
    > PythonDebug On
    > </Directory>
    >
    > 2. Then I added the line "LoadModule python_module modules/
    > mod_python.so" to http.conf.
    >
    > Then I tried execute the python script mentioned below from browser.
    >
    > from mod_python import apache
    > def handler(req):
    > req.content_type = 'text/plain'
    > req.write("Hello World!")
    > return apache.OK
    >
    > Then I am getting the following error
    >
    > Traceback (most recent call last):
    >
    > File "D:\softwares\Python25\Lib\site-packages\mod_python
    > \importer.py", line 1537, in HandlerDispatch
    > default=default_handler, arg=req, silent=hlist.silent)
    >
    > File "D:\softwares\Python25\Lib\site-packages\mod_python
    > \importer.py", line 1202, in _process_target
    > module = import_module(module_name, path=path)
    >
    > File "D:\softwares\Python25\Lib\site-packages\mod_python
    > \importer.py", line 304, in import_module
    > return __import__(module_name, {}, {}, ['*'])
    >
    > ImportError: No module named mptest
    >
    > I am using Apache 2.2.4, python 2.5 and mod_python-3.3.1.win32-py2.5-
    > Apache2.2.
    >
    > I am able to execute python scripts by configuring apache to execute
    > the cgi scripts. But I want to execute it using mod_python as it is
    > faster compared to cgi mode. Someone please help me on this issue.


    1) In the mod_python tutorial it says:

    -------
    2.4 Testing

    ....
    ....

    2. Add the following Apache directives, which can appear in either the
    main server configuration file, or .htaccess. If you are going to be
    using the .htaccess file, you will not need the <Directory> tag below
    (the directory then becomes the one in which the .htaccess file is
    located), ***and you will need to make sure the AllowOverride
    directive applicable to this directory has at least FileInfo
    specified. (The default is None, which will not work.)****

    <Directory /some/directory/htdocs/test>
    AddHandler mod_python .py
    PythonHandler mptest
    PythonDebug On
    </Directory>
    --------

    Note the last sentence in the directions above. I explain what is
    needed for that part at the link in (2) below. Also note the
    directory listed in the opening <Directory> tag:

    /some/directory/htdocs/test

    That specifies a sub directory of htdocs. That's because the htdocs
    directory has its own <Directory> tag in httpd.conf, which specifies
    the things you can to with it. If you look around in httpd.conf, you
    will see the <Directory> tag that applies to htdocs. Mine looks like
    this:

    <Directory "/Library/Apache2/htdocs">
    ....
    ....
    <Directory>

    The mod_python Testing tutorial wants you to create your own sub
    directory in htdocs, so that you can specify your own rules for that
    directory. You're probably getting errors because you have two
    <Directory> tags in your httpd.conf file for the htdocs directory, and
    the second tag is overwriting the first one.


    2) You can see the latter part of this thread for what I did to get
    mptest.py to work:

    http://groups.google.com/group/comp...?lnk=gst&q=mod_python&rnum=1#ea2645ec03370fb2


    3) I put any <Directory> tags I added to httpd.conf below the first
    <Directory> tag in http.conf, which is this one:

    <Directory "/Library/Apache2/htdocs">

    so that the ones lower down in the file will override the previous
    tags if there is a conflict.
     
    7stud, Aug 13, 2007
    #3
  4. joe jacob

    joe jacob Guest

    On Aug 13, 9:44 pm, 7stud <> wrote:
    > On Aug 13, 5:16 am, joe jacob <> wrote:
    >
    >
    >
    > > I configured apache to execute python scripts using mod_python
    > > handler. I followed below mentioned steps to configure apache.

    >
    > > 1. In http.conf I added

    >
    > > <Directory "D:/softwares/Apache2.2/htdocs">
    > > AddHandler mod_python .py
    > > PythonHandler mptest
    > > PythonDebug On
    > > </Directory>

    >
    > > 2. Then I added the line "LoadModule python_module modules/
    > > mod_python.so" to http.conf.

    >
    > > Then I tried execute the python script mentioned below from browser.

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

    >
    > > Then I am getting the following error

    >
    > > Traceback (most recent call last):

    >
    > > File "D:\softwares\Python25\Lib\site-packages\mod_python
    > > \importer.py", line 1537, in HandlerDispatch
    > > default=default_handler, arg=req, silent=hlist.silent)

    >
    > > File "D:\softwares\Python25\Lib\site-packages\mod_python
    > > \importer.py", line 1202, in _process_target
    > > module = import_module(module_name, path=path)

    >
    > > File "D:\softwares\Python25\Lib\site-packages\mod_python
    > > \importer.py", line 304, in import_module
    > > return __import__(module_name, {}, {}, ['*'])

    >
    > > ImportError: No module named mptest

    >
    > > I am using Apache 2.2.4, python 2.5 and mod_python-3.3.1.win32-py2.5-
    > > Apache2.2.

    >
    > > I am able to execute python scripts by configuring apache to execute
    > > the cgi scripts. But I want to execute it using mod_python as it is
    > > faster compared to cgi mode. Someone please help me on this issue.

    >
    > 1) In the mod_python tutorial it says:
    >
    > -------
    > 2.4 Testing
    >
    > ...
    > ...
    >
    > 2. Add the following Apache directives, which can appear in either the
    > main server configuration file, or .htaccess. If you are going to be
    > using the .htaccess file, you will not need the <Directory> tag below
    > (the directory then becomes the one in which the .htaccess file is
    > located), ***and you will need to make sure the AllowOverride
    > directive applicable to this directory has at least FileInfo
    > specified. (The default is None, which will not work.)****
    >
    > <Directory /some/directory/htdocs/test>
    > AddHandler mod_python .py
    > PythonHandler mptest
    > PythonDebug On
    > </Directory>
    > --------
    >
    > Note the last sentence in the directions above. I explain what is
    > needed for that part at the link in (2) below. Also note the
    > directory listed in the opening <Directory> tag:
    >
    > /some/directory/htdocs/test
    >
    > That specifies a sub directory of htdocs. That's because the htdocs
    > directory has its own <Directory> tag in httpd.conf, which specifies
    > the things you can to with it. If you look around in httpd.conf, you
    > will see the <Directory> tag that applies to htdocs. Mine looks like
    > this:
    >
    > <Directory "/Library/Apache2/htdocs">
    > ...
    > ...
    > <Directory>
    >
    > The mod_python Testing tutorial wants you to create your own sub
    > directory in htdocs, so that you can specify your own rules for that
    > directory. You're probably getting errors because you have two
    > <Directory> tags in your httpd.conf file for the htdocs directory, and
    > the second tag is overwriting the first one.
    >
    > 2) You can see the latter part of this thread for what I did to get
    > mptest.py to work:
    >
    > http://groups.google.com/group/comp.lang.python/browse_thread/thread/...
    >
    > 3) I put any <Directory> tags I added to httpd.conf below the first
    > <Directory> tag in http.conf, which is this one:
    >
    > <Directory "/Library/Apache2/htdocs">
    >
    > so that the ones lower down in the file will override the previous
    > tags if there is a conflict.


    Thank you very much I got it working.
     
    joe jacob, Aug 14, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. neha
    Replies:
    1
    Views:
    532
    Brad Teale
    Oct 19, 2005
  2. blbmdsmith
    Replies:
    1
    Views:
    679
    Graham Dumpleton
    Dec 14, 2006
  3. davidj411
    Replies:
    0
    Views:
    578
    davidj411
    Jun 27, 2008
  4. Samuel
    Replies:
    0
    Views:
    270
    Samuel
    Dec 28, 2008
  5. Íßêïò
    Replies:
    2
    Views:
    135
    Íßêïò
    Apr 9, 2007
Loading...

Share This Page