为什么这个腌制数据在通过网络传输后没有取消腌制?
logexample.py 使用logging.handlers.DatagramHandler 通过网络进行记录,它会pickles(协议1)它发送的数据。
logserver.py 应该解封并打印到屏幕,但它却引发错误。如果我使用 pickle.loads 那么 KeyError: '\x00' 如果我使用 cPickle.loads 它是一个 EOFError
文件在这里 - http://gist.github.com/542543
Python 版本 2.6.5
为什么会发生这种情况?
--------------------------修复---------------------- -----
对于其他可能感兴趣的人,这里是固定处理程序
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一个 示例 在如何使用 DataGramHandler 的文档中 - 它表明数据报可能通过多个数据包发送,这些数据包需要在接收端重新组装。第一个数据包的前四个字节是长度 - 您将其传递到 pickle.loads 以及 pickle 数据中。请改用示例代码。
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.