[mod_python] upload file?

John Mudd JohnMudd at mindspring.com
Sat May 8 18:04:42 EST 2004


Yep, this works great.  Just what I was looking for!  But, just as soon
as I got it working, I started changing it.  

I noticed that if I don't specify an "action=" term for the Form that
the response comes right back to the index() function.  I actually like
that for two reason.  First it lets me use one set of exception handling
code for my entire application.  I don't have to wrap multiple
functions.  Second it means the code to return the next web page only
has to be called from one place whether a form is being processed or
not.  Anyway, I'm pretty happy with the result.  I especially like how
my code has been simplified while adding the ability to upload files.  

The only thing I lost was the ability to use cgitb.  Now it reports the
location where I call the cgitb method.  Oh, well.

I'm not sure if that will make sense.  Here's what my sample app looks
like now for what it's worth.

John


P.S.  If life was fair I'd be sending you a pile of cash now.  You
solved my problem while other people were telling me to visit the local
library. Well, maybe just this once, life can be almost fair.  Where can
I send the money?  PayPal?





httpd.conf
------------------------------------------------------------
<Directory /home/mudd/pkgs/Apache2/htdocs/javaSignAuto>
    SetHandler mod_python
    PythonHandler mod_python.publisher
    PythonDebug On
</Directory>
-------------------------------------------------------------



index.py
----------------------------------------------------------
import sys, os, time, string
from mod_python   import util
import HTMLgen as H

def dbg(msg):
  log = open('/tmp/jsa.log', 'a')
  log.write(msg + '\n')
  log.close()

def index(req):
  try:
    dbg('-'*80)

    if req.args:
      dbg('util.parse_qsl(req.args)=%s' % `util.parse_qsl(req.args)`)

    # Did the user just respond to a form?
    if req.form.has_key('upload'):
      value = req.form['inputJar']
      dbg('value.name=%s' % `value.name`)
      dbg('value.filename=%s' % `value.filename`)
      dbg('len(value.value)=%s' % `len(value.value)`)

    # Build a new page.
    doc = H.SeriesDocument(None, title='Products')
    submit = H.Input(type='submit', name = 'upload', value='AddProduct')
    f = H.Form(submit=submit, enctype='multipart/form-data')
    i = H.Input(name='inputJar', type='file')
    f.append(i, H.BR())
    doc.append(f)

    return str(doc)
  except:
    # Rollback transactions, etc.
    pass
----------------------------------------------------------





Generated HTML (just for reference, normally I just use the HTML, not
read it)
-------------------------------------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>

<!-- This file generated using Python HTMLgen module. -->
<head>
  <meta NAME="GENERATOR" CONTENT="HTMLgen 2.2.2">
        <title>Products</title>
</head>
<body>
<img src="/image/banner.gif" height="30" width="472" alt="banner.gif" border="0"><br>
<span style="width: 60px"></span> 
<img src="../image/blank.gif" height="19" width="71" alt="blank.gif"> 
<img src="../image/blank.gif" height="19" width="71" alt="blank.gif"> 
<img src="../image/blank.gif" height="19" width="71" alt="blank.gif"> 
<h3>Products</h3>
<hr>


<form METHOD="POST" ENCTYPE="multipart/form-data">

<input TYPE="file" NAME="inputJar"><br>
<input TYPE="submit" NAME="upload" VALUE="Add Product">
</form>


<p><hr>
<span style="width: 60px"></span> 
<img src="../image/blank.gif" height="19" width="71" alt="blank.gif"> 
<img src="../image/blank.gif" height="19" width="71" alt="blank.gif"> 
<img src="../image/blank.gif" height="19" width="71" alt="blank.gif"> 
<br><img src="/image/logo.gif" height="35" width="36" alt="logo.gif" align="bottom">
<font SIZE="-1"><p>Copyright &#169 Micky Mouse<br>All Rights Reserved<br>

Comments to author: <a HREF="mailto:&#109;i&#99;k&#121;@&#100;i&#115;n&#101;y&#46;c&#111;m">micky at disney.com</a><br>
Generated: Sat May 8, 2004 <br><hr>
</font>
</body> </html>
---------------------------------------------------------------------------


On Fri, 2004-05-07 at 09:35, Volodya wrote:
> On Fri, May 07, 2004 at 08:14:02AM -0400, John Mudd wrote:
> > I'm still trying to upload a file.
> > 
> > I reduced my mod_python code to a small example and attached it.  It
> > uses HTMLgen to generate the HTML.  I've listed the HTML and the trace
> > output below.  The name of the file I'm trying to upload is
> > 'zopeIntro.txt'.  I'm using Apache/2.0.49 (Unix) mod_python/3.1.3
> > Python/2.3.
> > 
> > I'm hoping this will make whatever mistake I'm making easier to see. 
> > Any suggestions?
> > 
> 
> Here is simple example of working file uploads (using publisher
> handler).
> 
> Tested on FreeBSD 4.9 (i've also tested similar code on win32 several weeks ago):
>   Apache 2.0.48,
>   mod_python 3.1.3 (+ mentioned patch by David Fraser),
>   Python 2.3.3
> 
> 
> = 8< ======== .htaccess =========================
> SetHandler mod_python
> PythonHandler mod_python.publisher
> PythonDebug On
> = >8 ============================================
> 
> 
> 
> = 8< ======== index.py ==========================
> html = """
> <html><body>
> <form action="upload" method="POST" enctype="multipart/form-data">
>   <input type="file" name="fileupload"/>
>   <input type="submit" value="Upload!">
> </form>
> </body></html>
> """
> 
> def index(req):
>     return html
> 
> def upload(req):
>     fp = req.form['fileupload'].file
>     return type(fp)
>     
> = >8 ============================================
> 
> 
> Output:
> 
> <type 'file'>
> 
> Hope this help.
> 
> 
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python



More information about the Mod_python mailing list