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 Answers 2

2

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

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

Sign up to request clarification or add additional context in comments.

6 Comments

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
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 He's not running any os. It's about shared hosting.
TNT is right, Its on shared hosting and OS is CentOS
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.
|
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.