如何在Python中序列化scandir.DirEntry以通过网络套接字发送? [英] How to serialize a scandir.DirEntry in Python for sending through a network socket?

查看:157
本文介绍了如何在Python中序列化scandir.DirEntry以通过网络套接字发送?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有通过网络套接字相互通信的服务器程序和客户端程序.

I have server and client programs that communicate with each other through a network socket.

我想要的是通过套接字发送从scandir.scandir()获得的目录条目(scandir.DirEntry).

What I want is to send a directory entry (scandir.DirEntry) obtained from scandir.scandir() through the socket.

目前,我正在使用picklecPickle模块,并提出了以下内容(仅摘录):

For now I am using pickle and cPickle modules and have come up with the following (excerpt only):

import scandir, pickle

s = scandir.scandir("D:\\PYTHON")
entry = s.next()
data = pickle.dumps(entry)

但是,我得到了以下错误堆栈:

However, I am getting the following error stack:

File "untitled.py", line 5, in <module>
  data = pickle.dumps(item)
File "C:\Python27\Lib\pickle.py", line 1374, in dumps
  Pickler(file, protocol).dump(obj)
File "C:\Python27\Lib\pickle.py", line 224, in dump
  self.save(obj)
File "C:\Python27\Lib\pickle.py", line 306, in save
  rv = reduce(self.proto)
File "C:\Python27\Lib\copy_reg.py", line 70, in _reduce_ex
  raise TypeError, "can't pickle %s objects" % base.__name__

TypeError: can't pickle DirEntry objects

如何摆脱这个错误?

我听说过使用marshallJSON. UPDATE :JSON不会转储对象中的所有数据.

I have heard of using marshall or JSON. UPDATE: JSON is not dumping all the data within the object.

是否有完全不同的方法可以通过套接字发送对象?

Is there any completely different way to do so to send the object through the socket?

在此先感谢您的帮助.

推荐答案

是的,os.DirEntry对象是短暂的,并不是真正保留或序列化的.如果您需要对它们中的数据进行序列化,就好像您已经在自己的答案中弄清楚了-序列化(点刺)所需属性的字典版本.

Yes, os.DirEntry objects are intended to be short-lived, not really kept around or serialized. If you need the data in them to be serialized, looks like you've figured that out in your own answer -- serialize (pickle) a dict version of the attributes you need.

要反序列化为像os.DirEntry实例那样行走和鸣叫的对象,请创建一个模仿您所需事物的PseudoDirEntry类.

To deserialize into an object that walks and quacks like an os.DirEntry instance, create a PseudoDirEntry class that mimics the things you need.

请注意,您已经可以直接序列化stat对象,这省去了从中选择字段的麻烦.

Note that you can directly serialize the stat object already, which saves you picking the fields out of that.

结合起来,看起来像这样:

Combined, that would look like this:

class PseudoDirEntry:
    def __init__(self, name, path, is_dir, stat):
        self.name = name
        self.path = path
        self._is_dir = is_dir
        self._stat = stat

    def is_dir(self):
        return self._is_dir

    def stat(self):
        return self._stat

然后:

>>> import os, pickle
>>> entry = list(os.scandir())[0]
>>> pickled = pickle.dumps({'name': entry.name, 'path': entry.path, 'is_dir': entry.is_dir(), 'stat': entry.stat()})
>>> loaded = pickle.loads(pickled)
>>> pseudo = PseudoDirEntry(loaded['name'], loaded['path'], loaded['is_dir'], loaded['stat'])
>>> pseudo.name
'.DS_Store'
>>> pseudo.is_dir()
False
>>> pseudo.stat()
os.stat_result(st_mode=33188, st_ino=8370294, st_dev=16777220, st_nlink=1, st_uid=502, st_gid=20, st_size=8196, st_atime=1478356967, st_mtime=1477601172, st_ctime=1477601172)

这篇关于如何在Python中序列化scandir.DirEntry以通过网络套接字发送?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