ahupp/python-magic 源码下载与部署教程

项目概述

A python wrapper for libmagic

项目地址

https://github.com/ahupp/python-magic

项目页面预览

ahupp/python-magic preview

关键指标

  • Stars:2874
  • 主要语言:Python
  • License:Other
  • 最近更新:2025-12-02T23:45:42Z
  • 默认分支:master

本站高速下载(国内可用)

点击下载(本站镜像)
– SHA256:f3c73e88d1e930abcc382d1075b6516224b46cab40fa0d4de5ae18adac1f7e91

安装部署要点(README 精选)

Usage

>>> import magic
>>> magic.from_file("testdata/test.pdf")
'PDF document, version 1.2'
# recommend using at least the first 2048 bytes, as less can produce incorrect identification
>>> magic.from_buffer(open("testdata/test.pdf", "rb").read(2048))
'PDF document, version 1.2'
>>> magic.from_file("testdata/test.pdf", mime=True)
'application/pdf'

There is also a Magic class that provides more direct control,
including overriding the magic database file and turning on character
encoding detection. This is not recommended for general use. In
particular, it’s not safe for sharing across multiple threads and
will fail throw if this is attempted.

>>> f = magic.Magic(uncompress=True)
>>> f.from_file('testdata/test.gz')
'ASCII text (gzip compressed data, was "test", last modified: Sat Jun 28
21:32:52 2008, from Unix)'

You can also combine the flag options:

>>> f = magic.Magic(mime=True, uncompress=True)
>>> f.from_file('testdata/test.gz')
'text/plain'

Installation

The current stable version of python-magic is available on PyPI and
can be installed by running:

pip install python-magic

Other sources:

  • PyPI: http://pypi.python.org/pypi/python-magic/
  • GitHub: https://github.com/ahupp/python-magic

This module is a simple wrapper around the libmagic C library, and
that must be installed as well:

Running the tests

We use the tox test runner which can be installed with python -m pip install tox.

To run tests locally across all available python versions:

python -m tox

Or to run just against a single version:

python -m tox py

To run the tests across a variety of linux distributions (depends on Docker):

./test/run_all_docker_test.sh

常用命令(从 README 提取)

pip install python-magic

sudo apt-get install libmagic1

python -m tox

通用部署说明

  1. 下载源码并阅读 README
  2. 安装依赖(pip/npm/yarn 等)
  3. 配置环境变量(API Key、模型路径、数据库等)
  4. 启动服务并测试访问
  5. 上线建议:Nginx 反代 + HTTPS + 进程守护(systemd / pm2)

免责声明与版权说明

本文仅做开源项目整理与教程索引,源码版权归原作者所有,请遵循对应 License 合规使用。

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容