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)