0

I am using windows 7, python3 and WAMP2.2.

Struggling to make my localhost display Python files. For the purpose of installing, I want to run in my browser the following python file:

print ("Content-type:text/html\r\n\r\n")
print ("<html>")
print ("<head>")
print ("</head>")
print ("<body>")
print ("<h2> Hello world </h2>")
print ("</body>")
print ("</html>")

1) I have tried to run python -m http.server , but when I open the python file with the browser, the browser prints its content instead of shown the HTML.

2) I have installed the python support to my WAMP, exactly as described in the 1-st answer here: How to install Python with Wampserver , but I keep getting the same problem - the content of the file is displayed in the browser, instead of displaying the HTML.

How can I fix it?

Thanks

Community
  • 1
  • 1
Yura
  • 2,381
  • 8
  • 33
  • 44

2 Answers2

0

It seems to me that you don't need to run the file directly, but rather make apache run it as a cgi script. so make sure you have mod_cgi installed and active, and that you have this :

AddHandler cgi-script .py

in your apache configuration

Morty
  • 3,463
  • 4
  • 17
  • 21
  • I have added this line to my httpd.conf. Then run "http://localhost/index.py" , now I get and error in my browser: "You don't have permission to access /index.py on this server." – Yura May 28 '15 at 13:45
  • 1
    it's seems like you have not activated the cgi module yet, try following this instructions in this answer, and don't forget to restart apache afterwards http://stackoverflow.com/questions/869092/how-to-enable-mod-rewrite-for-apache-2-2 – Morty May 28 '15 at 13:49
  • Thanks. The answer is about mod_reqrite and staff... What exactly should I do (looks like Chinese to me lol ). By the way, my WAMP is on "D:\" and my python is on "CL\", does that have anything to do with my problem? – Yura May 28 '15 at 13:54
  • 1
    i don't think the problem comes from the install location, as long as your PATH variable is configured correctly it should work, are you sure you have configured the htaccess file for you application ? – Morty May 28 '15 at 14:01
  • No, I havent configured any htaccess.... what should I do pls? p.s. should I add something like "#!/usr/bin/python" to my script? – Yura May 28 '15 at 14:04
  • 1
    because you mentionned that your think the problem comes from mod_rewrite and in the htaccess you can disable mod_rewrite or define the pattern it can you use to rewrite requests to your file, – Morty May 28 '15 at 14:07
  • Oh, no. I did not say that the problem comes from mod_rewrite. I said that the link you sent me was regarding mod_rewrite and I did not know what to read... p.s. should I add something like "#!/usr/bin/python" to my script? – Yura May 28 '15 at 14:09
  • 1
    you have windows so that won't be necessary, please check if you have this line : Options FollowSymLinks +ExecCGI in your http.conf – Morty May 28 '15 at 14:11
  • No. What I have is: just under the line:' ' , I have the line: 'Options Indexes FollowSymLinks Includes ExecCGI'. But I dont have the line you wrote above – Yura May 28 '15 at 14:15
  • 1
    sorry but it seems like it should work, try going through the answer again. – Morty May 28 '15 at 15:08
0

Fount it. The problem was that the server did not parse the file correctly. one "\r" should be removed from the first line

print ("Content-type:text/html\n\r\n")
print ("<html>")
print ("<head>")
print ("</head>")
print ("<body>")
print ("<h2> Hello world </h2>")
print ("</body>")
print ("</html>")
Yura
  • 2,381
  • 8
  • 33
  • 44