2 # -*- coding: iso-8859-1 -*-
4 # $Id: file_upload.py,v 1.5 2005/02/13 08:53:13 mfx Exp $
9 # Class which holds a file reference and the read callback
11 def __init__(self, fp):
13 def read_callback(self, size):
14 return self.fp.read(size)
16 # Check commandline arguments
18 print "Usage: %s <url> <file to upload>" % sys.argv[0]
21 filename = sys.argv[2]
23 if not os.path.exists(filename):
24 print "Error: the file '%s' does not exist" % filename
29 c.setopt(pycurl.URL, url)
30 c.setopt(pycurl.UPLOAD, 1)
32 # Two versions with the same semantics here, but the filereader version
33 # is useful when you have to process the data which is read before returning
35 c.setopt(pycurl.READFUNCTION, FileReader(open(filename, 'rb')).read_callback)
37 c.setopt(pycurl.READFUNCTION, open(filename, 'rb').read)
39 # Set size of file to be uploaded.
40 filesize = os.path.getsize(filename)
41 c.setopt(pycurl.INFILESIZE, filesize)
44 print 'Uploading file %s to url %s' % (filename, url)