File-input and Wsgi

254 views
Skip to first unread message

Joni Töyrylä

unread,
Sep 3, 2009, 7:05:22 AM9/3/09
to modwsgi
Im getting form-values nicely:
form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ)

However, file- html input field is returned as MiniFieldStorage object
instead of FieldStorage object. I have read cgi documentation in
http://docs.python.org/library/cgi.html but no luck.

Any idea why my file uploads are not working?

Clodoaldo Neto

unread,
Sep 3, 2009, 11:56:34 AM9/3/09
to mod...@googlegroups.com
2009/9/3 Joni Töyrylä <jonito...@gmail.com>:
Is enctype="multipart/form-data" and method="post"?

Regards, Clodoaldo



>
> >
>

Joni Töyrylä

unread,
Sep 4, 2009, 3:52:23 AM9/4/09
to modwsgi
Clodoaldo, Yes. This form has been perfectly working on mod_python.



Graham Dumpleton

unread,
Sep 4, 2009, 6:01:07 AM9/4/09
to mod...@googlegroups.com
2009/9/4 Joni Töyrylä <jonito...@gmail.com>:

>
> Clodoaldo, Yes. This form has been perfectly working on mod_python.

The mod_python module doesn't use cgi.FieldStorage but uses its own
implementation. I don't believe it is full compatible with
cgi.FieldStorage any more, and thus why you will see differences.

Graham

Joni Töyrylä

unread,
Sep 4, 2009, 6:36:20 AM9/4/09
to modwsgi
Thank you for your replies.

To let you know what you already know, i am re-writing server side
program to use mod_wsgi instead of mod_python. There are no more
mod_python in my code.
Everything works perfectly but file uploads.

Html form is basic html code, it has worked in mod_python:
<form action="?" method="post" enctype="multipart/form-data">
<input type="hidden" name="reload" value="foobar"/>
<input type="hidden" name="target" value="foobar"/>
<input type="hidden" name="save" value="upload_image"/>
<input type="file" name="imagename" size="15"/><br/>
<input type="submit" value="upload"/>
</form>

In my application i read the form values...
form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ)

... and i get this:
FieldStorage(None, None, [MiniFieldStorage('reload', 'foobar'),
MiniFieldStorage('target', 'foobar'), MiniFieldStorage('save',
'upload_image'), MiniFieldStorage('imagename',
'Firefox_wallpaper.png')])

I have read cgi documentation. I should get FieldStorage object which
have "file" and "filename" attributes not None. Confused. I am
probably doing something stupid but just cannot see it.

Clodoaldo Neto

unread,
Sep 4, 2009, 8:58:36 AM9/4/09
to mod...@googlegroups.com
2009/9/4 Joni Töyrylä <jonito...@gmail.com>:
I tried to reproduce your problem in Fedora 11, Python 2.6, mod_wsgi
2.5 without success. This is my script and apache config:

import cgi, os

html = """\
<html><body>
<form enctype="multipart/form-data" action="/" method="post">
<p>File: <input type="file" name="file"></p>
<p><input type="submit" value="Upload"></p>
</form>
<p>%s</p>
</body></html>
"""

def application(environ, start_response):

form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ)

if form.has_key('file'):
fileitem = form['file']

if fileitem.filename:
fn = os.path.basename(fileitem.filename)
open('/var/www/html/wsgi-scripts/files/' + fn,
'wb').write(fileitem.file.read())
message = 'The file "' + fn + '" was uploaded successfully'

else:
message = 'No file was uploaded \n form:%s' % repr(form)

status = '200 OK'
output = html % message

response_headers = [('Content-type', 'text/html'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)

return [output]


<VirtualHost *:80>
ServerName wsgi_test.dkt
CustomLog logs/wsgi_test.dkt-access_log combined
ErrorLog logs/wsgi_test.dkt-error_log
DocumentRoot /var/www/html/wsgi-scripts
WSGIScriptAlias / /var/www/html/wsgi-scripts/myapp.wsgi
<Directory /var/www/html/wsgi-scripts>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>


Are you using Python 3.x and/or Windows?

Clodoaldo

>
>
> >
>

Joni Töyrylä

unread,
Sep 16, 2009, 4:54:46 AM9/16/09
to modwsgi
Thank you, i had a problem in sending the form :P

gert

unread,
Sep 16, 2009, 1:25:47 PM9/16/09
to modwsgi
Note that this does not work in python 3.x

form = FieldStorage(fp=environ['wsgi.input'], environ=environ)

File "C:\Program Files (x86)\Python3.1\lib\email\feedparser.py",
line 99, in push
data, self._partial = self._partial + data, ''
TypeError: Can't convert 'bytes' object to str implicitly

Graham when are they going to fix this ?

Graham Dumpleton

unread,
Sep 16, 2009, 7:03:32 PM9/16/09
to mod...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages