How do I create a webpage with buttons that invoke various Python scripts on the system serving the webpage?
I'm a hobbyist (and fairly new) programmer who has written several useful (to me) scripts in python to handle various system automation tasks that involve copying, renaming, and downloading files amongst other sundry activities.
I'd like to create a web page served from one of my systems that would merely present a few buttons which would allow me to initiate these scripts remotely.
The problem is that I don't know where to start investigating how to do this. Let's say I have a script called:
What do I need to do to have a webpage execute that script? This isn't meant for public consumption, so anything lightweight would be great. For bonus points, what do I need to look into to provide the web user with the output from such scripts?
edit: The first answer made me realize I forgot to include that this is a Win2k3 system.
Asked by: Cadie619 | Posted: 30-11-2021
This page on the python site has a good description and example of what you need to do to run a python CGI script. Start out with the simplest case first. Just make a short script that prints html.
#!/usr/bin/python #on windows change to your path to the python exe print "Content-Type: text/html" # HTML is following print # blank line, end of headers print "<TITLE>CGI script output</TITLE>" print "<H1>This is my first CGI script</H1>" print "Hello, world!"
When you try this the first time, the hardest part is usually figuring out where to put the script and how to make the web server recognize and run it. If you are using an apache web sever, take a look at these configuration steps.
Once you have this simple script working, you will just need to add an html form and button tag and use the action property to point it to scripts you want to run.Answered by: Haris675 | Posted: 01-01-2022
This simple approach requires nothing except Python standard library. Create this directory structure:
. |-- cgi-bin | `-- script.py |-- index.html `-- server.py
You put your scripts in "cgi-bin" directory, "index.html" contains links to scripts in "cgi-bin" directory (so you don't have to type them manually, although you could), and "server.py" contains this:
import CGIHTTPServer CGIHTTPServer.test()
To run your server, just run "server.py". That's it, no Apache, no other dependencies.
HTH...Answered by: Catherine103 | Posted: 01-01-2022
All you need is to have it in a directory that is set to execute CGI scripts, then you need to add
to the top, and it should execute the script as a normal python script if you link to it. (like you would a HTML or PHP file)
This, of course, assumes your webserver is set up to execute CGI at all, and that you have a linux system.
There are other, alternative solutions if you want something a bit more fancy, like mod_python and web frameworks like Pylons, but I think the CGI solution would suit your use case better.
On Windows if you have Apache, you SHOULD be able to use the same method, but replacing the stuff behind the #! with the path to your python.exe, like so:
The -u is apparently needed to put Python into unbuffered mode according to this link, which also has more indepth info: http://www.imladris.com/Scripts/PythonForWindows.htmlAnswered by: Rafael251 | Posted: 01-01-2022
You can then either import the other scripts into the service you have running on CherryPy. This would give you a little bit more control for monitoring the process and catching errors if they occur.
The other option is to simply tell the operating system to trigger the command using os.system(). Check here for an example of different approaches to using os.system().Answered by: Ted562 | Posted: 01-01-2022
A simple cgi script (or set of scripts) is all you need to get started. The other answers have covered how to do this so I won't repeat it; instead, I will stress that using plain text will get you a long way. Just output the header (
print("Content-type: text/plain\n") plus print adds its own newline to give you the needed blank line) and then run your normal program.
This way, any normal output from your script gets sent to the browser and you don't have to worry about HTML, escaping, frameworks, anything. "Do the simplest thing that could possibly work."
This is especially appropriate for non-interactive private administrative tasks like you describe, and lets you use identical programs from a shell with a minimum of fuss. Your driver, the page with the buttons, can be a static HTML file with single-button forms. Or even a list of links.
To advance from there, look at the logging module (for example, sending INFO messages to the browser but not the command line, or easily categorizing messages by using different loggers, by configuring your handlers), and then start to consider template engines and frameworks.
Don't output your own HTML and skip to using one of the many existing libraries—it'll save a ton of headache even spending a bit of extra time to learn the library. Or at the very least encapsulate your output by effectively writing your own mini-engine.Answered by: Ted426 | Posted: 01-01-2022
If you know your way around Apache this should get your started.Answered by: First Name426 | Posted: 01-01-2022
In an earlier question I asked for a minimal implementation for connecting an AJAX website to Python. This might accomplish what you are looking for. It has a button that triggers some Python code and then shows the result on the page. My solution uses the built-in webserver in Python, so it is really lightweight (no additional packages or even Apache).Answered by: John839 | Posted: 01-01-2022
When setting this up, please be careful to restrict access to the scripts that take some action on your web server. It is not sufficient to place them in a directory where you just don't publish the URL, because sooner or later somebody will find them.
At the very least, put these scripts in a location that is password protected. You don't want just anybody out there on the internet being able to run your scripts.Answered by: Patrick967 | Posted: 01-01-2022
If you're now using Python 3, print is a function requiring parenthesis.
Answered by: Patrick844 | Posted: 01-01-2022
#!/Python34/python print ("Content-type: text/html\n") print ("<html><head>") print ("</head><body>") print ("Hello World from Python Script.") print ("</body></html>")
windows - Create an icon in memory with win32 in python
What's a good way to generate an icon in-memory in python? Right now I'm forced to use pygame to draw the icon, then I save it to disk as an .ico file, and then I load it from disk as an ICO resource... Something like this: if os.path.isfile(self.icon): icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE hicon = win32gui.LoadImage(hinst, ...
windows xp - Process size in XP from Python
I have a python script that can approach the 2 GB process limit under Windows XP. On a machine with 2 GB physical memory, that can pretty much lock up the machine, even if the Python script is running at below normal priority. Is there a way in Python to find out my own process size? Thanks, Gerry
How to find out if there is data to be read from stdin on Windows in Python?
This code select.select([sys.stdin], , , 1.0) does exactly what I want on Linux, but not in Windows. I've used kbhit() in msvcrt before to see if data is available on stdin for reading, but in this case it always returns 0. Additionally msvcrt.getch() returns '\xff' whereas sys.stdin.read(1) returns ...
How can I read system information in Python on Windows?
Following from this OS-agnostic question, specifically this response, similar to data available from the likes of /proc/meminfo on Linux, how can I read system information from Windows using Python (including, but...
In the windows python console, how to make Tab = four spaces?
Hello I would like that when I am in the python console tabbing will give me four spaces. Any ideas?
windows xp - Cygwin and Python 2.6
New to python (and programming). What exactly do I need from Cygwin? I'm running python 2.6 on winxp. Can I safely download the complete Cygwin? It just seems like a huge bundle of stuff. Well, I keep running into modules and functionality (i.e. piping output) which suggest downloading various cygwin components. Will cygwin change or modify any other os functionality or have any other side effects?
windows - Using DPAPI with Python?
Is there a way to use the DPAPI (Data Protection Application Programming Interface) on Windows XP with Python? I would prefer to use an existing module if there is one that can do it. Unfortunately I haven't been able to find a way with Google or Stack Overflow. EDIT: I've taken the example code pointed to by "dF" and tweaked it into a standalone library which can be simply used at a high ...
Python script - SCP on windows
How is it possible to do secure copy using python (windows native install - ActivePython). Unfortunately pexpect module is for unix only and we don't want cygwin locally. I wrote a script that based on pscp.exe win tool - but always stops at first execution becuse of fingerprint host id. and haven't found option to switch this off. the remote hosts are running ssh-server on cygwin (win 2003 servers). Thanks
windows - Example of how to use msilib to create a .msi file from a python module
Can anyone give me an example of how to use python's msilib standard library module to create a msi file from a custom python module? For example, let's say I have a custom module called cool.py with the following code class Cool(object): def print_cool(self): print "cool" and I wa...