user3667167
user3667167

Reputation: 67

Python Code is not working on Browser

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.

Upvotes: 2

Views: 1680

Answers (2)

Randeep
Randeep

Reputation: 11

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)

Upvotes: 1

FlipperPA
FlipperPA

Reputation: 14311

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

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

Upvotes: 2

Related Questions