Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a webpage with buttons that invoke various Python scripts on the system serving the webpage?

Tags:

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:

file_arranger.py

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.

like image 458
Dustin Wyatt Avatar asked Jan 15 '09 22:01

Dustin Wyatt


People also ask

Can I use Python to click button on webpage?

We can find the button on the web page by using methods like find_element_by_class_name(), find_element_by_name(), find_element_by_id() etc, then after finding the button/element we can click on it using click() method. This will click on the button and a popup will be shown.

How do you call a Python script in HTML?

Use the <py-script> tag and then mention the Python code inside the tag. After that you can pass the Python file directly.


2 Answers

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.

like image 127
JaseAnderson Avatar answered Nov 15 '22 12:11

JaseAnderson


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...

like image 31
klozovin Avatar answered Nov 15 '22 12:11

klozovin