def run(self): '''This wraps the http request function in a threading operation.''' pool.acquire() thread_conn = ih.http(httphost) # Enables loop that attempts to post file information and breaks if successful. # while True: request_dict = thread_conn.request(self.request_type, self.request, self.content, self.headers) location = request_dict['location'] data = request_dict['data'] status = request_dict['status'] message = "%s posted to %s. Status: %s. Reply: %s" % (self.filename, location, status, data) nees_logging.append_log(neeshub_log_filename, message) # If post fails, wait a second before retrying. # if status == '400': # time.sleep(3) # Once Post is Successful, end the loop. # elif status == '200': # break pool.release()
def multipart_post(filename, nees_path_id, expnum, trialnum, rep_num, datafolder, request_path = http_file_path, threading = threading_on, verbose = False): '''This is technically an upload post. It assumes that there has already been an FTP file uploaded to the NEEShub and is simply waiting assignment. This post will be the assignment. Args: filename: name of the file you wish to upload. nees_path_id: string with the format "NEES-YEAR-PRJID#.groups" This is NEEShub specified.. EXAMPLE: NEES-2007-0353.groups expnum: Experiment Number rep_num: Repetition Number trialnum: Trial Number datafolder: Folder where you wish to upload files within a Repetition. (request_path): HTTP Request Parameter, where the post is being made on the HTTP server. (threading): When True, it will create a new Thread for every post being made. (verbose): When True''' xml_sheet = multipart_http.generate_file_xml(filename, nees_path_id, expnum, trialnum, rep_num, datafolder) content_type, body = multipart_http.encode_multipart_formdata(xml_sheet, filename, utils.hub_username, utils.hub_password) headers = { 'Host' : httphost, 'Content-Type' : content_type, 'Content-Length': str(len(body)) } authentic_request = utils.authenticate_request(request_path) # If post is in threading mode, creates a separate thread that executes the post. if threading == True: threading_request = threading_http.requestThread('POST',authentic_request, body, headers, filename) threading_request.start() # If post is in non-threading mode, this creates a thread if threading == False: response_dictionary = conn.request('POST',authentic_request, body, headers) post_location = response_dictionary['location'] post_data = response_dictionary['data'] post_status = response_dictionary['status'] message = "%s posted to %s. Status: %s. Reply: %s" % (filename, post_location, post_status, post_data) nees_logging.append_log(neeshub_log_filename, message) return post_status, post_data, post_location