當前位置: 首頁>>代碼示例>>Python>>正文


Python magic.from_buffer方法代碼示例

本文整理匯總了Python中magic.from_buffer方法的典型用法代碼示例。如果您正苦於以下問題:Python magic.from_buffer方法的具體用法?Python magic.from_buffer怎麽用?Python magic.from_buffer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在magic的用法示例。


在下文中一共展示了magic.from_buffer方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: upload_audio

# 需要導入模塊: import magic [as 別名]
# 或者: from magic import from_buffer [as 別名]
def upload_audio(self, file_id, mime):
        if not self.file_store:
            return None, "No file store available"

        filedata = self.download_file(file_id)
        if filedata is None:
            return None, "teleboto Faild to download file"

        if mime is None:
            mime = magic.from_buffer(filedata, mime=True).decode('utf-8')
        ext = mimetypes.guess_extension(mime)
        if ext is None:
            raise Exception("Failed to guess ext from mime: %s" % mime)
        filename = "voice" + ext
        url = self.file_store.upload_file(filedata, filename, filetype="audio")
        if url is None:
            return None, "Failed to upload Document"

        return url, None 
開發者ID:tuna,項目名稱:fishroom,代碼行數:21,代碼來源:telegram.py

示例2: get_filetype

# 需要導入模塊: import magic [as 別名]
# 或者: from magic import from_buffer [as 別名]
def get_filetype(data):
    """There are two versions of python-magic floating around, and annoyingly, the interface 
    changed between versions, so we try one method and if it fails, then we try the other.
    NOTE: you may need to alter the magic_file for your system to point to the magic file."""
    if sys.modules.has_key('magic'):
        try:
            ms = magic.open(magic.MAGIC_NONE) 
            ms.load() 
            return ms.buffer(data)
        except:
            try:
                return magic.from_buffer(data)
            except magic.MagicException:
                magic_custom = magic.Magic(magic_file='C:\windows\system32\magic')
                return magic_custom.from_buffer(data)
    return '' 
開發者ID:omriher,項目名稱:CapTipper,代碼行數:18,代碼來源:pescanner.py

示例3: run

# 需要導入模塊: import magic [as 別名]
# 或者: from magic import from_buffer [as 別名]
def run(self, directory):
    for root, dirs, files in os.walk(directory, followlinks=True):
      for name in files:
        filename = os.path.join(root, name)
        try:
          file_type = magic.from_buffer(open(filename).read(1024))
        except:
          log("Error reading file %s: %s" % (filename, str(sys.exc_info()[1])))
          continue

        if is_executable(file_type):
          md5_hash = md5(open(filename, "rb").read()).hexdigest()
          if not self.is_file_indexed(md5_hash):
            self.do_run(filename, file_type)
          else:
            log("File already indexed %s" % name)

#------------------------------------------------------------------------------- 
開發者ID:joxeankoret,項目名稱:maltindex,代碼行數:20,代碼來源:diaphora_index_batch.py

示例4: _get_filetype

# 需要導入模塊: import magic [as 別名]
# 或者: from magic import from_buffer [as 別名]
def _get_filetype(self, data):
        """Gets filetype, uses libmagic if available.
        @param data: data to be analyzed.
        @return: file type or None.
        """
        if not HAVE_MAGIC:
            return None

        try:
            ms = magic.open(magic.MAGIC_NONE)
            ms.load()
            file_type = ms.buffer(data)
        except:
            try:
                file_type = magic.from_buffer(data)
            except Exception:
                return None
        finally:
            try:
                ms.close()
            except:
                pass

        return file_type 
開發者ID:phdphuc,項目名稱:mac-a-mal-cuckoo,代碼行數:26,代碼來源:static.py

示例5: get_type

# 需要導入模塊: import magic [as 別名]
# 或者: from magic import from_buffer [as 別名]
def get_type(data):
    try:
        ms = magic.open(magic.MAGIC_NONE)
        ms.load()
        file_type = ms.buffer(data)
    except:
        try:
            file_type = magic.from_buffer(data)
        except:
            return ''
    finally:
        try:
            ms.close()
        except:
            pass

    return file_type 
開發者ID:opensourcesec,項目名稱:CIRTKit,代碼行數:19,代碼來源:utils.py

示例6: do_import

