def guess_mimetype(bin_data, default='application/octet-stream'):
 for entry in _mime_mappings:
  for signature in entry.signatures:
   if bin_data.startswith(signature):
    for discriminant in entry.discriminants:
     try:
      guess = discriminant(bin_data)
      if guess:
       return guess
     except Exception:
      _logger.getChild('guess_mimetype').warn("Sub-checker '%s' of type '%s' failed", discriminant.__name__, entry.mimetype, exc_info=True)
    return entry.mimetype
 return default

def guess_mimetype ( bin_data , default = 'application/octet-stream' ) : for entry in _mime_mappings : for signature in entry . signatures : if bin_data . startswith ( signature ) : for discriminant in entry . discriminants : try : guess = discriminant ( bin_data ) if guess : return guess except Exception : _logger . getChild ( 'guess_mimetype' ) . warn ( "Sub-checker '%s' of type '%s' failed" , discriminant . __name__ , entry . mimetype , exc_info = True ) return entry . mimetype return default
guess the mimetype of an uploaded file .