title: Python CGI script only printing partial data
body: <p>I have a simple web form that sends the contents of a <code>div</code> (an SVG document) to the following Python script. The script takes the SVG data and prints it to the browser. The client then saves it as a file:</p>
  
  <pre><code>#!/usr/bin/python
  
  import cgi, cgitb, urllib
  
  form = cgi.FieldStorage()
  try:
      data = urllib.unquote(form.getvalue('output-data'))
      fn = urllib.unquote(form.getvalue('output-fn'))
  except AttributeError:
      data = None
      fn = None
  
  print "Content-Type: application/x-gzip"
  print "Content-Disposition: attachment; filename=" + str(fn) 
  print "Content-Description: File to download\n"
  print data;
  </code></pre>
  
  <p>Here is the form:</p>
  
  <pre><code>&lt;form id="form-figure-export-svg" action="assets/src/exportSvg.py" method="POST" style="display:none;" enctype="multipart/form-data"&gt;
      &lt;input type="text" id="form-figure-svg-data" name="output-data" style=""&gt;
      &lt;input type="text" id="form-figure-svg-fn" name="output-fn" style=""&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>The problem is that if the form contains a lot of data (around about 450-500 KB — actually 524288 bytes; see update below) then the data that the Python script prints is truncated. This corrupts the SVG output. Chrome and Safari browsers truncate at different points — the latest version of Firefox appears to work okay.</p>
  
  <p>I verified that the data in the form is correct (e.g., <code>console.log($('#form-figure-svg-data').val())</code>) before it is submitted to the Python script.</p>
  
  <p>Is there some parameter I need to add to the Python script, to ensure that the complete contents of <code>data</code> are printed?</p>
  
  <p>Other things I have tried:</p>
  
  <ul>
  <li>Replacing the <code>Content-Type</code> directive with the MIME type <code>image/svg+xml</code></li>
  <li>Adding the <code>Content-Transfer-Encoding</code> directive setting of <code>binary</code></li>
  <li>Using the Python <code>StringIO</code> and <code>gzip</code> libraries to manually compress the SVG data before printing</li>
  </ul>
  
  <p><strong>Update</strong></p>
  
  <p>On inspection of output from Webkit browsers (Chrome and Safari) this might be due to a limit on the maximum length of an <code>input</code> field (524288 bytes). I'm investigating to see if there is a way to remove this limit, or use some other means for sending my data through the form without an artificial limit.</p>