Last active
October 7, 2024 16:10
Revisions
-
nbari renamed this gist
Nov 6, 2013 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
nbari renamed this gist
Nov 6, 2013 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ #!/usr/bin/env python import os import requests import uuid -
nbari created this gist
Nov 6, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ import os import requests import uuid from random import randint from uuid import uuid4 def read_in_chunks(file_object, chunk_size=65536): while True: data = file_object.read(chunk_size) if not data: break yield data def main(file, url): content_name = str(file) content_path = os.path.abspath(file) content_size = os.stat(content_path).st_size print content_name, content_path, content_size f = open(content_path) index = 0 offset = 0 headers = {} for chunk in read_in_chunks(f): offset = index + len(chunk) headers['Content-Type'] = 'application/octet-stream' headers['Content-length'] = content_size headers['Content-Range'] = 'bytes %s-%s/%s' % (index, offset, content_size) index = offset try: r = requests.put(url, data=chunk, headers=headers) print "r: %s, Content-Range: %s" % (r, headers['Content-Range']) except Exception, e: print e if __name__ == '__main__': url = 'http://localhost:8080/test_upload/%s' % str(uuid4()) main('images/test_image_%d.jpg' % randint(1,3), url)