Solved

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

Posted on 2007-04-06
11
946 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now