logexample.py logs over the network using logging.handlers.DatagramHandler, which pickles(protocol 1) the data it sends.
logserver.py is supposed to unpickle and print to screen, but instead it raises an error. If I use pickle.loads then KeyError: ‘\x00’ and if I use cPickle.loads its an EOFError
The files are here – http://gist.github.com/542543
Python version 2.6.5
Why is this happening?
————————–THE FIX—————————
For anyone else who might be interested, here is the fixed handler
class LogHandler(SocketServer.BaseRequestHandler):
def handle(self):
data = self.request[0]
socket = self.request[1]
out = pickle.loads(data[4:])
record = logging.makeLogRecord(out)
print record.msg
There is an example in the docs of how to use DataGramHandler – it shows that the datagram may be sent over multiple packets, which need to be reassembled at the receiving end. The first four bytes of the first packet are the length – you are passing this into pickle.loads as well as the pickled data. Use the example code instead.