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

Dustin Wyatt picture Dustin Wyatt · Jan 15, 2009 · Viewed 37.7k times · Source

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.

Answer

JaseAnderson picture JaseAnderson · Jan 16, 2009

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.