Solved

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

Posted on 2007-04-06
11
955 Views
Last Modified: 2012-06-27
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
Comment
Question by:Randall-B
  • 6
  • 5
11 Comments
 
LVL 14

Expert Comment

by:RichieHindle
ID: 18866356
Try this:

<LocationMatch "\.py$">
SetHandler mod_python
PythonHandler mod_python.cgihandler
</LocationMatch>
0
 

Author Comment

by:Randall-B
ID: 18866883
Hmm. That did not work; got the dreaded 500 error. Do think this would work:  AddHandler cgi-script .py  ?
0
 

Author Comment

by:Randall-B
ID: 18867328
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 14

Expert Comment

by:RichieHindle
ID: 18868921
Fair enough, but using plain old CGI will be many times slower than using mod_python.
0
 

Author Comment

by:Randall-B
ID: 18870772
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
 
LVL 14

Expert Comment

by:RichieHindle
ID: 18871806
Please post the exact "dreaded 500 error" you got (it's in your Apache error log).
0
 

Author Comment

by:Randall-B
ID: 18871831
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
 

Author Comment

by:Randall-B
ID: 18873878
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
 
LVL 14

Accepted Solution

by:
RichieHindle earned 250 total points
ID: 18875448
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
 

Author Comment

by:Randall-B
ID: 18875742
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
 
LVL 14

Expert Comment

by:RichieHindle
ID: 18876008
Happy to help!
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question