A python server that executes python scripts - python

I'm doing a python class this semester, and I'd like to add graphical user interfaces to my programs in the form of web pages. Partly I can't be bothered to learn Tkinter, partly I'm just challenging myself, and partly I just like coding interfaces in HTML/JS.
I know the basics on creating HTTP servers with SimpleHTTPServer, but what I need is a way for my web pages to fire ajax commands to python scripts on the server, which will then be executes server side, and then receive the script output. Kind of like how Sage does things.
How can I create an extension for SimpleHTTPServer that I can then use to serve up the outputs of python scripts? I need a solution that's very general, so that given any python script I write, I can easily wrap it in some sort of interface, put it in the server's folder, and not have to do anything else but call it with AJAX, and my server will handle the rest.
My current idea is to have a ServerSideScript class that my scripts can extend, with a single function called output. Every script I have should contain a subclass of ServerSideScript named Script. When the server is asked to serve, for example, foo.py, it notices the extension and does something like:
if self.path[-3:]: == ".py":
return getScriptOutput(self.path)
...
def getScriptOutput(self, path):
from path import Script # obviously not going to work, since path is a string
return Script().output()
That can't work for the reason pointed out in the comment, I'm aware of things like the import_module function, but importing seems like an ugly way to do this to begin with.
What's recommended?

Python comes with batteries included; CGIHTTPServer will run Python scripts using the CGI standard:
The CGIHTTPServer module defines a request-handler class, interface compatible with BaseHTTPServer.BaseHTTPRequestHandler and inherits behavior from SimpleHTTPServer.SimpleHTTPRequestHandler but can also run CGI scripts.
It'll run Python scripts as long as they have a .py or .pyw extension, and are executable; other paths mapping to files are treated as regular files and have their content served instead.
You may be interested in the cgi and cgitb modules as well, to help ease CGI script development.

You might want to take a look at IPython notebooks - it essentially gives you a web-based interactive Python shell, which even integrates with matplotlib etc. You can save Python scripts as notebooks, add text inbetween statements etc.
You can also use the nbviewer if you want to share the output, but not the interactivity.

Related

How to See Python Error Messages for Scripts Running on Apache Server?

I have a server side Python script that imports a big package called nltk. It ran at a command prompt, but would not run in Apache server.
I tried the logging and put it as the first import and created a log file immediately after. But nothing is written to the file before the script crashes.
Is there a way to see the "ImportError: no module ..." when the script runs on Apache?
As far as I'm aware, there's no built-in way to tell Apache to simply dump error log messages directly to the browser. I'm betting the error_log output happens in an independent part of the Apache pipeline -- just a guess.
Most of the time when someone wants output errors directly to the browser, that person is a developer working in a web script such as PHP, Python, Perl, or other lang. In order to output error messages that occur within your lang interpretter, you have to format the output and pass it through apache as if it was normal output.
PHP
Some languages provide an easy way to do this, such as the well known error_reporting switch in PHP.
Python
Unfortunately some languages, make this painful or almost impossible and Python is one of them.
At one time this was apparently possible using https://docs.python.org/3/library/cgitb.html
However that library has been deprecated. Seems like the Python community doesn't have much love for developers who want to get immediate feedback about errors in the browser. :(

Making GUI with only python without framework?

Is it possible to create a user interface without the help of python framework (like tinker or pygame) and use only vanilla python code? If yes, how?
Can you briefly explain how python framework works?
Is the code of different python framework different?
If the computer did not have the framework installed, will the program still runnable if the program uses a framework?
Thanks very much
Yes, after all tinker and pygame are just python classes packaged as modules.
Python frameworks are a bunch of pre-tested and reusable modules that allow you to use and extend upon so you don't have to reinvent the wheel.
Yes, frameworks will have differences in usability and code.
The computer will always need the dependencies, though you can package these in various ways aka create a package that has all your dependencies for the program to run.
If you want as few external dependencies as possible (but still a GUI) I would strongly suggest using a Web-Microframework like bottle (single file) and utilize the user's browser for rendering.
You can make a GUI without any external framework with HTML by setting up a webserver and using the user's browser to render it.
For a browser-GUI without an external Framework: Depending on whether you know JavaScript you can either use XML-RPC (xmlrpc.server+http.server with JS in the browser) or WSGI (wsgiref) (example on that page)
Yes, totally.
Of course the if you do not prepare for this case you cannot run a program without an integral part of it like a Framework - but you can distribute your program with the Framework included.
For XML-RPC
import xmlrpc.server
import http.server
class MyHandler(xmlrpc.server.SimpleXMLRPCRequestHandler,http.server.SimpleHTTPRequestHandler):
pass
This handler will serve files from the current working directory (your actual HTML-UI and JS for communication (there are several XMP-RPC libraries for JS)) but it can also be used like in the XML-RPC-Server example to glue your code and the UI together.

Restrict python exec acess to one directory

