Solved

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

Posted on 2007-04-06
11
966 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
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 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 if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

630 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