Checking file header, signature and type
Question: I am making a file type checking program (with file signature checking) but,
there are so many file signatures, so I can't compare object file with file
signature.

Is there any library checking file signatures, or how can I easily make this
script? How can I make one function checking file?

  * <http://forensic-proof.com/archives/300>
  * [Python: Check if uploaded file is jpg](http://stackoverflow.com/questions/266648/python-check-if-uploaded-file-is-jpg)

Code:

    
    
    __author__ = 'jun'
    
    # -*- coding: utf-8 -*-
    
    
    import sys
    
    if len(sys.argv) is 1:
        print(sys.stderr, '읽을 파일명을 입력해주세요')
        exit(1)
    
    try:
        IN = open(sys.argv[1], 'rb')
    except IOError:
        print(sys.stderr, '그런 파일이 없거나, 열기에러입니다.')
        exit(1)
    
    offset = 0
    
    def is_jpg(filename):
        data = IN.read(11)
        if data[:4] != '\xff\xd8\xff\xe0': return False
        if data[6:] != 'JFIF\0': return False
        return True
    
    
    if is_jpg(IN) is True:
        print ('jpg file')
    


Answer: What you're trying to do is called detecting the mime type.

[This library](https://github.com/ahupp/python-magic) can do what you want.

