def run(self): while True: task = CloudApi.uploadQueue.get() if task: self.url, self.callback = task uploadParams = self.getUploadParams() if uploadParams: try: form = MultiPartForm() for name in uploadParams["params"]: form.addField(name, uploadParams["params"][name]) filePath = urlparse(self.url).path # fp = open(filePath, 'rb') form.addFile(filePath) # form.addFile("file",os.path.basename(filePath) , fp) body = form.toBytes() req = urllib.request.Request(uploadParams["url"]) req.add_header('Content-type', form.getContentType()) req.add_header('Content-length', str(len(body))) req.add_header('Accept', 'application/json') req.add_data(body) response = urllib.request.urlopen(req) if self.callback: self.callback(json.loads(response.read().decode('utf-8'))) except urllib.error.HTTPError as e: logging.error("HTTP Error Code:" + str(e.code)) except urllib.error.URLError as e: logging.error(e.reason) except IOError as e: logging.error("IOError when opening file to upload: "+filePath)
def run(self): while True: task = CloudApi.uploadQueue.get() if task: self.url, self.callback = task uploadParams = self.getUploadParams() if uploadParams: try: form = MultiPartForm() for name in uploadParams["params"]: form.addField(name.encode('utf8'), uploadParams["params"][name].encode('utf8')) filePath = urlparse.urlparse(self.url).path fp = open(filePath, 'rb') form.addFile("file",os.path.basename(filePath) , fp) body = str(form) req = urllib2.Request(uploadParams["url"]) req.add_header('Content-type', form.getContentType()) req.add_header('Content-length', str(len(body))) req.add_header('Accept', 'application/json') req.add_data(body) response = urllib2.urlopen(req) if self.callback: self.callback(json.load(response)) except urllib2.HTTPError, e: print "HTTP Error Code:" + str(e.code) except urllib2.URLError, e: print e.reason except IOError, e: print "IOError when opening file to upload: "+filePath
def run(self): while True: task = CloudApi.uploadQueue.get() if task: self.url, self.callback = task uploadParams = self.getUploadParams() if uploadParams: try: filePath = urlparse.urlparse(self.url).path filename = os.path.basename(filePath) uploadParams['params']['key'] = uploadParams['params']['key'].replace('${filename}', filename) form = MultiPartForm() for name in uploadParams["params"]: form.addField(name.encode('ascii'), uploadParams["params"][name].encode('ascii')) fp = open(filePath, 'rb') form.addFile("file", os.path.basename(filePath), fp) body = str(form) req = urllib2.Request(uploadParams["url"].encode('ascii')) req.add_header('Content-type', form.getContentType()) req.add_header('Content-length', str(len(body))) req.add_header('Accept', 'application/json') req.add_data(body) response = urllib2.urlopen(req) if self.callback: self.callback(json.load(response)) except urllib2.HTTPError, e: print "HTTP Error Code:" + str(e.code) except urllib2.URLError, e: print e.reason except IOError, e: print "IOError when opening file to upload: "+filePath