3

OS X 10.7.5, Python 2.7, Firefox 28.0

I have an HTML form that captures four arguments.

I have a Python file that takes four command line arguments.

What I want is when I hit the 'Submit' button on the form, to invoke the Python file with the four arguments just as if I had done it on the command line:

vmsmith$ ./file.py arg1 arg2 arg3 arg4

I naively tried referencing the Python file in <form ... action="./file.py">, but I got a Firefox box asking me if I wanted to open ./file.py in Xcode.

I suspect I have to invoke the ./file.py somewhere in an intermediary CGI script, but I've scoured the web and cannot find any examples or explanations of how to do it.

In context, I'm not running Apache; rather, I'm running a simple Python CGI server I found here: http://pointlessprogramming.wordpress.com/2011/02/13/python-cgi-tutorial-1/. And I've run some simple CGI scrips with the HTML form and verified that the server, the HTML file, and the CGI scripts all work.

Specifically, given an HTML form with four input fields, the following CGI script works:

#!/usr/local/bin/python

import cgi
import cgitb
cgitb.enable() 

form = cgi.FieldStorage()

val1 = form.getvalue('arg1')
val2 = form.getvalue('arg2')
val3 = form.getvalue('arg3')
val4 = form.getvalue('arg4')

I can print all four passed values on a web page, but again, the idea is to invoke file.py with them.

1 Answer 1

1

1) file.py can be turned into a CGI script, e.g. by combining your CGI script code with the part of file.py that does something with variables val1 val2 val3 and val4. Then <form ... action="./file.py"> will work, but you will not be able to run file.py with arguments on the command line in a usual way.

2) Alternatively, file.py can be left untouched and called from your CGI script like this:

import os
os.system('python file.py' + ' ' + val1 + ' ' + val2 + ' ' + val3 + ' ' + val4)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.