Fixed Python3 encoding errors

My last fix wasn't quite right and caused more issues. This change uses the
UTF-8 codec to encode/write headers and bodies.