def _repr_html_(self): # External URLs and potentially local files are not embedded into the # notebook output. if not self.embed: url = self.url if self.url is not None else self.filename output = """<video src="{0}" controls> Your browser does not support the <code>video</code> element. </video>""".format(url) return output # Embedded videos are base64-encoded. mimetype = self.mimetype if self.filename is not None: if not mimetype: mimetype, _ = mimetypes.guess_type(self.filename) with open(self.filename, 'rb') as f: video = f.read() else: video = self.data if isinstance(video, unicode_type): # unicode input is already b64-encoded b64_video = video else: b64_video = base64_encode(video).decode('ascii').rstrip() output = """<video controls> <source src="data:{0};base64,{1}" type="{0}"> Your browser does not support the video tag. </video>""".format(mimetype, b64_video) return output
def __init__(self, manager, dispatcher, call_id, from_tag, from_uri, to_tag, to_uri, cseq, user_agent, media_list, is_downstream, is_caller_cseq, mark=0): self.manager = manager self.dispatcher = dispatcher self.session_id = base64_encode( hashlib.md5(call_id).digest()).rstrip('=') self.call_id = call_id self.from_tag = from_tag self.to_tag = None self.mark = mark self.from_uri = from_uri self.to_uri = to_uri self.caller_ua = None self.callee_ua = None self.cseq = None self.previous_cseq = None self.streams = {} self.start_time = None self.end_time = None self.logger = SessionLogger(self) self.logger.info('created: from-tag {0.from_tag})'.format(self)) self.update_media(cseq, to_tag, user_agent, media_list, is_downstream, is_caller_cseq)
def __set__(self, obj, value): """Set a new value in the field. If this is a :py:class:`Base64DecodedValue`, or the model is new and hasn't yet been persisted to the database, the value will be encoded and stored. Otherwise, if it's a standard string value or the model is not new, it's assumed that this is encoded data for storage, and will be stored directly. Args: obj (django.db.models.Model): The model owning the field. value (object): The value being set. This must be a valid string value or :py:class:`Base64DecodedValue`. Raises: Base64TypeError: The type of value provided could not be set. """ if value is not None: if not isinstance(value, (six.text_type, six.binary_type)): raise Base64TypeError(value) pk_val = obj._get_pk_val(obj.__class__._meta) pk_set = pk_val is not None and smart_text(pk_val) != '' if isinstance(value, six.text_type): value = value.encode('utf-8') if isinstance(value, Base64DecodedValue) or not pk_set: value = base64_encode(value) obj.__dict__[self.field.name] = value
def get_prep_value(self, value): """Return a value prepared for the field. Args: value (object): The value to prepare. This is expected to be a string or a :py:class:`Base64DecodedValue`. If the latter, it will be encoded. Returns: bytes: The resulting value. Raises: Base64TypeError: The type of value provided could not be prepared for writing. """ if value is not None: if not isinstance(value, (six.text_type, six.binary_type)): raise Base64TypeError(value) if isinstance(value, Base64DecodedValue): value = base64_encode(value) elif isinstance(value, six.text_type): value = value.encode('utf-8') return value
def get_prep_value(self, value): """Return a value prepared for the field. This prepares the value for use in database operations (saving or querying). It will convert the value into a Unicode Base64-encoded string. Args: value (object): The value to prepare. This is expected to be a string or a :py:class:`Base64DecodedValue`. If the latter, it will be encoded. Returns: unicode: The resulting value. Raises: Base64TypeError: The type of value provided could not be prepared for writing. """ if value is not None: if isinstance(value, Base64DecodedValue): value = base64_encode(value).decode('utf-8') elif isinstance(value, bytes): value = value.decode('utf-8') elif isinstance(value, six.memoryview): value = force_text(bytes(value)) elif not isinstance(value, six.text_type): raise Base64TypeError(value) return value
def gen_sri_hash(source_path: Path) -> str: """Generate SHA-384 subresource integrity hash (SRI). See: https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity for more information about SRIs. """ return base64_encode(sha384( source_path.read_bytes()).digest()).decode("utf-8")
def __init__(self, name, headers=None): self.name = name self.headers = headers or [] try: self.parsed_headers = dict( header.split(': ', 1) for header in self.headers) except Exception: raise CommandError('Could not parse command headers') else: self.__dict__[ 'session_id'] = None if self.call_id is None else base64_encode( hashlib.md5(self.call_id).digest()).rstrip('=')
def get_url (url): request = urllib2.Request(url) request.add_header('Accept-Encoding', 'gzip') if API_AUTH is not None: request.add_header('Authorization', 'Basic %s' % base64_encode(API_AUTH)) response = urllib2.urlopen(request) encoding = str(response.info().get('Content-Encoding')).lower() data = response.read() if encoding == 'gzip': buf = StringIO(data) data = gzip.GzipFile(fileobj=buf).read() elif encoding =='deflate': data = zlib.decompress(data) return data
def __set__(self, obj, value): """Set a new value in the field. If this is a :py:class:`Base64DecodedValue`, or the model is new and hasn't yet been persisted to the database, the value will be encoded and stored. Otherwise, if it's a standard string value or the model is not new, it's assumed that this is encoded data for storage, and will be stored directly. Args: obj (django.db.models.Model): The model owning the field. value (object): The value being set. This must be a valid string value or :py:class:`Base64DecodedValue`. Raises: Base64TypeError: The type of value provided could not be set. """ if value is not None: if not isinstance(value, (bytes, six.memoryview, six.text_type)): raise Base64TypeError(value) pk_val = obj._get_pk_val(obj.__class__._meta) pk_set = pk_val is not None and smart_text(pk_val) != '' if isinstance(value, six.text_type): if value == r'\x': # On Djblets 2.0 with Python 3 on Postgres, empty byte # strings were being stored as the literal 2-byte string # "\x". Check for this and convert back. value = b'' else: value = value.encode('utf-8') elif isinstance(value, six.memoryview): value = bytes(value) if isinstance(value, Base64DecodedValue) or not pk_set: value = base64_encode(value) obj.__dict__[self.field.name] = value
def value_to_string(self, obj): """Return a string representation of the value from a model. The returned value will be a Base64-encoded string value. Args: obj (django.db.models.Model): The model instance owning the field and value. Returns: bytes: The Base64-encoded byte string for the stored value. """ value = self.value_from_object(obj) assert value is None or isinstance(value, Base64DecodedValue) if value is not None: value = base64_encode(value) return value
def get_url (url, cache=True): cache_file = "cache/" + sha1(url).hexdigest() etag_file = cache_file + ".etag" data_file = cache_file + ".json" request = urllib2.Request(url) request.add_header('Accept-Encoding', 'gzip') if API_AUTH is not None: request.add_header('Authorization', 'Basic %s' % base64_encode(API_AUTH)) if cache and os.path.exists(etag_file): with open(etag_file, "r") as f: request.add_header('If-None-Match', '%s' % f.readline()) response = None try: response = urllib2.urlopen(request) except urllib2.HTTPError as e: if e.code == 304: with open(data_file, "r") as f: data = f.read() return data raise encoding = str(response.info().get('Content-Encoding')).lower() data = response.read() if encoding == 'gzip': buf = StringIO(data) data = gzip.GzipFile(fileobj=buf).read() elif encoding =='deflate': data = zlib.decompress(data) if cache: with open(data_file, "w") as f: f.write(data) with open(etag_file, "w") as f: f.write(response.info()["ETag"]) return data
def validate_signature(uri, nonce, signature, auth_token=''): """ Validates requests made by Plivo to your servers. :param uri: Your server URL :param nonce: X-Plivo-Signature-V2-Nonce :param signature: X-Plivo-Signature-V2 header :param auth_token: Plivo Auth token :return: True if the request matches signature, False otherwise """ auth_token = bytes(auth_token.encode('utf-8')) nonce = bytes(nonce.encode('utf-8')) signature = bytes(signature.encode('utf-8')) parsed_uri = urlparse(uri.encode('utf-8')) base_url = urlunparse( (parsed_uri.scheme.decode('utf-8'), parsed_uri.netloc.decode('utf-8'), parsed_uri.path.decode('utf-8'), '', '', '')).encode('utf-8') return base64_encode(hnew(auth_token, base_url + nonce, sha256).digest()).strip() == signature
def set_password(self, service, username, password): """Write the password in the S3 bucket.""" service = _escape_for_s3(service) username = _escape_for_s3(username) pwd_base64 = base64_encode(password.encode('utf-8')).decode() # Save in S3 using both server and client side encryption keyname = self._get_s3_key(service, username) try: self.bucket.Object(keyname).put(ACL='private', Body=pwd_base64, ServerSideEncryption='aws:kms', SSEKMSKeyId=self.kms_key_id) except EndpointConnectionError: if self.use_local_keyring: # Can't connect to S3: fallback to OS keyring print("WARNING: can't connect to S3, storing in OS keyring", file=sys.stderr) keyring.set_password(service, username, password) else: raise
def base64_from_hex(hex_str): """Set 1 - Challenge 1""" return base64_encode(hex_decode(hex_str))
def handle_login_password(session: requests.Session, password: str) -> str: data = session.get(API_PASSPORT_GET_RSA_KEY).json() pub_key = rsa.PublicKey.load_pkcs1_openssl_pem(data['key']) encrypted = rsa.encrypt((data['hash'] + password).encode(), pub_key) return base64_encode(encrypted).decode()
def make_id(s): if isinstance(s, unicode): s = s.encode("utf-8") return base64_encode(s, "-_").replace("=", "")
def encrypt_function(self, s): encrypted = self.encrypt_block(pad(s)) encoded = base64_encode(encrypted) return encoded
def action(cmd_args): parser = OptionParser() (options, args) = parser.parse_args(args=cmd_args) if options.out_filename is None: options.out_filename = "result.fb2" if len(args) > 1: print_err("there must just one PROJECT_FILE") exit(1) project_props, authors, translators, doc_authors, doc_history, genres, book_sequences = project.parse_project_file(args[0]) root = etree.Element("FictionBook", nsmap=NSMAP) desc = append_element(root, "description") title_info = append_element(desc, "title-info") for g in genres: append_element(title_info, "genre", g) for a in authors: append_author_element(title_info, "author", a) append_element(title_info, "book-title", project_props['book-title']) if project_props['annotation-file'] is not None: ann = markup.translate_annotation(project_props['annotation-file']) if len(list(ann)) != 0: title_info.append(ann) append_element_cond(title_info, "date", project_props['date'], {'value': project_props['date']}) cover_image_name = None if project_props['cover-image'] is not None: cover_image_name = project_props['cover-image'] cover = append_element(title_info, "coverpage") coverimage = append_element(cover, "image") coverimage.set("{%s}href" % XLINK_NAMESPACE, "#%s" % base64_encode(cover_image_name)) # set value later append_element_cond(title_info, "lang", project_props['lang']) append_element_cond(title_info, "src-lang", project_props['src-lang']) for t in translators: append_author_element(title_info, "translator", t) for s in book_sequences: append_element(title_info, "sequence", None, attrs=s) # fill document info section doc_info = append_element(desc, "document-info") for a in doc_authors: node = append_element(doc_info, "author") for k in ('first-name', 'middle-name', 'last-name', 'nickname', 'home-page', 'email'): if a[k] is not None: append_element(node, k, a[k]) append_element_cond(doc_info, "program-used", project_props['program-used']) append_element_cond(doc_info, "date", project_props['doc-date'], {'value': project_props['doc-date']}) append_element_cond(doc_info, "src-ocr", project_props['src-ocr']) append_element(doc_info, "id", project_props['book-id']) append_element_cond(doc_info, "version", project_props['book-version']) if len(doc_history) > 0: node = append_element(doc_info, "history") for v,t in doc_history: append_element(node, "p", "%s : %s" % (v, t)) # fill publish-info section publ_info = append_element(desc, "publish-info") append_element_cond(publ_info, "book-name", project_props['book-name']) append_element_cond(publ_info, "publisher", project_props['publisher']) append_element_cond(publ_info, "city", project_props['publish-city']) append_element_cond(publ_info, "year", project_props['publish-year']) append_element_cond(publ_info, "isbn", project_props['publish-isbn']) #body = append_element(root, "body") #append_element(body, "section") body, images, notes_map = markup.translate_body(project_props['content-file']) root.append(body) # prepare book title title = markup.fbe("title") # append authors list authors_list = [format_author(a) for a in authors] title.append(markup.pprocess("p", ", ".join(authors_list))) # append book title title.append(markup.pprocess("p", project_props['book-title'])) body.insert(0, title) body.insert(0, title) # process notes if project_props['notes-file'] is not None: notes_file = project_props['notes-file'] if not os.path.isfile(notes_file): raise markup.InvalidMarkupError("Notes files not found") #notes_body, notes_images = markup.translate_body(notes_file) notes_images, notes_sections = markup.translate_notes(notes_file) # notes_sections - dict, key is note_id all_note_ids = notes_sections.keys() # notes_map.keys() - list of all notes in the text # all_notes - list of all notes id for note_id in notes_map.keys(): if note_id not in all_note_ids: raise markup.InvalidMarkupError("Note id `%s' declared but not defined" % note_id) # form list of notes that should be included into result file rev_notes_map = dict() for k,v in notes_map.iteritems(): if v in rev_notes_map: raise markup.InvalidMarkupError("Each note MUST occur just once!") rev_notes_map[v] = k notes_body = markup.fbe("body") notes_body.set("name", "notes") root.append(notes_body) for k in sorted(rev_notes_map.keys()): note_id = rev_notes_map[k] notes_sections[note_id].sx_title.clear() notes_sections[note_id].sx_title.append(markup.fbe("p", str(k))) notes_body.append(notes_sections[note_id].sx) images = images.union(notes_images) if cover_image_name is not None: images.add(cover_image_name) for img in images: img_path = os.path.join(project_props['images-path'], img) img_id = make_id(img) if not os.path.isfile(img_path): raise markup.InvalidMarkupError("Picture file `%s' not found." % img_path) # encode img file f = open(img_path, "rb") b = base64_encode(f.read()) f.close() img_name_lo = img.lower() content_type = None if img_name_lo.endswith(".jpg"): content_type = "image/jpeg" elif img_name_lo.endswith(".png"): content_type = "image/png" elif img_name_lo.endswith(".gif"): content_type = "image/gif" else: raise markup.InvalidMarkupError("Unknown picture `%s' format." % img) bin = append_element(root, "binary", b, attrs={'id': img_id, 'content-type': content_type}) root.append(bin) #if project_props['cover-image'] is not None and project_props['cover-image'] not in images: # bin = append_element(root, "binary", b, attrs={'id': 'img_%04d' % img_id, 'content-type': content_type}) outf = codecs.open(options.out_filename, mode="w", encoding="utf-8") outf.write('<?xml version="1.0" encoding="utf-8"?>\n') outf.write(etree.tostring(root, pretty_print=True, xml_declaration=False, encoding=unicode)) outf.close()
def get(self): nextUrl = self.get_argument("next", default = SigninHandler.DEFAULT_REDIRECT) # po udanym zalogowaniu nastąpi przekierowanie self.set_cookie(SigninHandler.COOKIE_REDIRECT, base64_encode(json_encode(nextUrl))) self.render("auth/signin.html", url = nextUrl)