I am rookie in python - have experience in PHP
my service provider (Bluehost) say that python 2.6 is available, however I am not able to run any script (basic hello word) on it - probably because I try do it PHP way. create a script.php file and place the link to it in browser... :)
Where I can find explanation for dummy like me what I should do with file script.py (hello world inside) in order to be executed and displayed in browser, I am guessing that compilation could be required
thank you
Bensiu
If you're using Apache, this simple tutorial will show you how to configure it and run a python "hello world" script (in the simplest, old-fashioned way: as a CGI script).
This system-administration/configuration part will of course be different with other web servers, or other and better way of running Python web app (WSGI in particular), but then, you're not telling us what server or approach (CGI vs WSGI vs others yet) you want to use, so, this is probably the best we can suggest!-)
Related
My question is more theoretical than anything.
What I have:
Linux machine
A python script that runs an entire suite of tests.
The same script prints out all of the information to the terminal.
Currently, the way we run our software suite is via SSH to the machine, running python3 MyScript.py and then checking the output.
My goal is to create an interface that would work on a Linux/Windows machine and tablet, and would be able to execute the script, as well as visually display progress of it.
I figured, the best approach (also possibly the easiest?) is to use a web server. Web Interface is fairly universal regardless of platform it is being accessed from. And the backend is as simple as apt get install apache2 and voila! Done!
However, I have never had experience setting up cgi-bin, etc. My question is: is it actually feasible? Am I able to have an HTML button that when clicked will execute MyScript.py server-side and perhaps even parse through whatever the script is outputting to the terminal. That way I would know exactly what the script is doing, and its progress.
Any help regarding this approach (or technologies I can use) is greatly appreciated!
Is there a conventional way to run python scripts from a Ruby on Rails webserver?
I have a Ruby on Rails site that is currently deployed to Heroku (I can deploy to AWS or wherever this is possible).
I would like for people to upload, and execute arbitrary python scripts on certain page requests (that we review for security). The python scripts should be relatively short running (few seconds tops), mostly to parse or process data. Ideally we would let users upload a script in any language, but python is the most important for now.
There are several ways to do this. The most obvious one would be to write another web service, this one in Python, but that's hardly elegant. Another solution:
Install rubypython gem.
Then you can do things like:
require 'rubypython'
RubyPython.start
my_python_module = RubyPython.import('my.python.module')
value = my_python_module.my_python_function().rubify
RubyPython.stop
You can see more at rubypython documentation pages.
A third option would be to simply shell out and execute Python as any executable, through IO.popen.
I have tried searching online like crazy with no avail. PHP is as simple as naming the file .php and writing PHP. I know people say it's that simple for Python, but I have found no useful guides in setting it up. I merely want to practice Python on my computer via WAMP or another alternative. I am on Windows Vista.
I cannot get .py files to execute correctly. The actual text:
print("Hello!")
Appears just as that rather than "Hello!". I don't know what to do to make it actually work in my browser.
Any help or pointing towards guides would be greatly appreciated.
PHP does not execute in the browser. It is executed on the server side then the output is sent by the web server to the browser.
If you want a simple way to use Python to process web requests take a look at web.py (http://webpy.org).
Your server should handle Python code. Take a look at framework Django. And as for servers I can suggest you http://webfaction.com
In essence I want to run existing python scripts through a web browser, display the text and plots(if any) and also keeping the ability to run them through the command line.
I am wondering is there any toolkit that can help me with the development.Also it would be nice if the toolkit does or allows JavaScript based interactive plots.
Thanks!
-Abhi
WSGI is designed for just this purpose - it provides an interface for a web server to initiate python scripts.
You probably don't want to work with WSGI in the raw. Flask is a straightforward, simple framework you might use for this.
The details of how to actually build a WSGI web server are well beyond the scope of a stackoverflow answer - you can find plenty of tutorial docs on Flask's website.
For very simple, internal web-apps using ASP I was able to just switch IIS 'on' and then write some ASP scripts in the www directory that would start working immediately.
Is there an equivalent webserver app for Python scripts that I can run that will automatically start serving dynamic pages (python scripts) in a certain folder (with virtually no configuration)?
Solutions I've already found are either too limited (e.g. SimpleHTTPRequestHandler doesn't serve dynamic content) or require configuring the script that does the serving.
There's always CGI. Add a script mapping of .py to "C:\Python27\python.exe" -u "%s" then drop .py files in a folder and IIS will execute them.
I'd not generally recommend it for real work—in the longer term you would definitely want to write apps to WSGI, and then deploy them through any number of interfaces including CGI—but it can be handy for quick prototyping.
For development or just to play around, here's an example using the standard Python library that I have used to help friend who wanted to get a basic CGI server up and running. It will serve python scripts from cgi-bin and files from the root folder. I'm not near a Windows computer at the moment to make sure that this still works. This also assumes Python2.x. Python 3.x has this, it's just not named the same.
Make a directory on your harddrive with a cgi-bin folder in it (Ex. "C:\server\cgi-bin")
In a command window, navigate to "C:\server" directory
Type the following assuming you've installed python 2.7 in C:\Python27:
"c:\python27\python.exe -m CGIHTTPServer"
You should get a message like "Serving HTTP on 0.0.0.0 port 8000"
Linux is the same - "python -m CGIHTTPServer" in a directory with a cgi-bin/ in it.
WSGI setups are fairly easy to get started, but in no anyway turn key. django MVC has a simple built in development server if you plan on using a more comprehensive framework.
My limited experience with Python web frameworks has taught me that most go to one extreme or the other: Django on one end is a full-stack MVC framework, that will do pretty much everything for you. On the other end, there are Flask, web.py, CherryPy, etc., which do much less, but stay out of your way.
CherryPy, for example, not only comes with no ORM, and doesn't require MVC, but it doesn't even have a templating engine. So unless you use it with something like Cheetah, you can't write what would look like .asp at all.