# 需要導入模塊: import magic [as 別名]
# 或者: from magic import from_buffer [as 別名]
def do_import(self, results, url):
        response = requests.get(url, proxies=yeti_config.proxy)
        content_type = magic.from_buffer(response.content, mime=True)

        if content_type == "text/html":
            import_html(results, response.content)
            self.save_as_pdf(results, url)
        else:
            target = AttachedFile.from_content(
                StringIO(response.content), url, content_type)
            results.investigation.update(import_document=target)
            try:
                method = ImportMethod.objects.get(acts_on=content_type)
                method.do_import(results, target.filepath)
            except:
                raise ValueError(
                    "unsupported file type: '{}'".format(content_type)) 
開發者ID:yeti-platform,項目名稱:yeti,代碼行數:19,代碼來源:url.py

示例7: extract_comments_from_str

# 需要導入模塊: import magic [as 別名]
# 或者: from magic import from_buffer [as 別名]
def extract_comments_from_str(code, mime=None):
  """Extracts and returns comments from the given source string.

  Args:
    code: String containing code to extract comments from.
    mime: Optional MIME type for code (str). Note some MIME types accepted
      don't comply with RFC2045. If not given, an attempt to deduce the
      MIME type will occur.
  Returns:
    Python list of parsers.common.Comment in the order that they appear in
      the source code.
  Raises:
    UnsupportedError: If code is of an unsupported MIME type.
  """
  if not mime:
    mime = magic.from_buffer(code, mime=True)
    if isinstance(mime, bytes):
      mime = mime.decode('utf-8')
  if mime not in MIME_MAP:
    raise UnsupportedError('Unsupported MIME type %s' % mime)
  try:
    parser = MIME_MAP[mime]
    return parser.extract_comments(code)
  except common.Error as e:
    raise ParseError(str(e)) 
開發者ID:jeanralphaviles,項目名稱:comment_parser,代碼行數:27,代碼來源:comment_parser.py

示例8: iterHashes

# 需要導入模塊: import magic [as 別名]
# 或者: from magic import from_buffer [as 別名]
def iterHashes(self):
		'''
		Iterate over all the files in the archive, and yield 2-tuples
		containing the internal-path and the internal-item-data as a dict
		'''
		items = list(self.getFileList())
		items.sort()
		for item in items:
			if item not in self.hashedFiles:
				fp = self.open(item)
				cont = fp.read()
				ret = hf.getHashDict(item, cont)
				ret['cont'] = cont
				ret['type'] = fix_mime(magic.from_buffer(cont, mime=True))
				self.hashedFiles[item] = ret
			yield item, self.hashedFiles[item] 
開發者ID:fake-name,項目名稱:IntraArchiveDeduplicator,代碼行數:18,代碼來源:pArch.py

示例9: file_type

# 需要導入模塊: import magic [as 別名]
# 或者: from magic import from_buffer [as 別名]
def file_type(buf):
    import magic
    return magic.from_buffer(buf, mime=True) 
開發者ID:fpsw,項目名稱:Servo,代碼行數:5,代碼來源:utils.py

示例10: load_index

# 需要導入模塊: import magic [as 別名]
# 或者: from magic import from_buffer [as 別名]
def load_index(self):
        try:
            resp = self.boto.get_object(Bucket=self.bucket, Key=self.index_path())
            body = resp["Body"].read()
            content_type = magic.from_buffer(body, mime=True)

            if content_type in ("application/json", "text/plain"):
                logger.debug("Detected %s encoding for index", content_type)
                return json.loads(body.decode("utf-8"))

            # the magic/file command reports gzip differently depending on its version
            elif content_type in ("application/x-gzip", "application/gzip"):
                logger.debug("Detected gzip encoding for index")
                body = gzip.decompress(body)
                return json.loads(body.decode("utf-8"))

            elif content_type in ("application/zlib",):
                logger.debug("Detected zlib encoding for index")
                body = zlib.decompress(body)
                return json.loads(body.decode("utf-8"))

            # Older versions of Ubuntu and some versions of MAC cannot
            # do not detect the file type correctly. In this case we need
            # to try both gzip and zlib decompression
            elif content_type in ("application/octet-stream",):
                logger.debug("Cannot detect encoding for index - trying all")
                body = utils.try_decompress(body)
                return json.loads(body.decode("utf-8"))

            elif content_type == "application/x-empty":
                return {}
            else:
                raise ValueError("Unknown content type for index", content_type)
        except (ClientError):
            return {} 
開發者ID:MichaelAquilina,項目名稱:S4,代碼行數:37,代碼來源:s3.py

示例11: _get_file_magic_name

