summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorKjetil Jacobsen <kjetilja@gmail.com>2005-02-10 11:26:23 +0000
committerKjetil Jacobsen <kjetilja@gmail.com>2005-02-10 11:26:23 +0000
commit1c407d4086da401db09c0ee10a1701136475b630 (patch)
treecd51de7162b9d4a048a9a5212ad73a69cca21931 /examples
parent3fd852ba5895a402ada7e5592a2d7f16f398946e (diff)
downloadpycurl-1c407d4086da401db09c0ee10a1701136475b630.tar.gz
simplified the code a tad
Diffstat (limited to 'examples')
-rw-r--r--examples/file_upload.py45
1 files changed, 20 insertions, 25 deletions
diff --git a/examples/file_upload.py b/examples/file_upload.py
index 5907c7b..1ea9c9f 100644
--- a/examples/file_upload.py
+++ b/examples/file_upload.py
@@ -16,28 +16,6 @@ class filereader:
def read_callback(self, size):
return self.f.read(size)
-
-# Use filereader class to hold the file reference and callback
-def version_1(filename, url):
- reader = filereader(open(filename, 'rb'))
- filesize = os.path.getsize(filename)
- c = pycurl.Curl()
- c.setopt(pycurl.URL, url)
- c.setopt(pycurl.READFUNCTION, reader.read_callback)
- c.setopt(pycurl.INFILESIZE_LARGE, filesize) # to handle > 2GB file sizes
- c.setopt(pycurl.UPLOAD, 1)
- return c
-
-# Use the builtin file read method as the callback
-def version_2(filename, url):
- filesize = os.path.getsize(filename)
- c = pycurl.Curl()
- c.setopt(pycurl.URL, url)
- c.setopt(pycurl.READFUNCTION, open(filename, 'rb').read)
- c.setopt(pycurl.INFILESIZE_LARGE, filesize) # to handle > 2GB file sizes
- c.setopt(pycurl.UPLOAD, 1)
- return c
-
# Check commandline arguments
if len(sys.argv) < 3:
print "Usage: %s <url> <file to upload>" % sys.argv[0]
@@ -50,9 +28,26 @@ if not os.path.exists(filename):
print "Error: the file '%s' does not exist" % filename
raise SystemExit
-# They both do the same, version 2 is fine if you don't need to process the
-# data read from the callback before returning
-c = version_1(filename, url)
+# Initialize pycurl
+c = pycurl.Curl()
+c.setopt(pycurl.URL, url)
+c.setopt(pycurl.UPLOAD, 1)
+
+# Two versions with the same semantics here, but the filereader version
+# is useful when you have to process the data which is read before returning
+if 1:
+ c.setopt(pycurl.READFUNCTION, filereader(open(filename, 'rb')).read_callback)
+else:
+ c.setopt(pycurl.READFUNCTION, open(filename, 'rb').read)
+
+# Set size of file to be uploaded, use LARGE option if file size is
+# greater than 2GB
+filesize = os.path.getsize(filename)
+if filesize > 2**31:
+ c.setopt(pycurl.INFILESIZE_LARGE, filesize)
+else:
+ c.setopt(pycurl.INFILESIZE, filesize)
+
# Start transfer
print 'Uploading file %s to url %s' % (filename, url)
c.perform()