当前位置: 首页>>代码示例>>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;未经允许,请勿转载。