*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
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
#!/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
but how to know the name of the uploaded file ?
And thanks again for nice example, web.py is awesome!
-Curt