Python to look for hexadecimal patterns in a file

It is quite easy to look for hexadecimal patterns in a file in Python, for example match for the magic numbers for JPG or PNG:

import re

with open("~/file", "rb") as f:
  pattern =
b'(\xFF\xD8\xFF\xE0|\xFF\xD8\xFF\xE1|\xFF\xD8\xFF\xDB|\x89\x50\x4E\x47\x0D\x0A\x1A\x0A)'
  regex = re.compile(pattern)
  for match in regex.finditer(f.read()):
    print "%s" % (match.start())

This should print all the positions in the file where the given hexadecimal patterns were seen.