# 需要導入模塊: import magic [as 別名]
# 或者: from magic import from_buffer [as 別名]
def _get_file_magic_name(self, buffer):
        """
        Return the filetype guessed for a buffer
        :param buffer: bytes
        :return: str of filetype
        """
        default = "Unknown"
        ftype = None

        try:
            # Magic is optional
            import magic
        except ImportError:
            return default

        try:
            # There are several implementations of magic,
            # unfortunately all called magic
            # We use this one: https://github.com/ahupp/python-magic/
            getattr(magic, "MagicException")
        except AttributeError:
            # Looks like no magic was installed
            return default

        try:
            ftype = magic.from_buffer(buffer[:1024])
        except magic.MagicError as e:
            log.exception("Error getting the magic type!")
            return default

        if not ftype:
            return default
        else:
            return self._patch_magic(buffer, ftype) 
開發者ID:appknox,項目名稱:pyaxmlparser,代碼行數:36,代碼來源:core.py

示例12: is_packet_capture

# 需要導入模塊: import magic [as 別名]
# 或者: from magic import from_buffer [as 別名]
def is_packet_capture(bytes):
    """
    :param bytes: raw bytes
    :return: True is valid pcap or pcapng file
    """
    result = from_buffer(bytes)
    valid = "pcap-ng" in result or "tcpdump" in result or "NetMon" in result or 'pcap capture file' in result
    return valid 
開發者ID:PacketTotal,項目名稱:HoneyBot,代碼行數:10,代碼來源:utils.py

示例13: sync_avatar_from_ldap

# 需要導入模塊: import magic [as 別名]
# 或者: from magic import from_buffer [as 別名]
def sync_avatar_from_ldap(self, user: UserProfile, ldap_user: _LDAPUser) -> None:
        if 'avatar' in settings.AUTH_LDAP_USER_ATTR_MAP:
            # We do local imports here to avoid import loops
            from io import BytesIO

            from zerver.lib.actions import do_change_avatar_fields
            from zerver.lib.upload import upload_avatar_image

            avatar_attr_name = settings.AUTH_LDAP_USER_ATTR_MAP['avatar']
            if avatar_attr_name not in ldap_user.attrs:  # nocoverage
                # If this specific user doesn't have e.g. a
                # thumbnailPhoto set in LDAP, just skip that user.
                return

            ldap_avatar = ldap_user.attrs[avatar_attr_name][0]

            avatar_changed = is_avatar_new(ldap_avatar, user)
            if not avatar_changed:
                # Don't do work to replace the avatar with itself.
                return

            io = BytesIO(ldap_avatar)
            # Structurally, to make the S3 backend happy, we need to
            # provide a Content-Type; since that isn't specified in
            # any metadata, we auto-detect it.
            content_type = magic.from_buffer(copy.deepcopy(io).read()[0:1024], mime=True)
            if content_type.startswith("image/"):
                upload_avatar_image(io, user, user, content_type=content_type)
                do_change_avatar_fields(user, UserProfile.AVATAR_FROM_USER)
                # Update avatar hash.
                user.avatar_hash = user_avatar_content_hash(ldap_avatar)
                user.save(update_fields=["avatar_hash"])
            else:
                logging.warning("Could not parse %s field for user %s",
                                avatar_attr_name, user.id) 
開發者ID:zulip,項目名稱:zulip,代碼行數:37,代碼來源:backends.py

示例14: __init__

# 需要導入模塊: import magic [as 別名]
# 或者: from magic import from_buffer [as 別名]
def __init__(self):
        for key in self.magicsignatures.keys():
            self.magicsignatures[key] = re.compile(self.magicsignatures[key])
        for key in self.signatures.keys():
            self.signatures[key] = re.compile(self.signatures[key])
        try:
            import magic
            self.getfmime = magic.from_file
            self.getbmime = magic.from_buffer
        except:
            print "python-magic is not installed (or wrong version)"
            self.gotmagic = False 
開發者ID:georgenicolaou,項目名稱:nfi,代碼行數:14,代碼來源:MimeGuesser.py

示例15: save_file

# 需要導入模塊: import magic [as 別名]
# 或者: from magic import from_buffer [as 別名]
def save_file(uploaded_file, filename=None):
    value = "FILE:{}".format(stream_sha256(uploaded_file))
    mime_type = magic.from_buffer(uploaded_file.read(100), mime=True)
    uploaded_file.seek(0)
    body = AttachedFile.from_upload(uploaded_file, force_mime=mime_type)
    f = observables.File.get_or_create(
        value=value, body=body, mime_type=mime_type)

    if not filename:
        filename = uploaded_file.filename
    if filename not in f.filenames:
        f.filenames.append(filename)

    return f.save() 
開發者ID:yeti-platform,項目名稱:yeti,代碼行數:16,代碼來源:file.py


注:本文中的magic.from_buffer方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。