Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Python Question

Can anyone assist me in getting a Python script running on Hostgator Shared hosting? I work with PHP mostly, but have taken a liking to Python, and would like to try to get it going on the web. The only way I've ever ran Python is with either the interpreter, or through a terminal, with >Python file.py. I tried just uploading a hello world file to the webserver, but all it outputs is the script source. I talked with hostgator, but all they could tell me was I need to use a dispatcher, which I cannot seem to find an example of. All I want to know, is how to make a <p>Hello</p> output to the browser.

Thanks, and sorry if I'm rambley, I've been Googling this off and on all week now.

like image 226
Josh Avatar asked Dec 30 '10 20:12

Josh


2 Answers

Well, I got this from Hostgator's own support site.

Assuming your host is running Python 2.x, then you can adapt the linked-to script as follows:

#!/usr/bin/python
print "Content-type: text/html\r\n\r\n"
print "<html><head>"
print "<title>CGI Test</title>"
print "</head><body>"
print "<p>Test page using Python</p>"
print "</body></html>"

and put it in your cgi-bin folder with permissions of 755.

Update: In terms of "getting around" the cgi-bin folder: that'll depend on options your hosting package allows. Look at Bottle for a simple dispatcher which fits in a single Python module. You can deploy it using CGI,

import bottle
# Put your bottle code here, following the docs on the Bottle site
bottle.run(server=bottle.CGIServer)

Further update: Apart from the Bottle docs, I suggest you read the Python docs about CGI.

like image 200
Vinay Sajip Avatar answered Nov 06 '22 12:11

Vinay Sajip


Briefly you need modify your .htaccess file to allow Apache to interpret python file as cgi script, then write your python script with "#!/usr/bin/python" and don't forget to output http header (otherwise you will get a 500 internal error)

You can check this post to get the complete instruction

like image 40
Mark Ma Avatar answered Nov 06 '22 12:11

Mark Ma