def add_file(self, fields, filename, content, content_type='application/octet-stream', commit=True): """ Post the content from `file_handle` to Solr Cell's ExtractingRequestHandler. The keys in the params will be encoded into the request url and the data from file_handle will posted as a multipart content. """ start_time = time.time() self.log.debug("Starting to build add request...") form = MultiPartForm() # user speicified fields like `id` should be translated into # `literal.id` first, then set it as part of the form-data. for key in fields.iterkeys(): form.add_field('literal.%s' % key, fields[key]) if commit: form.add_field('commit', 'true') form.add_file('file', filename, content, content_type) request = urllib2.Request(self.url + 'update/extract') body = str(form) request.add_header('Content-type', form.get_content_type()) request.add_header('Content-length', len(body)) request.add_data(body) try: start_time = time.time() response = urllib2.urlopen(request).read() end_time = time.time() self.log.info("Finished '%s' (%s) with body '%s' in %0.3f seconds." % (filename, 'POST', str(body)[:10], end_time - start_time)) except AttributeError: # For httplib2. error_message = "Failed to connect to server at '%s'. Are you sure '%s' is correct? Checking it in a browser might help..." % (url, self.base_url) self.log.error(error_message) raise SolrError(error_message)
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
def upload(api_key, api_secret, project_id, filename, file): timestamp, dev_hash = _authenticate(api_secret) form = MultiPartForm() form.add_field('api_key', api_key) form.add_field('timestamp', timestamp) form.add_field('dev_hash', dev_hash) form.add_field('is_keeping_all_strings', 'true') form.add_field('file_format', 'IOS_STRINGS') form.add_file('file', filename, fileHandle=file) body = str(form) request = urllib2.Request( 'https://platform.api.onesky.io/1/projects/%s/files' % (project_id)) request.add_header('content-type', form.get_content_type()) request.add_header('content-length', len(body)) request.add_data(body) try: response = urllib2.urlopen(request) #print response.read() except urllib2.HTTPError as e: raise Exception(e.read())
def send_request(tmpzip, data, url, interface="Command Line"): """ Encodes all the data into a Http request where the album will be uploaded it returns the response as a string. If there is a server error, it will return the HTML of that error. If it's a success, the server returns "#### bytes recieved from server" """ tmpzip.seek(0) mpform = MultiPartForm() mpform.add_field('artist', str(data['artist'])) mpform.add_field('album', str(data['album'])) mpform.add_field('meta', str(data['meta'] or "")) mpform.add_field('date', str(data['date'])) mpform.add_field('password', str(data['password'])) mpform.add_field('profile', str(data['profile'])) mpform.add_file('file', 'album.zip', tmpzip) body = str(mpform) request = urllib2.Request(url + "/upload") ua = USER_AGENT.format(interface=interface) request.add_header('User-agent', ua) request.add_header('Content-type', mpform.get_content_type()) request.add_header('Content-length', len(body)) request.add_data(body) try: return urllib2.urlopen(request).read() except Exception, e: return e.read()