I have a python script which executes a string of code with the exec function. I need a way to restrict the read/write access of the script to the current directory. How can I achieve this?
Or, is there a way to restrict the python script's environment directly through the command line so that when I run the interpreter, it does not allow writes out of the directory? Can I do that using a virtualenv? How?
So basically, my app is a web portal where people can write and execute python apps and get a response - and I've hosted this on heroku. Now there might be multiple users with multiple folders and no user should have access to other's folders or even system files and folders. The permissions should be determined by the user on the nodejs app (a web app) and not a local user. How do I achieve that?
Execute the code as a user that only owns that specific directory and has no permissions anywhere else?
However- if you do not completely trust the source of code, you should simply not be using exec under any circumstances. Remember, say you came up with a python solution... the exec code could literally undo whatever restrictions you put on it before doing its nefarious deeds. If you tell us the problem you're trying to solve, we can probably come up with a better idea.
The question boils down to: How can I safely execute the code I don't trust.
You can't.
Either you know what the code does or you don't execute it.
You can have an isolated environment for your process, for example with docker. But the use cases are far away from executing unsafe code.

Deploying matlab app on the web using python

Hi I want to deploy a matlab application on the web using python. Is there a way to do it.I have converted my application into jar files (java classes) as per the documentation on math works site. Can someone point me in the right direction to go ahead
The fact that your Matlab code is packaged up as Jars may not help that much here, at least not with pure Python.
There are a few ways you can take code written in Java and expose it to Python.
Jython
If you are willing to give Jython a shot this may be a really easy way to provide a Django interface to your jars.
Basically you'll get to write a normal Django App and also use Jython to work natively with your Jars. This could be the best of both worlds assuming you aren't tied to CPython.
Django-Jython
Java Compatibility Interfaces
On CPYTHON either of the following projects will help you work with the code in your Jar files:
JCC: Create a Python extension module that wraps your Jar file
JPype: Provides an API for running the JVM and calling into code running in that JVM from Python.
Separate Process:
If you have a standalone program written in Matlab (really any language) you could execute it as a child process of your Django application. You'd look into a simple web form in Django that allowed you to submit values to be inputs to this process and then in your view (after validating the form) you'd do something like:
command = "mymatlabprogram.exe %s"%(arg1,)
process = subprocess.Popen(command.split())
stdout, stderr = process.communicate()
Assuming that worked you could pull answers out of stdout or error messages out of stderr. You could serve an image created by that process, etc. Once something like this is working you could look into celeryd to extract the subprocess stuff from your web app.
The advantage of working with a separate process is that you isolate bugs in your Matlab code from breaking your web application and vice versus. The disadvantage is you have to serialize everything and work with multiple times between the client's browser and your web app, between the web app and the executable, and back to the client.

Executing Python program on web

Can I use os.system() or subprocess.call() to execute a Python program on a webserver?
I mean can I write these functions in a .py script and run it from a web browser and expect the program to be executed?
Thanks a lot.
EDIT:
Sorry for all the confusion, I am giving you more background to my problem.
The reason I am trying to do is this.
I have a Python program that accepts an XML file and returns me TTF file.
I run that program in terminal like this:
ttx somefile.xml
After which it does all the work and generates a ttf file.
Now when I deploy this script as a module on web server. I use a to allow user to browse and select the XML file.
Then I read the file data to temporary file and then pass the file to the module script to be executed like this:
ttx.main([temp_filename])
Is this right way to do it? Because at this point, I don't get any error in the log or in browser. I get blank screen.
When this didn't work, I was going to try os.system or subprocess.call
You do not use os.system or subprocess.call to execute something as a cgi process.
Maybe you should read the Python cgi tutorial here:
http://www.cs.virginia.edu/~lab2q/
If you want your cgi process to communicate with another process on your local machine, you might want to look at "REST frameworks" for Python.
So long as your server is configured to run CGI scripts (Apache's documentation for that is here, for example), yes, you can execute a python script from a webserver. Simply make sure the script is in the appropriate cgi-bin/ directory and that the file has executable permission on the server.
With regards to your comments:
You can, if you really want, explicitly allow other folders on the server to run executable code. I don't know what server you're using, but on Apache this is done by setting Option +ExecCGI for the folder you want. Again, see the docs I linked to.
You need to give an absolute path with respect to the server. As an example, a site I develop has the layout: /public_html/cgi-bin/ When I want to access .cgi or .py files, the url for the site is something like http://chess.narnia.homeunix.com/cgi-bin/index.cgi. You can also set up re-directs to certain files if you want.
One way to pass parameters through your browser is to append them to the URL like an HTTP POST method. Here's a good example of doing that.
Is that what you were looking for with your question, or did you want to actually invoke the python script with os.system()?
Yes, I do it all the time. Import as you would do normally, stick your .py in your cgi-bin folder and make sure the server is capable of handling python.
Another option would be to simply create an application on Google's App Engine. That gives you oodles of resources and APIs for Python execution.
http://code.google.com/appengine
I've done it quite a bit in classic ASP on IIS 5 and above. I would have the ASP engine execute python code (instead of, e.g., vbscript (hearkening back to the old days, here)). Behind those asp pages would be python modules written in straight python that could be imported and could execute pretty much arbitrary code. As others have mentioned, the effective user needs to have execute permission on the thing you're trying to execute.

Categories