pickle无法序列化ftplib.FTP_TLS实例

时间:2019-09-23 14:57:08

标签: python python-2.7 ftp pickle dill

我的代码如下所示,只需要进行pickle.dump一个ftp实例,却遇到了TypeError: an integer is required错误。

#! /usr/bin/env python
# -*- coding: utf-8 -*-

import pickle
from ftplib import FTP_TLS


class FtpClient(object):
    """
    Create sftp instance;then implemnet all sftp methods
    """

    def __init__(self):
        self.ftps = FTP_TLS()


if __name__ == '__main__':
    ftps = FtpClient()
    pickle_ftps = pickle.dumps(ftps)
    print("pickle_ftps:", pickle_ftps)

跟踪显示如下:

Traceback (most recent call last):
  File "/Users/jacksonshawn/PythonCodes/pythonlearning/python_learning_ftp_pickle_issue.py", line 19, in <module>
    pickle_ftps = pickle.dumps(ftps)
  File "/Users/jacksonshawn/anaconda2/lib/python2.7/pickle.py", line 1380, in dumps
    Pickler(file, protocol).dump(obj)
  File "/Users/jacksonshawn/anaconda2/lib/python2.7/pickle.py", line 224, in dump
    self.save(obj)
  File "/Users/jacksonshawn/anaconda2/lib/python2.7/pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)
  File "/Users/jacksonshawn/anaconda2/lib/python2.7/pickle.py", line 425, in save_reduce
    save(state)
  File "/Users/jacksonshawn/anaconda2/lib/python2.7/pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "/Users/jacksonshawn/anaconda2/lib/python2.7/pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())
  File "/Users/jacksonshawn/anaconda2/lib/python2.7/pickle.py", line 669, in _batch_setitems
    save(v)
  File "/Users/jacksonshawn/anaconda2/lib/python2.7/pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "/Users/jacksonshawn/anaconda2/lib/python2.7/pickle.py", line 731, in save_inst
    save(stuff)
  File "/Users/jacksonshawn/anaconda2/lib/python2.7/pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "/Users/jacksonshawn/anaconda2/lib/python2.7/pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())
  File "/Users/jacksonshawn/anaconda2/lib/python2.7/pickle.py", line 669, in _batch_setitems
    save(v)
  File "/Users/jacksonshawn/anaconda2/lib/python2.7/pickle.py", line 306, in save
    rv = reduce(self.proto)
  File "/Users/jacksonshawn/anaconda2/lib/python2.7/copy_reg.py", line 71, in _reduce_ex
    state = base(self)
TypeError: an integer is required
[Finished in 0.2s with exit code 1]

有人可以提供一些建议吗?我已经在互联网上搜索了答案,似乎dill模块可以解决此问题,但是该模块无法在我的系统中使用,有人知道根本原因吗?

0 个答案:

没有答案