title: How to use Python/CGI for file uploading
body: <p>I'm trying to make a file uploader page that will prompt the user for a file and will upload while displaying progress. </p>
  
  <p>At the moment I've managed to make a simple HTML page that can calls my python script. The python script will then get the file and upload in 1000 byte chunks.</p>
  
  <p>I have two main problem (mainly due to be completely new to this):</p>
  
  <p>1) I can't get the file size to calculate percentage
  2) I don't know how to communicate between the server side python and whatever is in the page to update the progress status;presumably javascript.</p>
  
  <p>Am I going about everything the wrong way? Or is there a solution to my woes?</p>
  
  <p>Here is my python code:</p>
  
  <pre><code>#!/usr/local/bin/python2.5 
  import cgi, os
  import cgitb; cgitb.enable()
  
  try:
      import msvcrt
      msvcrt.setmode (0, os.O_BINARY) 
      msvcrt.setmode (1, os.O_BINARY) 
  
  except ImportError:
      pass
  
  form = cgi.FieldStorage()
  upload = form['file']
  
  if upload.filename:
      name = os.path.basename(upload.filename)
      out = open('/home/oetzi/webapps/py/' + name, 'wb', 1000)
      message = "The file '" + name + "' was uploaded successfully"
  
      while True:
          packet = upload.file.read(1000)
          if not packet:
              break
          out.write(packet)
      out.close()
  else:
  
  message = "Derp... could you try that again please?"
  
  print """\
  Content-Type: text/html\n
  &lt;html&gt;&lt;body&gt;
  &lt;p&gt;%s&lt;/p&gt;
  &lt;/body&gt;&lt;/html&gt;
  """ % (message,)
  </code></pre>