Running a python script on my hosting
Asked Answered
B

3

6

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!
Barony answered 26/6, 2014 at 10:32 Comment(0)
A
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.

Anatolian answered 26/6, 2014 at 12:3 Comment(2)
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.Barony
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.Anatolian
B
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

Bywaters answered 23/1, 2015 at 5:37 Comment(0)
A
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.

Anatolian answered 26/6, 2014 at 12:3 Comment(2)
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.Barony
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.Anatolian
N
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

Nonpartisan answered 26/6, 2014 at 11:16 Comment(3)
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.Barony
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.Anatolian
Instructions are here for FastCGI which is very handy for one-off scripts. help.dreamhost.com/hc/en-us/articles/… If you need to use packages, then you'll also have to compile your own version of python which is also pretty straightforward.help.dreamhost.com/hc/en-us/articles/…Csch

© 2022 - 2024 — McMap. All rights reserved.