2

i am new to python and anyhow i managed to install Python on my Linux shared hosting. When I am trying to execute Python code in Shell terminal its working fine but i am not able to execute this code in browser directly and it just shows python code as text.

In Shell: python public_html/index.py (Working)

But if i open same file in browser it doesnt execute code.

index.py

#!/usr/bin/env python


print("Content-Type: text/html\n")

print("Hello World")

I searched everywhere on internet but couldnt find answer, I also installed Django but same problem. please help me :(

I have not done any edit to .htaccess, if here i need any please tell me.

1 new line added in .bashrc

alias python='~/bin/python'

Also I am not sure how my shebang code must look like. Just i saw #!/usr/bin/env python as commonly used SHEBANG code and used in my script.

2 Answers2

2

You have to configure Apache to handle *.py files. Here's a good tutorial:

https://docs.python.org/2/howto/webservers.html

FlipperPA
  • 13,607
  • 4
  • 39
  • 71
  • i also saw this but as a beginner its very complicated for me to understand. so asked here to get any quick solution. but thanks for ur help – user3667167 May 23 '14 at 01:40
  • What flavor of Linux are you running? Ubuntu? If you're running Ubuntu, this is the command to install the Apache module you need: sudo apt-get install libapache2-mod-python – FlipperPA May 23 '14 at 02:28
  • @FlipperPA He's not running any os. It's about shared hosting. – TNT May 23 '14 at 10:39
  • TNT is right, Its on shared hosting and OS is CentOS – user3667167 May 23 '14 at 12:58
  • Yep, I saw it was about shared hosting, just wondering what flavor was being run. If you can, let us know the hosting provider, as that will determine whether you can run as an Apache module or with something like FastCGI. – FlipperPA May 23 '14 at 13:21
  • Arvixe Hosting Provider – user3667167 May 23 '14 at 14:36
1

Try hosting your html using CGI server which comes along with python installation

Step 1.(Save the code below in a separate file. Name it START_CGISERVER.py Save it in your working folder)

import SimpleHTTPServer
import SocketServer
import CGIHTTPServer

from CGIHTTPServer import CGIHTTPRequestHandler
from BaseHTTPServer import HTTPServer


server_address=('',8000)

httpd = HTTPServer(server_address, CGIHTTPRequestHandler)

httpd.serve_forever()

Step2 : name your html as index.html (again in your working folder) Step3 : Run START_CGISERVER.py and let that window open. This means your working folder is hosting as server. Step 4 : Go to your browser type http://127.0.0.1:8000/ Step 5: Make sure the file which your html is referring to has #!/usr/bin/env python2 as first line, this could be a .py or .cgi file(this will tell CGI interpreter to run your code)

zero298
  • 25,467
  • 10
  • 75
  • 100
Randeep
  • 11
  • 1