Quick code credited to Philip Whitfield "https://github.com/underdoeg/". I've only made a few modifications on the way the captured bits are processed.
There is an improved version of underdoeg's one here: "https://gist.github.com/iilxy/f8b95cd039759b18edcdfe60684a6b8f"
Check example.py to see how it works.