def post(self, url, params, boundary): headers = get_headers(params, boundary) headers['Cookie'] = self.hub_cookie params = MultipartParam.from_params(params) return self.hub_pool.urlopen('POST', url, MultipartReader(params, boundary), headers=headers)
def post(self, url, params, boundary): headers = get_headers(params, boundary) headers['Cookie'] = self.hub_cookie params = MultipartParam.from_params(params) return self.hub_pool.urlopen('POST', url, MultipartReader(params, boundary), headers=headers)
def process(self, inbox): """ Inbox is expected to be a list of imap.Message objects. Although any list of mapping objects is accepted, provided that objects support methods enlisted in 'actions' option """ # Poster: Register the streaming http handlers with urllib2 register_openers() for message in inbox: res = self.map(message) if not res: continue url, options = res for action in options['actions']: getattr(message, action)() files = [] if options['send_files']: for num, attachment in enumerate(message.attachments): filename, ctype, fileobj = attachment file_param = MultipartParam('attachment[%d]' % num, filename=filename, filetype=ctype, fileobj=fileobj) files.append(file_param) data = {} for name in options['msg_params']: part = message.get(name, None) if not part: part = getattr(message, name, None) if part: #TODO: maybe we should raise an exception #if there's no part data[name] = part data.update(options['add_params']) data = MultipartParam.from_params(data) data += files datagen, headers = multipart_encode(data) request = urllib2.Request(url, datagen, headers) if options.get('auth', None): cj, urlopener = auth.authenticate(options['auth'], request, self.base_url) try: result = urllib2.urlopen(request).read() except urllib2.URLError, e: result = e #continue # TODO Log error and proceed. yield url, result
# Register the streaming http handlers with urllib2 register_openers() # Start the multipart/form-data encoding of the file "DSC0001.jpg" # "image1" is the name of the parameter, which is normally set # via the "name" parameter of the HTML <input> tag. # headers contains the necessary Content-Type and Content-Length # datagen is a generator object that yields the encoded parameters file = MultipartParam.from_file("file1", "test.tlog") file.filetype = 'application/vnd.mavlink.tlog' params = { #'tlog1.tlog': file, # the name doesn't matter if the content type is correct #'tlog1.tlog': open("mav.tlog", "rb"), 'autoCreate': 'true', 'login': '******', 'password': '******', 'email': '*****@*****.**', 'fullName': 'Bob Bumblesticks', 'api_key': 'APPID.DEVKEY' # DONT FORGET YOUR KEY } datagen, headers = poster.encode.multipart_encode(MultipartParam.from_params(params) + [file]) # Create the Request object vehicle = str(uuid.uuid1(clock_seq = 0)) request = urllib2.Request("https://api.3drobotics.com/api/v1/mission/upload/" + vehicle, datagen, headers) # Actually do the request, and get the response print urllib2.urlopen(request).read()
# Start the multipart/form-data encoding of the file "DSC0001.jpg" # "image1" is the name of the parameter, which is normally set # via the "name" parameter of the HTML <input> tag. # headers contains the necessary Content-Type and Content-Length # datagen is a generator object that yields the encoded parameters file = MultipartParam.from_file("file1", "test.tlog") file.filetype = 'application/vnd.mavlink.tlog' params = { #'tlog1.tlog': file, # the name doesn't matter if the content type is correct #'tlog1.tlog': open("mav.tlog", "rb"), 'autoCreate': 'true', 'login': '******', 'password': '******', 'email': '*****@*****.**', 'fullName': 'Bob Bumblesticks', 'api_key': 'APPID.DEVKEY' # DONT FORGET YOUR KEY } datagen, headers = poster.encode.multipart_encode( MultipartParam.from_params(params) + [file]) # Create the Request object vehicle = str(uuid.uuid1(clock_seq=0)) request = urllib2.Request( "https://api.3drobotics.com/api/v1/mission/upload/" + vehicle, datagen, headers) # Actually do the request, and get the response print urllib2.urlopen(request).read()
def post_files(self, files, start, end, uploaded_queue_put, boundary, local_deploy): hub_session = self.hub_session hub_cookie = self.hub_cookie hub_pool = self.hub_pool while start < end: if self.stopped: uploaded_queue_put(None) # Make sure the waiting thread wakes up break item = files[start] start += 1 deploy_path, relative_path, file_size, file_hash, file_md5 = item try: if local_deploy: guessed_type = guess_type(relative_path)[0] if guessed_type is None: guessed_type = "" params = {'file.content_type': guessed_type, 'file.name': relative_path, 'file.path': deploy_path, 'session': hub_session, 'hash': file_hash, 'length': str(file_size), 'md5': file_md5} if deploy_path.endswith('.gz'): params['encoding'] = 'gzip' r = hub_pool.request('POST', '/dynamic/upload/file', fields=params, headers={'Cookie': hub_cookie}, timeout=self.hub_timeout) else: params = [MultipartParam('file', filename=relative_path, filetype=guess_type(relative_path)[0], fileobj=open(deploy_path, 'rb')), ('session', hub_session), ('hash', file_hash), ('length', file_size), ('md5', file_md5)] if deploy_path.endswith('.gz'): params.append(('encoding', 'gzip')) headers = get_headers(params, boundary) headers['Cookie'] = hub_cookie params = MultipartParam.from_params(params) params = MultipartReader(params, boundary) r = hub_pool.urlopen('POST', '/dynamic/upload/file', params, headers=headers, timeout=self.hub_timeout) except IOError: self.stop('Error opening file "%s".' % deploy_path) continue except (HTTPError, SSLError, ValueError) as e: self.stop('Error uploading file "%s": "%s".' % (relative_path, e)) continue if r.headers.get('content-type', '') != 'application/json; charset=utf-8': self.stop('Hub error uploading file "%s".' % relative_path) continue answer = json_loads(r.data) # pylint: disable=E1103 if r.status != 200: if answer.get('corrupt', False): self.stop('File "%s" corrupted on transit.' % relative_path) else: msg = answer.get('msg', None) if msg: self.stop('Error when uploading file "%s".\n%s' % (relative_path, msg)) else: self.stop('Error when uploading file "%s": "%s"' % (relative_path, r.reason)) continue if not answer.get('ok', False): self.stop('Error uploading file "%s".' % relative_path) continue # pylint: enable=E1103 uploaded_queue_put((relative_path, file_size, file_hash)) answer = None r = None params = None relative_path = None deploy_path = None item = None
def get_batch_request(): batch_request_file = open("batch_request/batch_request.xml") batch_request = batch_request_file.read() batch_request = batch_request.replace("{{filename}}", "Request.csv") batch_request = batch_request.replace("{{application_batch_id}}", get_next_batch_id()) batch_request = batch_request.replace("{{message_timestamp}}", datetime.datetime.now().isoformat() + "Z") username = '******' password = '******' url = "https://maxcvservices.dnb.com:8443/V2.0/Batches" token = get_auth_token(username, password) # Register the streaming http handlers with urllib2 register_openers() # Start the multipart/form-data encoding of the file "DSC0001.jpg" # "image1" is the name of the parameter, which is normally set # via the "name" parameter of the HTML <input> tag. request_param = MultipartParam.from_params({'request' : }) # headers contains the necessary Content-Type and Content-Length # datagen is a generator object that yields the encoded parameters datagen, headers = MultipartParam.from_file({"image1": open("DSC0001.jpg", "rb")}) # Create the Request object request = urllib2.Request("http://localhost:5000/upload_image", datagen, headers) # Actually do the request, and get the response print urllib2.urlopen(request).read()
def post_files(self, files, start, end, uploaded_queue_put, boundary, local_deploy): hub_session = self.hub_session hub_cookie = self.hub_cookie hub_pool = self.hub_pool while start < end: if self.stopped: uploaded_queue_put(None) # Make sure the waiting thread wakes up break item = files[start] start += 1 deploy_path, relative_path, file_size, file_hash, file_md5 = item try: if local_deploy: guessed_type = guess_type(relative_path)[0] if guessed_type is None: guessed_type = "" params = {'file.content_type': guessed_type, 'file.name': relative_path, 'file.path': deploy_path, 'session': hub_session, 'hash': file_hash, 'length': str(file_size), 'md5': file_md5} if deploy_path.endswith('.gz'): params['encoding'] = 'gzip' r = hub_pool.request('POST', '/dynamic/upload/file', fields=params, headers={'Cookie': hub_cookie}, timeout=self.hub_timeout) else: params = [MultipartParam('file', filename=relative_path, filetype=guess_type(relative_path)[0], fileobj=open(deploy_path, 'rb')), ('session', hub_session), ('hash', file_hash), ('length', file_size), ('md5', file_md5)] if deploy_path.endswith('.gz'): params.append(('encoding', 'gzip')) headers = get_headers(params, boundary) headers['Cookie'] = hub_cookie params = MultipartParam.from_params(params) params = MultipartReader(params, boundary) r = hub_pool.urlopen('POST', '/dynamic/upload/file', params, headers=headers, timeout=self.hub_timeout) except IOError: self.stop('Error opening file "%s".' % deploy_path) continue except (HTTPError, SSLError, ValueError) as e: self.stop('Error uploading file "%s": "%s".' % (relative_path, e)) continue if r.headers.get('content-type', '') != 'application/json; charset=utf-8': self.stop('Hub error uploading file "%s".' % relative_path) continue answer = json_loads(r.data) # pylint: disable=E1103 if r.status != 200: if answer.get('corrupt', False): self.stop('File "%s" corrupted on transit.' % relative_path) else: msg = answer.get('msg', None) if msg: self.stop('Error when uploading file "%s".\n%s' % (relative_path, msg)) else: self.stop('Error when uploading file "%s": "%s"' % (relative_path, r.reason)) continue if not answer.get('ok', False): self.stop('Error uploading file "%s".' % relative_path) continue # pylint: enable=E1103 uploaded_queue_put((relative_path, file_size, file_hash)) answer = None r = None params = None relative_path = None deploy_path = None item = None