[mod_python] Form upload

Graham Dumpleton grahamd at dscpl.com.au
Thu May 18 18:53:42 EDT 2006


jaba at findyourcore.com wrote ..
> heres my code:
> 
> #########upload.py#########
> 
> from mod_python import util
> import sys
> def handler(req):
>     sys.stdout=sys.stderr=req
>     form=util.FieldStorage(req)
>     if form.has_key("file"):print form.list[0].file.getvalue()
>     print "<form method=post><input type=file name=file></form>"
> 
> ###########################
> 
> the problem is, it only returns the filename, not the file contents.
> Whats wrong?

Am sure someone else will answer your real question, I can't remember
off the top of my head how to handle files as not something I do.
Anyway, would like to point out some other things you are doing wrong.

The main issue is that you MUST not set sys.stdout/sys.stderr to the
req object and then use "print". This will blow up in bad ways if you
are using a multithreaded MPM such as used on Win32 or "worker"
on UNIX. If you really want to use "print", leave sys.std??? alone and
use:

  if form.has_key("file"):
    print >> req, form.list[0].file.getvalue()
  print >> req, "<form method=post><input type=file name=file></form>"

Ie., use ">>" redirection to make it print to the req object.

Since you are returning HTML, also make sure you set content type.

  req.content_type = 'text/html'

before first content written. Don't assume browsers will always guess
correctly.

Also, I assume that not returning apache.OK is purely because you
didn't need it necessary to supply all the code????

Graham
  


More information about the Mod_python mailing list