python script placement in apache - python

This would lead to a more nvolved question, but I could use some help with such things as search terms to use to find more info, links (someone has already done this, etc.).
First of all, lets assume a centos7/apache/mod_wsgi serverset up.
Lets also assume no django, no nginx, no bootstrap,no php, possibly not even a database.
(That should narrow things down quite a bit).
I want to use 2 or 3 python scripts (maybe more), but 3 main ones.
Now, let's say I have an html page with a button,a href that calls a python script. What choices do I have on where to put this script? Right in the apache root directory, the cgi-bin, somewhere else? And let's say this button and script takes the user into a protected directory (the same script - is that possible?). And hooks up to another python script that does some math - maybe random numbers, and leads to another python script that sends the user somewhere else - another html page? Sorry, this is pretty vague - which is why I'm asking the question - need more info. And I think this also applies to security, a lot of these questions and answers are very outdated. Where's the best place to place python scripts in apache?

Try this for a start: https://www.linux.com/blog/configuring-apache2-run-python-scripts.
Once you've got it running, take the next step (i.e. adapt your python code to do what you want it to do).
Does that help you to take a step towards your goal?

Related

Hosting Python Script with I/O in <div> on website

My goal is fairly simple, I have written a python 3 script that runs on an ubuntu server at my workplace. Currently I (and other users) have to SSH into this ubuntu box anytime we want to run the script, which with a growing number of users is getting messy - from a convenience and security standpoint. So my boss would like me to create a nice webpage-based GUI to access this script and some other similar scripts I have written.
This seems like a straight forward thing, but the difficulty is in the details - the script is used for viewing and editing records so it has a lot of back-and-forth communication with the user (using input()) as well as screen clears and code like the following:
sys.stdout.write("\033[F") #back to previous line
sys.stdout.write("\033[K") #clear line
...to keep things clean and within one terminal.
If absolutely needed of course I could modify the script to accept inputs in a different format and output in a more html-friendly method without line and screen clears, but this would be rather tedious to redo for the whole script. SO - my question is what would be the best way to achieve what I'm after with minimal modifications to my written code? Ideally the webpage would just have a container holding a terminal-like graphic that handles all the input and output of the selected script as if it was running through a full fat terminal.
Things I've already looked into include basic python web frameworks such as CGI or mod_wsgi, or a python package like flask, but - correct me if I'm wrong - it seems there isn't a nice way to handle constant inputs within scripts with these let alone the terminal-oriented clearing I do for clearing lines and screens.
I also looked into ways to just host a full terminal on the webpage, and security concerns aside, this seems like the closest solution - using something like ajaxterm (which seems rather outdated now?) or wetty. My main concern with this is it isn't any easier than just using SSH at that point. Unless I could modify the terminal to auto-login and start a specific script automagically with the push of a button on the webpage, this would just be a glorified web-rendition of putty.
Any suggestions on how you would achieve this?

Documentation for XML code

At this moment I am searching documentations or trying to make documentations for the XML-Code which builds the script boxes in Choregraphe. For example, like you could see in the next images (1 and 2): what does „Input name="Multiplicand" type="2" type_size="1" nature="1" inner="0"“mean in the basic script boxes just like “Multiply.py”? It would mean one variable as input value in type number with "onEvent" in choregraphe graphic interface according to the image3, I guess.
It's very hard to find them on the official documentation website from Aldebaran or even by Google results. In principle I could already find it out with just trying to change the values and see the changes in choregraphe, but it's not very smart and takes a while. At this point, I’m really forward to hearing from you. That would be very helpful for the next students who want to understand more about choregraphe and who want to build choregraphe script boxes by themselves in Python code level.
Yes, that "Multiplicand" you see in the xml is the input you can edit via the gui.
You can create and edit Choregraphe boxes entirely through the GUI, editing their inputs, outputs and parameters; I recommend doing that instead of touching the XML; because the exact format used in the .xar file may change in future versions. I have used Choregraphe quite a bit and have never felt the need to directly edit the .xar (except maybe when resolving git conflicts, but that was years ago and since then we've been avoiding having to ever merge a .xar file).

How can I call a python's local function from an HTML doc?

I have written a python file with several functions. Now I'm writting a web page where I want to call a function of the file that I mentioned before, by pressing a button but I don't know how.
Can someone help me, or telling me where can I look for some example where python and html are connected.
I'm going to talk only about a front end solution , well known solution for back end only exist (Django) and the others will certainly talk about it.
Beforehand, you should know that using Python as a front end web language is very strange.
The "King" of these languages is JavaScript, and if you want to go further with web development, especially with front end frameworks (AngularJS, react), you should probably learn it.
That being said, there is a way to achieve what you want.
Either with this Python framework, or with a transpiler.
First, you need to set up a server to handle HTTP requests.
Then, something that can interpret python codes is needed. mod_wsgi is a good choice for python. But for a simpler way, you can also use CGI (take a look here).
Edit:
A more complete/structured solution is using a python web framework. For instance, Django or Flask. But be prepared to spend more time on them.

How can I have my python script display in html, and accept input from a webform to check against the script

I have looked around for an answer to this question, and I haven't been able to find one that seems clear to me. Basically, I am writing a python script that asks a user a question. Based on their answer (and some if/else statements) the script will return a string (and an image if I can figure it all out). Basically I want to have a webpage that displays the question string I print in python and has a form that accepts the answers, then checks that answer against the script, and returns the result. What is the best way to do this?
You need some sort of web server from Python to support this. You can start as simple and mundane as SimpleHTTPServer, which may fit your needs perfectly, to something a bit more robust, such as Flask. There are others out there as well:
Django
CherryPy
web.py
You'll also need to be mindful of templating, which typically comes in the form of Jinja, Genshi, or Mako, which are compatible with all of these save for web.py (I think).

python script output to webpage

I wrote a python script that reads in text from a file and writes a text file of definitions. I want to somehow integrate my program with a webpage for the whole world to see.
I want to be able to retrieve input text from one text box, have the python script process it, then display the output in the other text box.
I have done quite a bit of research thus far but I am still unsure of the best way to go about doing this. I tried using google's app engine but encountered too many problems, for example the app engine runtime environment uses python 2.5.2, I wrote my program using 3.1.2. Other than that I just felt that I was beginning to waste my time trying to port my program over.
I'm starting to think that javascript is the way to go or maybe pyjamas. I was also wondering if it would be possible to just have the python program constantly running on the server and to perform a system call.
I posses very little knowledge when it comes to web development. I appreciate any advice.
You could use the cgi module and create a CGI script, if your server supports it.
It's a much bigger question, involving:
Where are you going to host the site,
How slow is the script (can it execute in a few seconds or not),
Does it need access data from files or a database,
How complex is it,
etc.
I would suggest you read about Django:
http://docs.djangoproject.com/en/1.2/
That is probably the easiest way to set up a simple web site, but is also very powerful if you want to do something more in the future (related to this project or not).
However, since your script is Python 3 only, you don't have too many options, see this question:
https://stackoverflow.com/questions/373945/what-web-development-frameworks-support-python-3
I suppose, if not too hard, it is worth thinking about converting it to Python 2.7.
If that is an option, then you might very well go down to Python 2.5 and use Google App Engine. It gives you many things for free and you really don't need to worry about many things that you would if you were to set up your server. It includes a modified (better to say, shrunk down) version of Django 1.1. When you say you are wasting your time porting from 3.x to 2.5, I guess you were not counting the time that you will waste setting all other things up.

Categories