pythoncgidreamhost

Running a python script on my hosting


I am a newbie to web development and Python. Since I dont have the vocabulary to ask the exact question, here is a summary of what need to do:

Can someone tell me If i need to create an html file, and if yes how to get it to point to the python script The domain folder has wordpress installed. My hosting is dreamhost shared hosting

The script is there below:

#! /usr/bin/python

print 'Content-type: text/html'
print ''
print 'Hello, World!

Solution

  • Usually you'd need to put your python script under the /home/username/bin/ folder. I'm not sure if your particular webhost actually allows you to run your Python script outside of the /bin folder (normally this is not the case), but if yes then you can substitute the /pyscripts folder.

    The URL would look something like this: www.domain.com/bin/mypythonscript.py

    Or with the pyscripts folder (if possible with your webhost): www.domain.com/pyscripts/mypythonscript.py

    You don't need to create an HTML file as the first content line that you print in your Python script is telling the user's browser to display the output of the script like an HTML file. You simply type the URL to your python script into your browser and then the server runs the script and outputs it as a text/HTML file, which your browser then reads and displays.

    Also, don't forget - you need to grant execute/read/write permission to your Python script file after you upload it to the correct folder on your webhost server or it won't run at all. Usually this is done through your upload utility like Filezilla or using a shell command like chmod.