• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 979
  • Last Modified:

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?
0
Randall-B
Asked:
Randall-B
  • 6
  • 5
1 Solution
 
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
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
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
 
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now