File upload

21 views
Skip to first unread message

Grigory Bakunov

unread,
Jan 26, 2006, 1:09:45 PM1/26/06
to web.py
Short question: anybody can show me simple file upload example with
web.py?

peter

unread,
Jan 27, 2006, 12:21:20 AM1/27/06
to web.py
Hi Grigory,
no web.py examples but I found a few to let you decide how you might
tackle it....

*sample code to upload data to flickr. The method you should look at is
'def encode_multipart_formdata'
-http://berserk.org/uploadr/

*this example shows how to upload to server using urllib2
-uses modified script from aspn
+http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146306
-download from here
+http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=upload_test.py

*Since your posting data should add to the post method.
-see the tute to see what I mean (class Add: def post(self):)
-http://webpy.org/tutorial

Grigory Bakunov

unread,
Jan 27, 2006, 2:00:48 AM1/27/06
to web.py
Thanks Peter!
Cookbook example absolutely awesome. Now I want to know how to catch it
on a server side :)
I can prepare request but cannot handle it on CGI.
What should I do after get web.input()?

peter

unread,
Jan 28, 2006, 1:01:49 AM1/28/06
to web.py
> What should I do after get web.input()?

Now you know more than me about this. I found a script to allow for
both uploads & handle downloads here.

#1 Basic python examples
Nice article on python based cgi tute in pyzine. I've included it
because it has a section on receiving form submissions but not
multipart mime.
-http://www.pyzine.com/Issue008/Section_Articles/article_CGIOne.htm

#2 (clean but basic)
Another aspn example.
-http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/392879

'... ScriptServer is a minimalist application server, handling both GET
and POST requests, including multipart/form-data for file uploads, HTTP
redirections, and with an in-memory session management. It can run
Python scripts and template files using the standard string
substitution format ...'

Try looking at the ScriptRequestHandler. do_GET method. This should
allow you to run the service, where according to the python docs

*http://docs.python.org/lib/node471.html
-' ... file upload draft standard entertains the possibility of
uploading multiple files from one field (using a recursive multipart/*
encoding). ...'

The script uses the 'SimpleHTTPServer' module as the base.
*http://docs.python.org/lib/module-SimpleHTTPServer.html

beware:
-----------
*this is a first try cgi app
*very basic handling
*not using threads
*no info here on securing the script
*example in python but not using web.py


#2 (not as clean but has receiving code)
But my bet is to have a look at this script
*http://www.voidspace.org.uk/python/cgi.shtml
-'Upload Scripts Sending and Receiving Files Over HTTP Two scripts to
do with uploading files to a server'

So theres 3 things to look at. Sorry I dont have any web.py examples
but I must confess I've not done this with python. Now I need to read
#1.

Have fun. Let us know if you get the receiving end going.

Regs PR

Curtis

unread,
Jan 29, 2006, 4:43:28 AM1/29/06
to web.py
Here's a quick example that shows two ways to handle file uploads with
web.py:

#!/usr/local/bin/python
import web

# Try http://localhost:8080/1 or /2 for the different methods

urls = (
'/([12]{0,1})', 'testupload',
'/upload([12]{0,1})', 'upload',
)

web.internalerror = web.debugerror

class testupload:
def GET(self,n):
try:
n = int(n)
except:
n = 1
print """
<html>
<body>
<form action="upload%s" method="post"
enctype="multipart/form-data">
filename: <input type="file" name="test_upload"
/>
<br />
<input type="submit" />
</form>
</body>
</html>
""" % n


class upload:
def POST(self,n):
try:
n = int(n)
except:
n = 1

if n == 1:
# You can access the uploaded file as a string (value)
req = web.input()
size = "%s bytes" % len(req["test_upload"])
else:
# Or you can access an uploaded file as a file type object
uploaded = web.context._inputfs["test_upload"].file
if uploaded:
# It's an uploaded file; count lines
linecount = 0
while True:
line = uploaded.readline()
if not line: break
linecount = linecount + 1
size = "%s lines" % linecount

print """
<html>
<body>
The uploaded file size was %s
</body>
</html> """ % size

if __name__ == "__main__": web.run(urls)

Cheers,

-Curt

manatlan

unread,
Jan 29, 2006, 5:31:09 AM1/29/06
to web.py
thanx ...

but how to know the name of the uploaded file ?

Grigory Bakunov

unread,
Jan 29, 2006, 11:58:02 AM1/29/06
to web.py
Good example!
Small code cleanup for it:
-----

try:
n = int(n)
except:
n = 1
------
can be replaced to
------
if n.isdigit(): n=1
else: n = int(n)
------
It's much faster and easy to read
But I (as a long time lisp hacker :) prefer to use
-----
n = n.isdigit() and n or 1
-----

And thanks again for nice example, web.py is awesome!

Curtis

unread,
Jan 29, 2006, 1:34:06 PM1/29/06
to web.py

You'd have to use method 2 and access it via the 'filename' attribute
e.g. web.context._inputfs["test_upload"].filename

-Curt

Reply all
Reply to author
Forward
0 new messages