Python: Check if uploaded file is jpg

前端 未结 5 1164
-上瘾入骨i
-上瘾入骨i 2020-12-05 03:20

How can I check if a file uploaded by a user is a real jpg file in Python (Google App Engine)?

This is how far I got by now:

Script receives image via HTML F

5条回答
  •  悲&欢浪女
    2020-12-05 03:53

    A more general solution is to use the Python binding to the Unix "file" command. For this, install the package python-magic. Example:

    import magic
    
    ms = magic.open(magic.MAGIC_NONE)
    ms.load()
    type =  ms.file("/path/to/some/file")
    print type
    
    f = file("/path/to/some/file", "r")
    buffer = f.read(4096)
    f.close()
    
    type = ms.buffer(buffer)
    print type
    
    ms.close()
    

提交回复
热议问题