def _extract_base_headers(self, post_vars): data = { 'frontend': post_vars.getvalue('frontend', '').replace('"', '"'), 'reply': post_vars.getvalue('reply', '').replace('"', '"'), 'board': post_vars.getvalue('board', '').replace('"', '"'), 'target': post_vars.getvalue('target', '').replace('"', '"'), 'name': post_vars.getvalue('name', '').replace('"', '"'), 'email': post_vars.getvalue('email', '').replace('"', '"'), 'subject': post_vars.getvalue('subject', '').replace('"', '"') } data['custom_headers'] = self._custom_headers_to_html(self._get_custom_headers(post_vars, data['frontend'])) if post_vars.getvalue('hash', '') != '': data['comment'] = post_vars.getvalue('comment', '').replace('"', '"') data['file_name'] = post_vars.getvalue('file_name', '').replace('"', '"') data['file_ct'] = post_vars.getvalue('file_ct', '').replace('"', '"') data['file_b64'] = post_vars.getvalue('file_b64', '').replace('"', '"') else: data['comment'] = base64.encodestring(post_vars.getvalue('comment', '')) data['file_name'], data['file_ct'], data['file_b64'] = '', '', '' if 'allowed_files' in self.origin.frontends[data['frontend']]: try: data['file_name'] = post_vars['file'].filename.replace('"', '"') except KeyError: pass if data['file_name']: data['file_ct'] = post_vars['file'].type.replace('"', '"') f = io.StringIO() base64.encode(post_vars['file'].file, f) data['file_b64'] = f.getvalue() f.close() return data
def CreateBase64HNAPRequest(file, actionName, elementName): # Add the request header content = StringIO.StringIO() content.write('''\ <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <soap:Body> <%s xmlns="http://purenetworks.com/HNAP1/"> <%s> ''' % (actionName, elementName)) # Base64 encode the firmware file fh = open(file, "rb") try: base64.encode(fh, content) except: fh.close() raise fh.close() # Add the footer content.write('''\ </%s> </%s> </soap:Body> </soap:Envelope>''' % (elementName, actionName)) # Return the request soapAction = "http://purenetworks.com/HNAP1/%s" % actionName return (soapAction, content.getvalue())
def dump(elm, out=sys.stdout, indent=0, depth=0, suppress=False): if indent and not suppress: out.write(" " * depth) if isinstance(elm, dict): out.write(u"<dict>") if indent: out.write(u"\n") for key, val in elm.iteritems(): if indent: out.write(" " * (depth + indent)) out.write(u"<key>%s</key>" % key) dump(val, out, indent, depth+indent, suppress=True) if indent: out.write(" " * depth) out.write(u"</dict>") elif isinstance(elm, (list, tuple)): out.write(u"<array>") if indent: out.write(u"\n") for item in elm: dump(item, out, indent, depth+indent) if indent: out.write(" " * depth) out.write(u"</array>") elif isinstance(elm, basestring): out.write(u"<string>%s</string>" % escape(elm)) elif isinstance(elm, bool): if elm: out.write(u"<true/>") else: out.write(u"<false/>") elif isinstance(elm, (int, long)): out.write(u"<integer>%d</integer>" % elm) elif isinstance(elm, float): out.write(u"<real>%f</real>" % elm) elif elm is None: out.write(u"<null/>") elif isinstance(elm, Data): out.write(u"<data>") base64.encode(StringIO(elm), out) out.write(u"</data>") elif isinstance(elm, datetime.datetime): raise Exception("not implemented") else: raise Exception("unknown data type '%s' for value '%s'" % (str(type(elm)), str(elm))) if indent: out.write(u"\n")
def _save_to_qiniu(self): import qiniu output = BytesIO() self._source.seek(0) base64.encode(self._source, output) self._source.seek(0) output.seek(0) hex_octet = lambda: hex(int(0x10000 * (1 + random.random())))[-4:] key = ''.join(hex_octet() for _ in range_type(4)) key = '{0}.{1}'.format(key, self.extension) data = { 'name': self._name, 'key': key, 'ACL': self._acl, 'mime_type': self._type, 'metaData': self._metadata, } response = client.post('/qiniu', data) content = response.json() self.id = content['objectId'] self._url = content['url'] uptoken = content['token'] ret, info = qiniu.put_data(uptoken, key, self._source) if info.status_code != 200: raise LeanCloudError(1, 'the file is not saved, qiniu status code: {0}'.format(info.status_code))
def base64_encodefile(fname): ''' Read a file from the file system and return as a base64 encoded string .. versionadded:: Boron Pillar example: .. code-block:: yaml path: to: data: | {{ salt.hashutil.base64_encodefile('/path/to/binary_file') | indent(6) }} The :py:func:`file.decode <salt.states.file.decode>` state function can be used to decode this data and write it to disk. CLI Example: .. code-block:: bash salt '*' hashutil.base64_encodefile /path/to/binary_file ''' encoded_f = StringIO.StringIO() with open(fname, 'rb') as f: base64.encode(f, encoded_f) encoded_f.seek(0) return encoded_f.read()
def upload_file(upload_file_name, temp_file_name='encoded.csv', split_file_format="{orig_file}_{id}.{orig_ext}", parent_folder_id='0B46HJMu9Db4xTUxhQ0x4WHpfVmM'): file_name = os.path.basename(upload_file_name) # Encode file. base64.encode(open(upload_file_name), open(temp_file_name, 'w+')) # Split file. num_split_files, file_names = splitfile(temp_file_name, SPLIT_SIZE, split_file_format) # Start upload threads. start = time.time() file_id = uuid.uuid1() thread_pool = ThreadPoolExecutor(max_workers=MAX_DOWNLOADS) for i in range(num_split_files): current_file_name = file_names[i] up_t = upload_worker.UploadWorker(index=i + 1, file_id=file_id, filename=file_name, parent_folder_id=parent_folder_id, total_file_num=num_split_files, upload_file_name=current_file_name) future = thread_pool.submit(up_t.run) # Wait for completion. thread_pool.shutdown() end = time.time() m, s = divmod(end - start, 60) print "Overall time taken: ", m, "m ", s, "s" return file_id
def _complexOutput(self, output, complexOutput): #Checks for the correct output and logs self.checkMimeTypeOutput(output) complexOutput["mimetype"] = output.format["mimetype"] complexOutput["encoding"] = output.format["encoding"] complexOutput["schema"] = output.format["schema"] if output.format["mimetype"] is not None: # CDATA section in output #attention to application/xml if output.format["mimetype"].find("text") < 0 and output.format["mimetype"].find("xml")<0: #complexOutput["cdata"] = 1 os.rename(output.value, output.value+".binary") base64.encode(open(output.value+".binary"),open(output.value,"w")) # set output value complexOutput["complexdata"] = open(output.value,"r").read() # remove <?xml version= ... part from beginning of some xml # documents #Better <?xml search due to problems with \n if output.format["mimetype"] is not None: if output.format["mimetype"].find("xml") > -1: beginXMLidx=complexOutput["complexdata"].find("?>") #All <?xml..?> will be beginXMLidx + 2 #beginXml = complexOutput["complexdata"].split("\n")[0] if beginXMLidx > -1: complexOutput["complexdata"] = complexOutput["complexdata"].replace(complexOutput["complexdata"][:(beginXMLidx+2)],"") return complexOutput
def submit(request): if request.method == 'POST': form = SubmitArticle(request.POST) if form.is_valid(): newart = Article(name=request.POST['name'], writer=request.POST['writer'], short_text=request.POST['short_text'], text=request.POST['text'], date=timezone.now(), views=0) newart.save() numberofimages = int(request.POST['articlefakeimage_set-TOTAL_FORMS']) for number in range(numberofimages): if 'articlefakeimage_set-' + str(number) + '-image' in request.FILES: newimagetosave = ArticleImages() newimagetosave.article = newart d = StringIO.StringIO() filee = request.FILES['articlefakeimage_set-' + str(number) + '-image'] base64.encode(filee, d) s = d.getvalue() newimagetosave.image = "data:%s;base64,%s" % (filee.content_type, s) newimagetosave.save() template_name = 'news/submit.html' context = {'user_form': form, 'err': '', 'suc': True} return render(request, template_name, context) else: template_name = 'news/submit.html' context = {'user_form': form, 'err': 'Please fill in all the fields'} return render(request, template_name, context) else: template_name = 'news/submit.html' form = SubmitArticle() context = {'user_form': form, 'err': ''} return render(request, template_name, context)
def create_blobbone(directory, filename="blobbone.py"): tar_obj = StringIO.StringIO() tar = tarfile.open(mode='w:gz', fileobj=tar_obj) tar.add(directory) tar.close() tar_obj.seek(0) header_code = "import tarfile, base64, os, StringIO\n" uncompacting_code = ( "encoded_obj = StringIO.StringIO()\n" "tar_obj = StringIO.StringIO()\n" "encoded_obj.write(blob)\n" "encoded_obj.seek(0)\n" "base64.decode(encoded_obj, tar_obj)\n" "tar_obj.seek(0)\n" "tar = tarfile.open('r:gz', fileobj=tar_obj)\n" "tar.extractall()\n" "tar.close()\n") f2 = open(filename, 'wb') f2.write(header_code) f2.write('blob = r"""') base64.encode(tar_obj, f2) f2.write('"""\n') f2.write(uncompacting_code) f2.close() print "Blobbone created. Run python %s to decompress it." % filename
def createTmpBase64(rLayer): try: # tmpFile = tempfile.NamedTemporaryFile(prefix="base64", delete=False) # infile = open(unicode(rLayer.source())) # tmpFileName = tmpFile.name # outfile = open(tmpFileName, 'w') # base64.encode(infile,outfile) # outfile.close() # outfile = open(tmpFileName, 'r') # base64String = outfile.read() # outfile.close() # os.remove(tmpFile.name) tmpFile = tempfile.NamedTemporaryFile(prefix="base64", delete=False) infile = open(rLayer.source()) tmpFileName = tmpFile.name outfile = tmpFile #open(tmpFileName, 'w') base64.encode(infile,outfile) outfile.close() infile.close() outfile = open(tmpFileName, 'r') base64String = outfile.read() outfile.close() os.remove(tmpFileName) except: QMessageBox.critical(None, QApplication.translate("QgsWps","Error"), QApplication.translate("QgsWps","Unable to create temporal file: ") + filename + QApplication.translate("QgsWps"," for base64 encoding") ) return base64String
def to_xml(cls, value, tns, parent_elt, name='retval'): ''' This class method takes the data from the attachment and base64 encodes it as the text of an Element. An attachment can specify a file_name and if no data is given, it will read the data from the file ''' assert isinstance(value, cls) element = etree.SubElement(parent_elt, '{%s}%s' % (tns,name)) if value.data: # the data has already been loaded, just encode # and return the element element.text = base64.encodestring(value.data) elif value.file_name: # the data hasn't been loaded, but a file has been # specified data_string = cStringIO.StringIO() file_name = value.file_name file = open(file_name, 'rb') base64.encode(file, data_string) file.close() # go back to the begining of the data data_string.seek(0) element.text = str(data_string.read()) else: raise Exception("Neither data nor a file_name has been specified")
def _encode_img(self, file_path): """Returns image base64 string representation and makes a cache file""" filename = file_path.rpartition(os.sep)[2] need_cache = True content = "" cache_file = "%s_cache" % os.path.join(CACHE_DIR, filename) try: with open(cache_file, 'r') as cached_file: content = cached_file.read() need_cache = False except IOError: pass if need_cache: try: image = open(file_path, 'r') out = StringIO() base64.encode(image, out) content = out.getvalue().replace('\n', '') except IOError: pass else: try: with open(cache_file, 'w+') as cached_file: cached_file.write(content) except IOError: pass return content
def send_mail(server, port, sender, recipient, subject, message, username = '', password = '', attachment = '', mime_type = ''): from StringIO import StringIO from smtplib import SMTP from MimeWriter import MimeWriter import base64 # Build the message header. message_obj = StringIO() writer = MimeWriter(message_obj) writer.addheader('Subject', subject) writer.startmultipartbody('mixed') part = writer.nextpart() # Add the message body. body = part.startbody('text/plain') body.write(message) # And (optionally) the attachment. if attachment != '': part = writer.nextpart() part.addheader('Content-Transfer-Encoding', 'base64') body = part.startbody(mime_type) base64.encode(StringIO(attachment), body) # Finish the message. writer.lastpart() # Send the mail (and authenticate if necessary). smtp = SMTP(server, port) if username != '': smtp.login(username, password) smtp.sendmail(sender, recipient, message_obj.getvalue()) smtp.quit() return True
def get_image(self, size=DISPLAY_SIZE, page=DEFAULT_PAGE_NUMBER, zoom=DEFAULT_ZOOM_LEVEL, rotation=DEFAULT_ROTATION, as_base64=False, version=None): if zoom < ZOOM_MIN_LEVEL: zoom = ZOOM_MIN_LEVEL if zoom > ZOOM_MAX_LEVEL: zoom = ZOOM_MAX_LEVEL rotation = rotation % 360 try: file_path = self.get_valid_image(size=size, page=page, zoom=zoom, rotation=rotation, version=version) except UnknownFileFormat: file_path = get_icon_file_path(self.file_mimetype) except UnkownConvertError: file_path = get_error_icon_file_path() except: file_path = get_error_icon_file_path() if as_base64: image = open(file_path, 'r') out = StringIO() base64.encode(image, out) return u'data:%s;base64,%s' % (get_mimetype(open(file_path, 'r'), file_path, mimetype_only=True)[0], out.getvalue().replace('\n', '')) else: return file_path
def read_file(self, filename, resource_name): if self.valid_extensions and os.path.splitext(filename)[1][1:] not in self.valid_extensions: print "Skipping:", filename return '' resource_name = resource_name.replace('\\', '/') print "Reading: '%s' --> '%s'" % (filename, resource_name) mimetype = mimetypes.guess_type(filename)[0] is_code = (mimetype in CODE_MIMETYPES) is_text = (mimetype in TEXT_MIMETYPES) is_image = (mimetype.split('/')[0] == 'image') data = StringIO() if is_code: file_code = codecs.open(filename, 'r', encoding='utf-8').read() file_code = self.parse_supers(file_code) code = CODE_RESOURCE_TEMPLATE % (resource_name, mimetype, file_code) elif is_text: code = TEXT_RESOURCE_TEMPLATE % (resource_name, mimetype, json.dumps(open(filename, 'r').read())) elif is_image: base64.encode(open(filename, 'rb'), data) code = IMAGE_RESOURCE_TEMPLATE % (resource_name, mimetype, mimetype, data.getvalue().replace('\n', '').replace('\r', '')) else: # Binaries base64.encode(open(filename, 'rb'), data) code = BINARY_RESOURCE_TEMPLATE % (resource_name, mimetype, data.getvalue().replace('\n', '').replace('\r', '')) return code
def save(self): if self._source: output = cStringIO.StringIO() self._source.seek(0) base64.encode(self._source, output) self._source.seek(0) data = { 'base64': output.getvalue(), '_ContentType': self._type, 'mime_type': self._type, 'metaData': self._metadata, } elif self._url and self.metadata['__source'] == 'external': data = { 'name': self._name, 'ACL': self._acl, 'metaData': self._metadata, 'mime_type': self._type, 'url': self._url, } else: raise ValueError response = client.post('/files/{0}'.format(self._name), data) content = utils.response_to_json(response) self._name = content['name'] self._url = content['url'] self.id = content['objectId'] if 'size' in content: self._metadata['size'] = content['size'] return self
def to_xml(cls, value, name='retval', nsmap=ns): '''This class method takes the data from the attachment and base64 encodes it as the text of an Element. An attachment can specify a filename and if no data is given, it will read the data from the file ''' if value.__class__ is not Attachment: raise Exception("Do not know how to serialize class %s" % type(value)) element = create_xml_element(name, nsmap) if value.data: # the data has already been loaded, just encode # and return the element element.text = base64.encodestring(value.data) elif value.fileName: # the data hasn't been loaded, but a file has been # specified data_string = cStringIO.StringIO() fileName = value.fileName file = open(fileName, 'rb') base64.encode(file, data_string) file.close() # go back to the begining of the data data_string.seek(0) element.text = str(data_string.read()) else: raise Exception("Neither data nor a filename has been specified") return element
def binary_diff(old_filename, old_lines, new_filename, new_lines, to_file): temp = StringIO() internal_diff(old_filename, old_lines, new_filename, new_lines, temp, allow_binary=True) temp.seek(0) base64.encode(temp, to_file) to_file.write('\n')
def __shell(self): #self.cliente.setblocking(0) self.cliente.settimeout(50) while True: cmd = raw_input(V + "root@root-PC:~$ " + C) if cmd == "salir": break # Encodeamos y pickleamos try: cmd = dumps(encode(cmd)) except Exception as e: print R + " [!]" + N + " Error al codificar: " + C + str(e) + B continue # Enviamos try: self.cliente.send(cmd) except socket.error as e: print R + " [!]" + N + " Error al enviar: " + C + str(e) + B continue # Recibimos stdout, stderr = self.__recibir() if stdout: print stdout[:-1] if stderr is not None: print stderr self.cliente.send(dumps(encode("salir"))) self.__salir()
def createTmpBase64(rLayer): tmpFile = tempfile.NamedTemporaryFile(prefix="base64", delete=False) tmpFileName = tmpFile.name # try: # tmpFile = tempfile.NamedTemporaryFile(prefix="base64", delete=False) # infile = open(unicode(rLayer.source())) # tmpFileName = tmpFile.name # outfile = open(tmpFileName, 'w') # base64.encode(infile,outfile) # outfile.close() # outfile = open(tmpFileName, 'r') # base64String = outfile.read() # outfile.close() # os.remove(tmpFile.name) if rLayer.dataProvider().name() == 'ogr' or rLayer.dataProvider().name() == 'gdal': infile = open(rLayer.source()) outfile = tmpFile #open(tmpFileName, 'w') base64.encode(infile,outfile) outfile.close() infile.close() outfile = open(tmpFileName, 'r') base64String = outfile.read() outfile.close() os.remove(tmpFileName) return base64String else: QMessageBox.critical(None, QApplication.translate("QgsWps",'Error'), QApplication.translate("QgsWps",'Datatype %s of layer %s is not supported!' % (rLayer.dataProvider().name(), rLayer.name()))) return ''
def AddAttachment(self): attach = self.__m.nextpart() attach.addheader("Content-Transfer-Encoding", "base64") attach.addheader("Content-Disposition", 'attachment; filename="%s"' % filename) f = attach.startbody('application/octet-stream; name="%s"' % filename) attach.flushheaders() base64.encode(open(path + filename, "r"), f)
def generateResponse(msg,outputList): doc = xml.dom.minidom.parseString(msg) job=doc.getElementsByTagName("job")[0] id=job.getAttribute("id") owner=job.getAttribute("owner") f= '<job id="' + id + '" owner="' + owner + '">' previousPath="" numOutputs = 0 for file in outputList: if not os.path.isfile(file): continue numOutputs += 1 parts= file.split("/") path="/".join(parts[:-1]) out=open(file+'.64','w') base64.encode(open(file,'r'),out) out.flush() out.close() out=open(file+'.64','r') encoded=out.read().strip() out.close() if (path != previousPath): if (previousPath !=""): f+="</folder>" f += '\t<folder path="' + path + '">\n' f += '\t\t<output filename="'+parts[-1]+'">'+encoded +'</output>\n' if numOutputs > 0: f+='</folder>\n' f+='</job>' return f
def create_blobbone(directory, filename): """ Create an autoextracting python executable containing the given data. Tested with files up to 20 MB ! :param directory: the directory to compress :param filename: the target filename for the compressed data. """ tar_obj = StringIO.StringIO() tar = tarfile.open(mode='w:gz', fileobj=tar_obj) tar.add(directory, arcname='') tar.close() tar_obj.seek(0) header_code = "import tarfile, base64, os, StringIO\n" uncompacting_code = ( "encoded_obj = StringIO.StringIO()\n" "tar_obj = StringIO.StringIO()\n" "encoded_obj.write(blob)\n" "encoded_obj.seek(0)\n" "base64.decode(encoded_obj, tar_obj)\n" "tar_obj.seek(0)\n" "tar = tarfile.open('r:gz', fileobj=tar_obj)\n" "tar.extractall(path='%s')\n" "tar.close()\n" % os.path.join('.', os.path.basename(directory))) with open(filename, 'wb') as f: f.write(header_code) f.write('blob = r"""') base64.encode(tar_obj, f) f.write('"""\n') f.write(uncompacting_code)
def base64Enc(inf,outf): inf=open(inf,"r") outf=open(outf,"wb") base64.encode(inf,outf) #for l in inf: # outf.write(base64.b64encode(l)+"\n") outf.close() inf.close()
def emailAttachment(self,subject): """ It uses the MIMEWriter,StringIO and base64 module for creating the subject of the mail, also defining the body of the mail and attaching the file and encoding the mail with base64 encrytion technique for security reasons """ message = StringIO.StringIO() writer = MimeWriter.MimeWriter(message) writer.addheader('Subject', subject) writer.startmultipartbody('mixed') # start off with a text/plain part try : part = writer.nextpart() body = part.startbody('text/plain') body.write('\n\rThe Result is stored at the path %s\n\rRegards, \n\rNextone-SIT' %self.path) except : self.log.error("Exception : Error in the subject part of the mail to be sent") raise EmailAttachError("Exception :Error in the subject part of the mail ") # now add an Attachment try : part = writer.nextpart() part.addheader('Content-Transfer-Encoding', 'base64') part.addheader('Content-Disposition', 'attachment; filename %s' % self.path) body = part.startbody('application/Octet-Stream; name=%s' % self.path) base64.encode(open(self.path, 'r'), body) except : self.log.error("Exception : Error in the body part of the mail to be sent") raise EmailAttachError("Exception :Error in the body part of the mail ") # finish off writer.lastpart() # send the mail try : s_toaddrs = string.join(self.toaddrs,",") # For Ticket 34247 message_getvalue = message.getvalue() + s_toaddrs except : self.log.error("Error : The to-address %s is errorneous" %self.toaddrs) raise EmailAttachError("Exception : The toaddress is errorneous") smtp = smtplib.SMTP("%s" %self.mailserver) # Added for ticket 34247 try : smtp.login(self.login,self.passwd) except : self.log.error("Error: The login function is not able to Authenticate Login or password") raise EmailAttachError("Exception :The value assigned to login %s or passwd %s is errorneous" %(self.login,self.passwd)) try : smtp.sendmail(self.fromaddr,self.toaddrs,message_getvalue) except : self.log.error("Exception : The value assigned to to-addressess %s and from-address %s is errorneous" %(self.toaddrs,self.fromaddr)) raise EmailAttachError("Exception :The value assigned to to-addressess %s and from-address %s is errorneous" %(s_toaddrs,self.fromaddr)) smtp.quit()
def get_file(self, filename): """Retrive file from test data""" path = addons.get_module_resource('async_move_line_importer', 'tests', 'data', filename) with open(path) as test_data: with tempfile.TemporaryFile() as out: base64.encode(test_data, out) out.seek(0) return out.read()
def make_base64(): input = open('ant.tar.gz', mode='rb') output = open('ant.tar.gz.base64', 'wb') try: base64.encode(input, output) finally: input.close() output.close()
def encode_file(email_filename, file_path): if fake: email_filename += FAKE_EXTENSION maintype, encoding = mimetypes.guess_type(email_filename) if not maintype: maintype = 'application/octet-stream' fhmail.write(sub_header % {'maintype':maintype, 'filename':email_filename}) if verbose: print 'Converting+writing base64 to "%s" ...' % temp_email_file base64.encode(file(file_path,'rb'),fhmail)
def dump_manager_deployment(): name = 'manager' file_obj = BytesIO() output = StringIO() with tarfile.open(fileobj=file_obj, mode='w:gz') as tar: tar.add(os.path.join(_workdir(), name), arcname=name) file_obj.seek(0) base64.encode(file_obj, output) return output.getvalue()
def _get_original(self, image_path): try: with open(image_path, 'rb') as f: buffer = StringIO() base64.encode(f, buffer) except Exception: return {'code':500, 'json':{'error':True, 'reason':"Failed to get image"}} else: headers = {'Content-Type':"application/octet-stream"} return {'code':200, 'headers':headers, 'base64':buffer.getvalue()}
def encode_password(shortcode, passkey, timestamp): """Generate and return a base64 encoded password for online access. """ return base64.encode(shortcode + passkey + timestamp)
def convert_img(self, path): stream = BytesIO() base64.encode(self.zf.open('word/' + path), stream) stream.seek(0) return self.src_attr % (path[:-3], stream.read().decode('unicode_escape'))
def base64Encode(data): return base64.encode(data)
def encrypt(self, val): return base64.encode(val)
def reactToAMQPMessage(message, send_back): """ React to given (AMQP) message. `message` is expected to be :py:func:`collections.namedtuple` structure from :mod:`.structures` filled with all necessary data. Args: message (object): One of the request objects defined in :mod:`.structures`. send_back (fn reference): Reference to function for responding. This is useful for progress monitoring for example. Function takes one parameter, which may be response structure/namedtuple, or string or whatever would be normally returned. Returns: object: Response class from :mod:`structures`. Raises: ValueError: if bad type of `message` structure is given. """ user_db = _UserHandler( conf_path=settings.ZEO_CLIENT_CONF_FILE, project_key=settings.PROJECT_KEY, ) status_db = _StatusHandler( conf_path=settings.ZEO_CLIENT_CONF_FILE, project_key=settings.PROJECT_KEY, ) cache_db = _CacheHandler( conf_path=settings.ZEO_CLIENT_CONF_FILE, project_key=settings.PROJECT_KEY, ) if _instanceof(message, SaveLogin): return user_db.add_user( username=message.username, pw_hash=message.password_hash, ) elif _instanceof(message, RemoveLogin): status_db.remove_user(username=message.username) return user_db.remove_user(username=message.username) elif _instanceof(message, CacheTick): if user_db.is_empty(): return AfterDBCleanupRequest() if cache_db.is_empty(): return # this will pop the RequestInfo from `cache_db` if success with cache_db.pop_manager() as cached_request: cached_file = cached_request.get_file_obj() # convert the file to base64 memory-efficient way with tempfile.TemporaryFile() as tmp_file: base64.encode(cached_file, tmp_file) tmp_file.seek(0) req = UploadRequest( username=cached_request.username, rest_id=cached_request.rest_id, b64_data=tmp_file.read(), metadata=cached_request.metadata, ) cached_file.close() return req elif _instanceof(message, StatusUpdate): status_db.save_status_update( rest_id=message.rest_id, message=message.message, timestamp=message.timestamp, book_name=message.book_name, pub_url=message.pub_url, ) return raise ValueError("'%s' is unknown type of request!" % str(type(message)))
#! /usr/bin/env python3 """ This script is only for unittests, And not to be used for production. """ import sys import io import pickle import base64 if __name__ == '__main__': buffer = io.BytesIO() data = {'cpu': 40, 'memory': 50} # Dumping pickle.dump(data, buffer) # Encrypting # TODO: encrypt data using pycrypto, not required, because the connection is made by SSH. # Encode the binary as base64 buffer.seek(0) base64.encode(buffer, sys.stdout.buffer)
import base64 msg = input("message:") print("original", msg) print("base64", base64.encode(msg.encode())
def getFilePath(self, filepath): with open(filepath, 'rb') as test_data: with tempfile.TemporaryFile() as out: base64.encode(test_data, out) out.seek(0) return filepath, out.read()
def continue_app(): global user_name, demo_name, demo_novel, demo_new global num_vis, demo_rate, demo_comm, image_file datasentScreen = appuifw.Text() datasentScreen.add(u'Great job, ' + user_name + u'!') datasentScreen.add(u'\n\nWe will slog your data now.') datasentScreen.add(u'\n\nThis might take a couple of mins.') datasentScreen.add(u'\nData will be encoded, wrapped in xml, and sent.') appuifw.app.body = datasentScreen user_name_filtered = user_name.replace(',', '_') demo_name_filtered = demo_name.replace(',', '_') demo_novel_filtered = demo_novel.replace(',', '_') demo_new_filtered = demo_new.replace(',', '_') demo_comm_filtered = demo_comm.replace(',', '_') for chr in user_name_filtered: if ord(chr) not in range(0, 128): user_name_filtered = user_name_filtered.replace(chr, '_') for chr in demo_name_filtered: if ord(chr) not in range(0, 128): demo_name_filtered = demo_name_filtered.replace(chr, '_') for chr in demo_novel_filtered: if ord(chr) not in range(0, 128): demo_novel_filtered = demo_novel_filtered.replace(chr, '_') for chr in demo_new_filtered: if ord(chr) not in range(0, 128): demo_new_filtered = demo_new_filtered.replace(chr, '_') for chr in demo_comm_filtered: if ord(chr) not in range(0, 128): demo_comm_filtered = demo_comm_filtered.replace(chr, '_') continueScreen = appuifw.Text() continueScreen.add(u'Are you done, ' + user_name + u'?') continueScreen.add( u'\n\nIf so, please return the equipment back to UrbanCENS.') continueScreen.add(u' If not, we will let you document another demo.') continueScreen.add(u'\n\n\nAre you done?') #pos = continueScreen.get_pos() continueScreen.add(u'\nYes (pencil) or No (c)') continueScreen.add(u'\nPress and hold the key.') #continueScreen.set_pos(pos) continueScreen.bind(EKeyEdit, done) continueScreen.bind(EKeyBackspace, not_done) date_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) image_output = StringIO.StringIO() image_input = open(image_file, "rb") base64.encode(image_input, image_output) image_data = image_output.getvalue() #print 'Image Base 64 Time %0.3fms' % ((t2-t1)*1000.) datasentScreen.add(u'\n\nData encoded...') xml = '<?xml version="1.0" encoding="UTF-8"?>' xml += '<table>' xml += '<row>' xml += '<field name="User_Name">' + user_name_filtered + '</field>' xml += '<field name="Demo_Name">' + demo_name_filtered + '</field>' xml += '<field name="Demo_Novel">' + demo_novel_filtered + '</field>' xml += '<field name="Demo_New">' + demo_new_filtered + '</field>' xml += '<field name="Num_Of_Visitors">' + str(num_vis) + '</field>' xml += '<field name="Rate_Of_Demo">' + str(demo_rate) + '</field>' xml += '<field name="Comment">' + demo_comm_filtered + '</field>' xml += '<field name="Image">' + image_data + '</field>' #xml += '<field name="Image">' + 'abc' + '</field>' xml += '<field name="Date_Time">' + date_time + '</field>' xml += '</row>' xml += '</table>' params = {} params['email'] = '*****@*****.**' params['pw'] = 'intel' params['data_string'] = xml params['type'] = 'xml' params['project_id'] = "39" params['tableName'] = 'ubicompDemo' params = urllib.urlencode(params) #print 'URL Encode Time %0.3fms' % ((t2-t1)*1000.) datasentScreen.add(u'\nXML encoded...') headers = {} headers['Content-type'] = 'application/x-www-form-urlencoded' headers['Accept'] = 'text/plain' conn = httplib.HTTPConnection("sensorbase.org") conn.request("POST", "/alpha/upload.php", params, headers) response = conn.getresponse() responseText = response.read() conn.close() #print 'HTTP Post Time %0.3fms' % ((t2-t1)*1000.) datasentScreen.add(u'\nHTTP POST...') e32.ao_sleep(1) #print responseText appuifw.app.body = continueScreen
def verification_key(self): "" return encode("%s,%s,%s" % (self.uid, self.id, self.pw))
def onPbemSend(argsList): import smtplib, MimeWriter, base64, StringIO szToAddr = argsList[0] szFromAddr = argsList[1] szSubject = argsList[2] szPath = argsList[3] szFilename = argsList[4] szHost = argsList[5] szUser = argsList[6] szPassword = argsList[7] print 'sending e-mail' print 'To:', szToAddr print 'From:', szFromAddr print 'Subject:', szSubject print 'Path:', szPath print 'File:', szFilename print 'Server:', szHost print 'User:'******'host or address empty' return 1 message = StringIO.StringIO() writer = MimeWriter.MimeWriter(message) writer.addheader('To', szToAddr) writer.addheader('From', szFromAddr) writer.addheader('Subject', szSubject) writer.addheader('MIME-Version', '1.0') writer.startmultipartbody('mixed') part = writer.nextpart() body = part.startbody('text/plain') body.write('CIV4 PBEM save attached') part = writer.nextpart() part.addheader('Content-Transfer-Encoding', 'base64') szStartBody = "application/CivBeyondSwordSave; name=%s" % szFilename body = part.startbody(szStartBody) base64.encode(open(szPath + szFilename, 'rb'), body) # finish off writer.lastpart() # send the mail try: smtp = smtplib.SMTP(szHost) if len(szUser) > 0: smtp.login(szUser, szPassword) smtp.sendmail(szFromAddr, szToAddr, message.getvalue()) smtp.quit() except smtplib.SMTPAuthenticationError, e: CyInterface().addImmediateMessage( "Authentication Error: The server didn't accept the username/password combination provided.", "") CyInterface().addImmediateMessage( "Error %d: %s" % (e.smtp_code, e.smtp_error), "") return 1
def create_id(req, pub, priv): return encode( hashlib.sha256(str(getrandbits(2048))).digest(), choice(['rA', 'aZ', 'gQ', 'hH', 'hG', 'aR', 'DD'])).rstrip("==")
def dump(obj, fp): input_fp = io.BytesIO(obj) _base64.encode(input_fp, fp)
def ECB_encode_date(self,key="BuctBuct",iv="BuctBuct"): data=self.read_date() k=des(key,ECB,iv,pad=None,padmode=PAD_PKCS5) d=k.encrypt(data) assert k.decrypt(d,padmode=PAD_PKCS5) == data return base64.encode(d)
def getFile(self, path): with open(path) as test_data: with tempfile.TemporaryFile() as out: base64.encode(test_data, out) out.seek(0) return path, out.read()
def process_block(self, block_text, user, block_id, **params): def int_or_none(val): """Returns an integer or none. May pass anything """ try: ret = int(val) except ValueError: ret = None return ret def int_or_default(val, default=0): """ Returns int or a given default """ try: ret = int(val) except ValueError: ret = default return ret def float_or_default(val, default=0.0): """ Returns float or a given default """ try: ret = float(val) except ValueError: ret = default return ret tokens = self.parse_block(block_text) # get our tokens db_name = tokens.get(self.ATTR_DB, settings.DATABASE_DEFAULT_ALIAS).strip() # chart title. empty by default chart_title = tokens.get(self.ATTR_TITLE, '').strip() # chart width and height in pixels. Beware, the user # inevitably will write anything but valid numbers chart_width = int_or_none(tokens.get(self.ATTR_WIDTH, '').strip()) chart_height = int_or_none(tokens.get(self.ATTR_HEIGHT, '').strip()) ylabel = tokens.get(self.ATTR_YLABEL, '').strip() xlabel = tokens.get(self.ATTR_XLABEL, '').strip() font_size = int_or_default( tokens.get(self.ATTR_FONT_SIZE, self.DEFAULT_FONT_SIZE)) if font_size <= 1: font_size = self.DEFAULT_FONT_SIZE if font_size >= 100: font_size = self.DEFAULT_FONT_SIZE # grid grid = tokens.get(self.ATTR_GRID, 'none').strip().lower() if grid not in ('x', 'y', 'both', 'none'): grid = 'none' # also check for negative numbers or zeroes if (chart_width is not None) and (chart_height is not None): if (chart_width <= 0) or (chart_height <= 0): # both none chart_width = None chart_height = None else: # some of them are none # set both to none chart_width = None chart_height = None # xtick rotation x_tick_rotation = int_or_default( tokens.get(self.ATTR_XTICK_ROTATION, self.DEFAULT_XTICK_ROTATION)) if x_tick_rotation > 90: x_tick_rotation = 90 if x_tick_rotation < -90: x_tick_rotation = -90 # legend legend_str = tokens.get(self.ATTR_LEGEND, self.ATTR_VALUE_NO).strip().lower() if legend_str not in (self.ATTR_VALUE_NO, self.ATTR_VALUE_YES): legend_str = self.ATTR_VALUE_NO # legend location legend_location = tokens.get( self.ATTR_LEGEND_LOCATION, self.DEFAULT_LEGEND_LOCATION).strip().lower() # this is not necessary, matplotlib does it by himself, # yet we check it here to avoid warnings on stdout if legend_location not in self.VALID_LEGEND_LOCATIONS: legend_location = self.DEFAULT_LEGEND_LOCATION # bar width bar_width = float_or_default( tokens.get(self.ATTR_BAR_WIDTH, self.DEFAULT_BAR_WIDTH), self.DEFAULT_BAR_WIDTH) if bar_width > self.MAX_BAR_WIDTH: bar_width = self.MAX_BAR_WIDTH if bar_width < self.MIN_BAR_WIDTH: bar_width = self.MIN_BAR_WIDTH # stacked stacked = tokens.get(self.ATTR_STACKED, self.ATTR_VALUE_NO).strip().lower() if stacked not in (self.ATTR_VALUE_YES, self.ATTR_VALUE_NO): stacked = self.ATTR_VALUE_NO con = None # if anything, we rethrow the exception try: db = settings.DATABASES.get(db_name, None) # non-existent database - exception if db == None: raise DwException(db_name, "Unknown database %s." % db_name) true_sql = tokens.sqltext # now that we have a sql, try to replace parameters in it. # Parameter looks like ${param_name} p = re.compile(u'\$\{[a-z]+[0-9]*\}') # get the list of all mentioned params lst = p.findall(true_sql) # loop through all these variables and try to replace # them with params passed to us for item in lst: stripped_item = item[2:-1] # TODO check for default value here value = 'None' if stripped_item in params: value = params[stripped_item] value = urllib.unquote(value) # replace it true_sql = true_sql.replace(item, value) # get a connection from somewhere con = self.get_con(db) cur = con.cursor() cur.execute(true_sql) row = cur.fetchone() # TODO Now if row is None it means the set is empty # in this case we have to generate a stub # for now assume the data is present # Get columns for labels # We need at least two columns # first one is x axis, column heading does not matter # the second is y axis values # the third one is the color of values # if there is no third column, use default color # which is gray columns = cur.description col_count = len(columns) if col_count < 2: raise DwException(db_name, "There should be at least two columns") # second column should be a number. Don't test it here. # we will try to test each value # every odd column is value # every even column is color # Now traverse all the rows # x_tick_marks is col 0 values x_tick_marks = list() # y_values is an array of bar heights # stored in the second column, fourth etc. columns # this is array of arrays y_values_array = list() y_colors_array = list() for i in range(1, col_count): is_value = i % 2 == 1 is_color = i % 2 == 0 if is_value is True: y_values = list() y_values_array.append(y_values) # add color array in any case y_colors = list() y_colors_array.append(y_colors) #if is_color is True: #y_colors_array.append(y_colors) #y_values = list() # bar_colors is an array of bar colors # stored in the third column # if there is no third column, # assume the color is grey #y_colors = list() # Now traverse. If row count is too big, # just stop where it exceeds the maximum allowed number # which we set to 100 for now curr_bar = 0 color_converter = ColorConverter() while row is not None: # tick mark as unicode mark = row[0] if mark is None: mark = '' if type(mark) is str: mark = mark.decode('utf-8') elif type(mark) is unicode: pass else: # TODO maybe a number # we need to convert it to a string # according to it's type (integer, decimal, float) # date, time, datetime mark = unicode(mark) x_tick_marks.append(mark) # collect values and colors for i in range(1, col_count): is_value = i % 2 == 1 is_color = i % 2 == 0 # value itself. plot does not support Decimal, # need to convert to float if is_value is True: value_index = i / 2 y_values = y_values_array[value_index] value = row[i] tp = type(value).__name__ if tp is 'Decimal': value = float(value) # all nulls are replaced with zeroes if value is None: value = 0.0 y_values.append(value) # now colors color_col = i + 1 y_colors = y_colors_array[value_index] # color himself +1 col if exists color_str = None if color_col < len(row): color_str = self.make_unicode_or_none(row[i + 1]) if color_str is None: color_str = self.DEFAULT_BAR_COLOR # Now try to make a color. If it's unsuccessful, # it returns an exception try: color_rgba = color_converter.to_rgba(color_str) except Exception, e: #sys.stderr.write("Color error: %s\n" % color_str) color_rgba = color_converter.to_rgba( self.USER_ERROR_BAR_COLOR) # Hopefully, color_rgba is now set # to a tuple of R, G, B and Alpha y_colors.append(color_rgba) # increment row counter curr_bar += 1 if curr_bar >= self.MAX_BARS: break else: row = cur.fetchone() # Now we have collected all the values, marks and colors con.close() con = None # Build a graph # 80 dots per inch. So 7 x 4 inches will make 560 x 320 pixels if (chart_width > 0) and (chart_height > 0): fig = plt.figure(figsize=[chart_width / 80, chart_height / 80], dpi=80) else: fig = plt.figure() ax = fig.add_subplot(111) # data size #N = len(curr_bar) # number of bar groups - scale units for x axis = number of query rows # arrange N between 0 and N-1. Returns an array ind = np.arange(curr_bar) # distribute equally # todo expose bar width if stacked == self.ATTR_VALUE_NO: width = bar_width / len( y_values_array) # width of the bars (measures?) else: width = bar_width min_value = 0 max_value = 0 for y_values in y_values_array: # remember it may be negative temp_min_value = min(y_values) min_value = min(temp_min_value, min_value) if min_value > 0: min_value = 0 temp_max_value = max(y_values) max_value = max(temp_max_value, max_value) if max_value < 0: max_value = 0 # now add additional 10% of min-max range at the top value_range = max_value - min_value top_y_axis_margin = value_range * self.DEFAULT_TOP_Y_AXIS_MARGIN # no margin for bottom bottom_y_axis_margin = 0 # if we have negative bars, set margin below too if min_value < 0: bottom_y_axis_margin = top_y_axis_margin # here we set final max and min y axis values to account for our margins max_y_axis_value = max_value + top_y_axis_margin min_y_axis_value = min_value - bottom_y_axis_margin # cycle through values and draw bars for i in range(len(y_values_array)): y_values = y_values_array[i] y_colors = y_colors_array[i] # bar labels # get a description # it's in columns 1,3,5 etc. # i=0 => col=1 # i=1 => col=3 col_index = i * 2 + 1 col = columns[col_index] bar_label = col[0] if type(col[0]) is str: bar_label = col[0].decode('utf-8') elif type(col[0]) is unicode: bar_label = col[0] # The Drawing if stacked == self.ATTR_VALUE_NO: rects1 = ax.bar(ind + i * width, y_values, width, color=y_colors, label=bar_label) else: rects1 = ax.bar(ind, y_values, width, color=y_colors, label=bar_label) ax.set_xlim(-1 + bar_width, len(ind)) ax.set_ylim(min_y_axis_value, max_y_axis_value) # draw horizontal grid lines if grid <> 'none': ax.grid(which='major', axis=grid) if ylabel <> '': plt.ylabel(ylabel, fontsize=font_size) if xlabel <> '': plt.xlabel(xlabel, fontsize=font_size) ax.set_title(chart_title, fontsize=font_size) # font for y major ticks for tick in ax.yaxis.get_major_ticks(): tick.label.set_fontsize(font_size) # ticks on top and bottom ax.set_xticks(ind + bar_width / 2) #canvas = FigureCanvasAgg(fig) x_tick_labels = ax.set_xticklabels(x_tick_marks) plt.setp(x_tick_labels, rotation=x_tick_rotation, fontsize=font_size) # legend if legend_str == self.ATTR_VALUE_YES: ax.legend(loc=legend_location, prop={'size': font_size}) plt.tight_layout() tempfiledir = cherrypy.config['dwwiki.tempfiledir'] tempfile.tempdir = tempfiledir try: f = tempfile.NamedTemporaryFile(delete=False) # print graph to a temporary file # TODO we assume it prints png. Yet I don't know # why. Is it the default format? #canvas.print_figure(f) #canvas.print_png(f) fig.savefig(f) f.close() # Now all we need is to encode the file in base64, # then serve it as an <img> input_file = open(f.name, 'rb') output_file = cStringIO.StringIO() base64.encode(input_file, output_file) # we have it as a png string #yield '<img alt="graph" src="data:image/png;base64,' yield '<img alt="graph" src="data:image/png;base64,' yield output_file.getvalue() yield "\"/>\n" output_file.close() input_file.close() finally: os.remove(f.name)
def dump_stage3(indenter, node, use_references, name=None): cls = type(node) if cls == types.InstanceType: cls = node.__class__ # For old style stuff if name is None: name_key = "" else: name_key = " label=" + quoteattr(name) if issubclass(cls, str): indenter.write_line("<str%s>%s</str>" % (name_key, escape(node))) elif issubclass(cls, float): indenter.write_line("<float%s>%s</float>" % (name_key, str(node))) elif issubclass(cls, bool): indenter.write_line("<bool%s>%s</bool>" % (name_key, str(node))) elif issubclass(cls, int): indenter.write_line("<int%s>%s</int>" % (name_key, str(node))) elif cls == Undefined: pass elif cls == list: indenter.write_line("<list%s>" % name_key, 1) for item in node: dump_stage3(indenter, item, use_references) indenter.write_line("</list>", -1) elif cls == dict: indenter.write_line("<dict%s>" % name_key, 1) for key, val in node.iteritems(): if not isinstance(key, str): raise FilterError("ZML supports only strings as dictionary keys.") dump_stage3(indenter, val, use_references, key) indenter.write_line("</dict>", -1) elif cls == tuple: indenter.write_line("<tuple%s>" % name_key, 1) for item in node: dump_stage3(indenter, item, use_references) indenter.write_line("</tuple>", -1) elif cls == numpy.ndarray: shape = node.shape indenter.write_line("<array%s>" % name_key, 1) indenter.write("<shape>") for value in node.shape: indenter.write("%s " % value) indenter.write("</shape>", True) indenter.write("<cells>") for value in numpy.ravel(node): indenter.write("%s " % value) indenter.write("</cells>", True) indenter.write_line("</array>", -1) elif cls == StringIO.StringIO: indenter.write("<binary%s>" % name_key) node.seek(0) base64.encode(node, f) indenter.write("</binary>", True) elif cls == Translation: indenter.write_line("<translation%s>" % name_key, 1) dump_stage3(indenter, node.t, use_references, name="translation_vector") indenter.write_line("</translation>", -1) elif cls == Rotation: indenter.write_line("<rotation%s>" % name_key, 1) dump_stage3(indenter, node.r, use_references, name="rotation_matrix") indenter.write_line("</rotation>", -1) elif cls == Complete: indenter.write_line("<transformation%s>" % name_key, 1) dump_stage3(indenter, node.t, use_references, name="translation_vector") dump_stage3(indenter, node.r, use_references, name="rotation_matrix") indenter.write_line("</transformation>", -1) elif cls == UnitCell: indenter.write_line("<unit_cell%s>" % name_key, 1) dump_stage3(indenter, node.matrix, use_references, name="matrix") dump_stage3(indenter, node.active, use_references, name="active") indenter.write_line("</unit_cell>", -1) elif cls == Expression: indenter.write_line("<expression%s>%s</expression>" % (name_key, escape(node.code))) elif issubclass(cls, ModelObject): if node in identifiers: if use_references: indenter.write_line("<reference to='%i' />" % identifiers[node]) else: indenter.write_line("<model_object%s id='%i' class='%s'>" % (name_key, identifiers[node], node.class_name()), 1) for key, item in node.__getstate__().iteritems(): dump_stage3(indenter, item, key!="children", key) indenter.write_line("</model_object>", -1) else: raise FilterError, "Can not handle node %s of class %s" % (node, cls)
def iter2base(arr, b_conv): b_ips = bz2.compress(b"".join(map(b_conv, arr))) b_in, b_out = io.BytesIO(b_ips), io.BytesIO() base64.encode(b_in, b_out) return b_out.getvalue()
# - Vulnerable to frequency analysis attacks, use scripts in 'addNoise' directory # to add entropy to your cloaked payloads (must strip away prior to decloaking) # - Creates temporary Base64 file in local directory and deletes when finished, # but does not do "secure delete" (potential digital forensics trail) import os, sys, getopt, base64 array64 = list( "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/+=") payloadB64File = "payloadB64.txt" if (len(sys.argv) != 3): print "usage: cloakify.py <payloadFilename> <cipherFilename>" exit else: base64.encode(open(sys.argv[1]), open(payloadB64File, "w")) with open(payloadB64File) as file: payloadB64 = file.read() with open(sys.argv[2]) as file: arrayCipher = file.readlines() for char in payloadB64: if char != '\n': print arrayCipher[array64.index(char)], if os.path.exists(payloadB64File): os.remove(payloadB64File)
def encode(self, out): import base64, StringIO out.write("<value><base64>\n") base64.encode(StringIO.StringIO(self.data), out) out.write("</base64></value>\n")
import base64 import codecs import binascii from requests.utils import requote_uri from urllib.parse import unquote text = input("Enter text: ") print("Text length:", len(text)) print(''' ------------------------------- Base 64 ------------------------------- ''') print("Base64 decode (if any): ", base64.decode(text)) print("Base64 encode (if any): ", base64.encode(text)) print(''' ------------------------------- Base 32 ------------------------------- ''') print("Base32 decode (if any): ", base32.decode(text)) print("Base32 encode (if any): ", base32.encode(text))
y8PgwH8AfAxFzC0JzeAMtratAsC/ffwAAAD//wBlAJr/BGKAyCAA4AAAAvgeYTAwHd1kmQF5chkG ABoMIHcL5xVpTfQbUqzlAAAErwAQBgAAEOClA5D9il08AEh/tUzdCBsXkbgACED+woQg8Si9VeqY lODCn7lmF6NhnAEYgAAA/NMIAAAAAAD//2JgjLZgVGBg5Pv/Tvpc8hwGBjYGJADjHDrAwPzAjv/H /Wf3PzCwtzcwHmBgYGcwbZz8wHaCAQMDOwMDQ8MCBgYOC3W7mp+f0w+wHOYxO3OG+e376hsMZjk3 AAAAAP//YmCMY2A4wMAIN5e5gQETPD6AZisDAwMDgzSDAAPjByiHcQMDAwMDg1nOze1lByRu5/47 c4859311AYNZzg0AAAAA//9iYGDBYihOIIMuwIjGL39/fwffA8b//xv/P2BPtzzHwCBjUQAAAAD/ /yLFBrIBAAAA//9i1HhcwdhizX7u8NZNzyLbvT97bfrMf/QHI8evOwcSqGUJAAAA//9iYBB81iSw pEE170Qrg5MIYydHqwdDQRMrAwcVrQAAAAD//2J4x7j9AAMDn8Q/BgYLBoaiAwwMjPdvMDBYM1Tv oJodAAAAAP//Yqo/83+dxePWlxl3npsel9lvLfPcqlE9725C+acfVLMEAAAA//9i+s9gwCoaaGMR evta/58PTEWzr21hufPjA8N+qlnBwAAAAAD//2JiWLci5v1+HmFXDqcnULE/MxgYGBj+f6CaJQAA AAD//2Ji2FrkY3iYpYC5qDeGgeEMAwPDvwQBBoYvcTwOVLMEAAAA//9isDBgkP///0EOg9z35v// Gc/eeW7BwPj5+QGZhANUswMAAAD//2JgqGBgYGBgqEMXlvhMPUsAAAAA//8iYDd1AAAAAP//AwDR w7IkEbzhVQAAAABJRU5ErkJggg== ''')))) return f if __name__ == "__main__": # create font data chunk for embedding import base64, os, sys output = open(sys.argv[1], "wb") font = b"../Images/courB08" output.write(b" f._load_pilfont_data(\n") output.write(b" # " + os.path.basename(font) + b"\n") output.write(b" BytesIO(base64.decodestring(b'''\n") base64.encode(open(font + b".pil", "rb"), output) output.write(b"''')), Image.open(BytesIO(base64.decodestring(b'''\n") base64.encode(open(font + b".pbm", "rb"), output) output.write(b"'''))))") output.close()
def create_file(repo, msg, new_file, branch, data): print(msg, new_file, branch) repo.create_file(message=msg, content=base64.encode(msg), path=new_file, branch=branch)
def makeSecret(password): salt = os.urandom(4) h = hashlib.sha1(password) h.update(salt) return "{SSHA}" + encode(h.digest() + salt)[:-1]
def hash_pw(password, saltf=get_salt): salt = saltf() h = hashlib.sha1(password) h.update(salt) return "{SSHA}" + encode(h.digest() + salt).rstrip()
def post(self): """ Change password for a given user --- tags: - User Management parameters: - in: body name: body schema: required: - user - password properties: user: type: string description: SOCA user password: type: string description: New password to configure responses: 200: description: Pair of username/token is valid 203: description: Invalid username/token pair 400: description: Malformed client input """ parser = reqparse.RequestParser() parser.add_argument("user", type=str, location="form") parser.add_argument("password", type=str, location="form") args = parser.parse_args() user = args["user"] password = args["password"] if user is None or password is None: return errors.all_errors( "CLIENT_MISSING_PARAMETER", "user (str) and password (str) parameters are required") dn_user = "******" + user + ",ou=people," + config.Config.LDAP_BASE_DN enc_passwd = bytes(password, 'utf-8') salt = os.urandom(16) sha = hashlib.sha1(enc_passwd) sha.update(salt) digest = sha.digest() b64_envelop = encode(digest + salt) passwd = '{{SSHA}}{}'.format(b64_envelop.decode('utf-8')) new_value = passwd try: conn = ldap.initialize('ldap://' + config.Config.LDAP_HOST) conn.simple_bind_s(config.Config.ROOT_DN, config.Config.ROOT_PW) mod_attrs = [(ldap.MOD_REPLACE, "userPassword", new_value.encode('utf-8'))] conn.modify_s(dn_user, mod_attrs) return { "success": True, "message": "Password updated correctly." }, 200 except Exception as err: return errors.all_errors(type(err).__name__, err)
def populate_foxml_datastream(foxml, pid, datastream, base_url='http://localhost:8080/fedora', archival=False, inline_to_managed=False, cursor=None): """ Add a FOXML datastream into an lxml etree. """ datastream_attributes = { 'ID': datastream['dsid'], 'STATE': datastream['state'], 'CONTROL_GROUP': datastream['control_group'], 'VERSIONABLE': str(datastream['versioned']).lower(), } with foxml.element('{{{0}}}datastream'.format(FOXML_NAMESPACE), datastream_attributes): versions = list(datastream_reader.old_datastreams(datastream['id'])) versions.append(datastream) for index, version in enumerate(versions): datastream_reader.resource(version['resource'], cursor=cursor) resource_info = cursor.fetchone() datastream_reader.mime(resource_info['mime'], cursor=cursor) mime_info = cursor.fetchone() try: created = format_date(version['committed']) except KeyError: created = format_date(datastream['created']) version_attributes = { 'ID': '{}.{}'.format(datastream['dsid'], index), 'LABEL': version['label'] if version['label'] else '', 'CREATED': created, 'MIMETYPE': mime_info['mime'], } if datastream['control_group'] != 'R': size = filestore.uri_size(resource_info['uri']) version_attributes['SIZE'] = str(size) with foxml.element( '{{{0}}}datastreamVersion'.format(FOXML_NAMESPACE), version_attributes): datastream_reader.checksums(version['resource'], cursor=cursor) checksums = cursor.fetchall() for checksum in checksums: foxml.write( etree.Element( '{{{0}}}datastreamDigest'.format(FOXML_NAMESPACE), { 'TYPE': checksum['type'], 'DIGEST': checksum['checksum'] })) if datastream['control_group'] == 'X' and ( not inline_to_managed): content_element = etree.Element( '{{{0}}}xmlContent'.format(FOXML_NAMESPACE)) uri = filestore.resolve_uri(resource_info['uri']) xml_etree = etree.parse(uri) content_element.append(xml_etree.getroot()) foxml.write(content_element) elif datastream['control_group'] in ['M', 'X'] and archival: uri = filestore.resolve_uri(resource_info['uri']) with open(uri, 'rb') as ds_file: with foxml.element('{{{0}}}binaryContent'.format( FOXML_NAMESPACE)): base64.encode(ds_file, foxml) else: if datastream['control_group'] == 'R': content_attributes = { 'TYPE': 'URL', 'REF': resource_info['uri'], } else: content_attributes = { 'TYPE': 'INTERNAL_ID', 'REF': ('{}/objects/{}/datastreams/{}/' 'content?asOfDateTime={}').format( base_url, pid, datastream['dsid'], created), } foxml.write( etree.Element( '{{{0}}}contentLocation'.format(FOXML_NAMESPACE), content_attributes))
def encode(self, out): out.write('<value><base64>\n') base64.encode(StringIO.StringIO(self.data), out) out.write('</base64></value>\n')
#!/home/dacastrom/Documents/django/test2/myvenv/bin/python3 from __future__ import print_function import base64 import os import sys if __name__ == "__main__": # create font data chunk for embedding font = "Tests/images/courB08" print(" f._load_pilfont_data(") print(" # %s" % os.path.basename(font)) print(" BytesIO(base64.decodestring(b'''") base64.encode(open(font + ".pil", "rb"), sys.stdout) print("''')), Image.open(BytesIO(base64.decodestring(b'''") base64.encode(open(font + ".pbm", "rb"), sys.stdout) print("'''))))")
#!/usr/bin/python import base64 base = base64.encode(r'binary\x00strin') print base print base64.decode(base) l = r'i\x7\x1d\xfb\xef\xff' base1 = base64.b64encode(l) #base1 = base64.b64encode(l) print base1 base11 = base64.urlsafe_b64encode(l) print base11 print base64.urlsafe_b64decode(l)