7

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.

Josh
  • 12,448
  • 10
  • 74
  • 118

3 Answers3

9

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.

Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191
  • Worked great! Is there a way to get around the cgi-bin folder? Or perhaps use .htaccess to write it out of the url? – Josh Dec 30 '10 at 21:50
  • Last question, what is the simplest way to access get / post in Python, with this setup? Something akin to $_GET / $_POST in PHP? – Josh Dec 30 '10 at 22:37
1

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

Mark Ma
  • 1,342
  • 3
  • 19
  • 35
0

I don't know about Hostgator, but you want your web server to see your Python script as an executable CGI script. That means making it executable and ensuring there's a #! in it and either putting it in cgi-bin, or setting your server to recognize .py as a CGI script file extension. Here is a simple example script and some context using the cgi module.

nmichaels
  • 49,466
  • 12
  • 107
  • 135