5

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:

  • I have a small test python cgi script, which i have uploaded to /home/username/pyscripts which is above the /home/username/domain.com

  • I need a link I can type in the URL bar, which will lead to the script being executed and the content displayed in the browser.

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!
baldrs
  • 2,132
  • 25
  • 34
kmcodes
  • 807
  • 1
  • 8
  • 20

3 Answers3

3

Heroku is a good place to host and python scripts.

Pre-req

pythonscripts.py
procfile
requirements.txt

and After add, commit and push the scripts to heroku app. Just run the following command on terminal to run the scripts.

heroku run python your_scripts.py

More if you want to run this scripts on a schedule timing. then heroku provides lots of adds-on. just search it on heroku

Muhammad Taqi
  • 5,356
  • 7
  • 36
  • 61
2

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.

Alium Britt
  • 1,246
  • 4
  • 13
  • 25
  • Dreamhost does allow you to run from any directory. The problem is that the folder is on the same level as the domain.com folder (for security reasons, they recommend the scripts folder not be a sub directory). So using the link gives me a "Page not found" error. – kmcodes Jun 27 '14 at 05:34
  • I would contact your webhost and ask them how to determine the appropriate URL to your file - they may even have a FAQ telling you how to find the URL if they recommend putting it outside the domain name folder. If you want to just check to make sure your file works and you're able to run scripts, try putting it in your /bin folder, and then try the URL I posted above again. Then you can continue coding and testing until your webhost gives you an answer. – Alium Britt Jun 27 '14 at 14:02
1

Well dream host support python. Check if they are providing shell access deployment. All you need is create .py file and run it.

Then consider to use Django or Jinja2 like framwork. Its easy for creating web application

Raja Simon
  • 10,126
  • 5
  • 43
  • 74
  • Let me try this solution. I was hoping to avoid using Django as I primarily need the python file to create an output on request for google visualisation. – kmcodes Jun 27 '14 at 05:35
  • 1
    You can just stick to "vanilla" Python for now - you don't need any frameworks for what you're asking. Once you've learned regular Python well enough and find that it doesn't meet your needs, then you should move on to the various frameworks. – Alium Britt Jun 27 '14 at 14:09
  • Instructions are here for FastCGI which is very handy for one-off scripts. https://help.dreamhost.com/hc/en-us/articles/216128557-Guidelines-for-setting-up-a-Python-file-at-DreamHost If you need to use packages, then you'll also have to compile your own version of python which is also pretty straightforward.https://help.dreamhost.com/hc/en-us/articles/115000702772-Installing-a-custom-version-of-Python-3 – iJames Jul 02 '23 at 19:18