Update middleware.py

With python3 the code raises an exception:

RuntimeError: dictionary changed size during iteration