title: Passing command line arguments via CGI in Python
body: <p>OS X 10.7.5, Python 2.7, Firefox 28.0</p>
  
  <p>I have an HTML form that captures four arguments.</p>
  
  <p>I have a Python file that takes four command line arguments.</p>
  
  <p>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:</p>
  
  <pre><code>vmsmith$ ./file.py arg1 arg2 arg3 arg4
  </code></pre>
  
  <p>I naively tried referencing the Python file in <code>&lt;form ... action="./file.py"&gt;</code>, but I got a Firefox box asking me if I wanted to open <code>./file.py</code> in Xcode.</p>
  
  <p>I suspect I have to invoke the <code>./file.py</code> somewhere in an intermediary CGI script, but I've scoured the web and cannot find any examples or explanations of how to do it.</p>
  
  <p>In context, I'm not running Apache; rather, I'm running a simple Python CGI server I found here: <a href="http://pointlessprogramming.wordpress.com/2011/02/13/python-cgi-tutorial-1/" rel="nofollow">http://pointlessprogramming.wordpress.com/2011/02/13/python-cgi-tutorial-1/</a>. 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. </p>
  
  <p>Specifically, given an HTML form with four input fields, the following CGI script works:</p>
  
  <pre><code>#!/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')
  </code></pre>
  
  <p>I can print all four passed values on a web page, but again, the idea is to invoke <code>file.py</code> with them.</p>