zoukankan      html  css  js  c++  java
  • Python判断上传文件类型

    在开发上传服务时,经常需要对上传的文件进行过滤。

    本文为大家提供了python通过文件头判断文件类型的方法,非常实用。

    代码如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    import struct 
    # 支持文件类型 
    # 用16进制字符串的目的是可以知道文件头是多少字节 
    # 各种文件头的长度不一样,少半2字符,长则8字符 
    def typeList(): 
      return 
        "52617221": EXT_RAR, 
        "504B0304": EXT_ZIP} 
      
    # 字节码转16进制字符串 
    def bytes2hex(bytes): 
      num = len(bytes) 
      hexstr = u"" 
      for in range(num): 
        = u"%x" % bytes[i] 
        if len(t) % 2
          hexstr += u"0" 
        hexstr += 
      return hexstr.upper() 
      
    # 获取文件类型 
    def filetype(filename): 
      binfile = open(filename, 'rb'# 必需二制字读取 
      tl = typeList() 
      ftype = 'unknown' 
      for hcode in tl.keys(): 
        numOfBytes = len(hcode) / 2 # 需要读多少字节 
        binfile.seek(0# 每次读取都要回到文件头,不然会一直往后读取 
        hbytes = struct.unpack_from("B"*numOfBytes, binfile.read(numOfBytes)) # 一个 "B"表示一个字节 
        f_hcode = bytes2hex(hbytes) 
        if f_hcode == hcode: 
          ftype = tl[hcode] 
          break
      #不要忘记关闭打开的文件,避免出现异常
      binfile.close() 
      return ftype
      
    if __name__ == '__main__'
      print filetype('pythontab.jpg')

    常见文件格式的文件头

    文件格式 文件头(十六进制)

    JPEG (jpg) FFD8FF

    PNG (png) 89504E47

    GIF (gif) 47494638

    TIFF (tif) 49492A00

    Windows Bitmap (bmp) 424D

    CAD (dwg) 41433130

    Adobe Photoshop (psd) 38425053

    Rich Text Format (rtf) 7B5C727466

    XML (xml) 3C3F786D6C

    HTML (html) 68746D6C3E

    Email [thorough only] (eml) 44656C69766572792D646174653A

    Outlook Express (dbx) CFAD12FEC5FD746F

    Outlook (pst) 2142444E

    MS Word/Excel (xls.or.doc) D0CF11E0

    MS Access (mdb) 5374616E64617264204A

  • 相关阅读:
    Extensions in UWP Community Toolkit
    Rust 算法排位记
    【华为云技术分享】mongos-sharding连接池配置
    【华为云技术分享】opensuse使用zypper安装软件
    MongoDB经典故障系列一:数据库频繁启动失败怎么办?
    刚刚,华为云找到了更安全更卫生的乘坐电梯方式
    攻防学习
    通过FTP无法删除文件
    photoswipe-3.0.5 手机端横屏后竖屏图片无法归位问题解决
    【转载】apache配置虚拟主机以及虚拟目录详解
  • 原文地址:https://www.cnblogs.com/chenjingyi/p/5741854.html
Copyright © 2011-2022 走看看