Python on WAMP5: .htaccess Allows Browser to Run .PY , But Loses .HTM; How to Make Both Work?

To get Python.py files to execute as CGI scripts on WAMP localhost, I put these statements in my .htaccess file in the   www  directory:
     SetHandler mod_python
     PythonHandler mod_python.cgihandler

That allows the .py files to execute when called from the browser, like "http://localhost/helloworld.py",
but the problem is:  those .htaccess statements are making the server think all the html files are gone.  When trying to view an html web page on localhost, it says the file cannot be found.
    But when I comment out those statements in .htaccess, the html files work again.
    How can the .htaccess  statements be modified to allow both .py and .htm / .html (and .php , .pl , .cgi ) to work simultaneously?
Randall-BAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RichieHindleCommented:
Try this:

<LocationMatch "\.py$">
SetHandler mod_python
PythonHandler mod_python.cgihandler
</LocationMatch>
0
Randall-BAuthor Commented:
Hmm. That did not work; got the dreaded 500 error. Do think this would work:  AddHandler cgi-script .py  ?
0
Randall-BAuthor Commented:
I ended up taking out the lines (listed in my question) from .htaccess.  Then I put   AddHandler cgi-script .py  in .htaccess,  and it worked well.  Even better, I got rid of the .htacess file altogether, and simply added   AddHandler cgi-script .py   into the httpd.conf file, in this part

<Directory "d:/wamp/www/">  # adjust to c:/ if applicable
  AddHandler python-program .py
  AddHandler cgi-script .py   #  <---------

  PythonDebug On
    <Files *.py>
        PythonPath "['d:/Python24/'] + sys.path"
    </Files>
    <Files "index.py">
      PythonHandler index
    </Files>
    <Files "xa.py">
      PythonHandler xa
    </Files>
    PythonHandler default
#  PythonHandler mod_python.publisher
</Directory>

So the complete way to get Python working on WAMP seems to be:  

1) Install WAMP from wampserver.com/en and follow all the directions at http://www.mplstutorial.com/wamp_python/python_wamp.html ;  

2)  Insert    AddHandler cgi-script .py   into the httpd.conf file;  

3)  Stop and restart the WAMP Server;  

4) use the physical path to Python  in the shebang line  (  #!D:\Python24\python  or  #!D:\Python24\python.exe  [[but adjust to C:\ if that's where it is installed ); and

5) include the content header, like
   print "Content-Type: text/plain\n\n"    or
   print "Content-Type: text/html\n\n"
immediately after the bang line.

Hope this helps others.

Thanks for leading me to find that.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

RichieHindleCommented:
Fair enough, but using plain old CGI will be many times slower than using mod_python.
0
Randall-BAuthor Commented:
I agree.  Although it was nice to see it working at all, through CGI, I certainly prefer the speed of using the mod_python built into WAMP's Apache server.
   Any further suggestions on how to make that work without blocking access to .htm files?  Thanks.
0
RichieHindleCommented:
Please post the exact "dreaded 500 error" you got (it's in your Apache error log).
0
Randall-BAuthor Commented:
It was:
   [alert] [client 127.0.0.1] D:/wamp/www/.htaccess: <LocationMatch not allowed here

And that happened for any kind of page (.htm, .py, etc.)
0
Randall-BAuthor Commented:
I also tried adding this line to the httpd.conf file:
    AddHandler mod_python .py

But it gives this error:
--------------------------
Mod_python error: "PythonHandler default"

Traceback (most recent call last):

  File "D:\Python24\Lib\site-packages\mod_python\apache.py", line 287, in HandlerDispatch
    log=debug)

  File "D:\Python24\Lib\site-packages\mod_python\apache.py", line 461, in import_module
    f, p, d = imp.find_module(parts[i], path)

ImportError: No module named default
-----------------------

Could the solution possibly involve a modification to mod_python itself?    
0
RichieHindleCommented:
I've added this to my httpd.conf:

Alias /Randall/  "C:/temp/ee-modpy/"
<Directory "C:/temp/ee-modpy/">
    Allow from all
</Directory>
<LocationMatch "/Randall/.*\.py$">
    SetHandler mod_python
    PythonHandler mod_python.cgihandler
</LocationMatch>

and I've created C:/temp/ee-modpy/test.py:

print "Content-Type: text/plain\n\n"
print "Hello Python world!"

and C:/temp/ee-modpy/test.html:

<html><body>
<p>Hello HTML world!</p>
</body></html>

Now, http://localhost/Randall/test.html shows "Hello HTML world!", and http://localhost/Randall/test.py shows "Hello Python world!".

So I think that gives you what you want.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Randall-BAuthor Commented:
Thanks. I followed that setup and restarted Apache.  I discovered the test URL has to be like:
   http://localhost/Randall/test.py and
   http://localhost/Randall/test.htm
and that works, but it requires appending the alias to the end of localhost.

Then I tested the following, and it allowed using the localhost url without appending a subdirectory to the localhost URL:

    Alias /  "C:/temp/ee-modpy/"
    <Directory "C:/temp/ee-modpy/">
        Allow from all
    </Directory>
    <LocationMatch "/.*\.py$">
        SetHandler mod_python
        PythonHandler mod_python.cgihandler
    </LocationMatch>


And the following allowed it to run scripts within the WAMP www directory (instead of a temp folder):

   Alias /  "d:/wamp/www/"
    <Directory "d:/wamp/www/">
       Allow from all
   </Directory>
   <LocationMatch "/.*\.py$">
      SetHandler mod_python
      PythonHandler mod_python.cgihandler
   </LocationMatch>

So that seems to be the way of getting mod_python to work on the localhost www root.  
    I appreciate your sticking with me on this question until it was resolved. Thanks.
0
RichieHindleCommented:
Happy to help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.