Upload files to a TurboGears web application...

Ran into this problem on Friday at a client site and just couldn't find a Python library that did everything I needed wrt providing login to TurboGears and properly uploading a file to a controller in the TurboGears application.  Bothered me that there was no easy way to do this (seems like it should be trivial), so I just coded this little library up.  Here's a sample script that uploads a "file" to a TurboGears controller:

from restclient import *
import pprint

if __name__ == "__main__":
r = JSONClient(
'http://localhost:8081', user_name='user', password='password', login='.' ,
)
result = r(
'http://localhost:8081/upload_test',
data = ( 'test.jpg', '\000'*30 ),
mime_type = 'image/jpeg',
)
pprint.pprint( result )

with the controller looking like this:

    @identity.require(identity.in_group('required_group'))
    @expose( format='json' )
    def upload_test( self, data, mime_type):
        """A little test upload..."""
        print repr(data.file.read())
        return dict( result=True )

Which isn't a huge change from the interface of the underlying library by Michael Foord, it just parses the named arguments into separate fields/files and does the TurboGears-specific login.  As it turns out, the client needs something different, so oh well, wasted time.  Still, maybe it will be useful to someone some day.  Can't say it's been even lightly tested, so caveat abandonned software-or.

Comments

  1. Mike Fletcher

    Mike Fletcher on 01/05/2009 10:15 p.m. #

    There's now a more reasonable version that doesn't have the name conflict and uses cookielib to allow more general logins to web-based login forms...

    bzr branch http://www.vrplumber.com/bzr/postclient

Comments are closed.

Pingbacks

Pingbacks are closed.