How to create a web interface for my Python script? [closed]
Andrew Henderson
I have a computer in my university office, with Ubuntu on it, ssh running, and Python. I'm connecting on it myself using ssh to run Python programs and see the output. Only command-line output, nothing fancy.
However, I have this colleague I would like to allow to do the same. Unfortunately, he is not great with IT, so I wouldn't want to go through the whole process of familiarizing him with ssh. I'm looking for a simple alternative way for him to run the program and see the (command-line) output.
Running the program in this context means being confined to a specific folder myFolder, and do python file1.py within that folder.
Does Ubuntu come with apache or another web server installed? Is there another means of communication I could set up for him? He is using Windows 7.
84 Answers
Of course you can install apache and try your luck from there.
To me, the following seems simpler:
- (optional) create a user account for him on the university machine. This is to "separate" him from your files. This is not strictly necessary but may be a good idea.
- Install putty on your friend's Windows 7 machine.
- Create a putty session (as described here) and put that as an icon on the desktop (or any other convenient place). Make sure you also configure user name and password. (A fancy alternative would be to use pageant for authentication)
This way, all your friend has to do is click on an icon and he will be presented with a console window where he can directly use the script: python file1.py
You could set up an Ipython notebook server.
Here are the instructions.
The notebooks should be relatively easy to use (for some applications even better than the terminal interface). Plus you don't have to install a SSH client on your friend's computer, or invent your own secure protocol.
1If you're already coding in Python, you could write a web front-end for your program using the http.server module in Python 3, or BaseHTTPServer in Python 2. Your program would then emit its output directly over HTTP to the remote browser. The details of how to do so are outside the scope of this site, but depending on your program it could be less than 10 lines of code.
Bear in mind that by doing this, you would lose all the authentication and authorization capability of SSH. In other words, this method doesn't allow the server to ensure that you or your colleague is the one accessing the page. If anyone else discovers the right URL, they can run your program, potentially many times in quick succession; they could crash it; they could attempt to hack it; etc. But if these risks are acceptable to you, it's probably hard to beat pure Python for simplicity.
It is possible to do authentication and authorization over HTTPS, of course, but then it's no longer so simple.
2Ubuntu has a package customized and pre-configured for Ubuntu that includes apache and mysql that you can install with sudo apt-get install lamp-server^. If you install that you will need to code software to do what you want to do; like a webservice where he enters a link and then sees the python output inside his browser window. I consider it overkill; unless you expect to do more with it.
But would it not be easier to create a script on the Windows machine that does the ssh, the execution on your server and download the output so he can see it? Seem trivial to me (well I do this all the time but do not use Windows to do this ;) ).
1