为什么我的代码会引发异常?

时间:2012-03-08 23:50:26

标签: python

您好我正在尝试创建一个FTP服务器并帮助开发我正在使用pyftpdlib。我想要做的是在用户下载特定文件时执行一些文件操作,但有时会引发异常而我真的不知道原因。

在本教程之后,我在pyftpdlib中编写了自己的处理程序:http://code.google.com/p/pyftpdlib/wiki/Tutorial#3.8_-_Event_callbacks

但是当用户下载日志文件(我打算对其进行一些文件操作)时,有时会出现严重错误,而我真的不明白为什么。我有另一个类,它基本上从配置文件中读取,错误消息说它找不到FTP部分。但它很奇怪,因为我在配置文件中清楚地知道它并且它有时完美地工作。

可能会出现此错误,因为我有两个“连接”对象?这是我唯一的猜测,所以如果有人能解释出了什么问题,我会很高兴。这是我的代码困扰(没关注file.name检查,因为最近添加了它):

class ArchiveHandler(ftpserver.FTPHandler):

def on_login(self, username):
    # do something when user login
    pass

def on_logout(self, username):
    # do something when user logs out
    pass


def on_file_sent(self, file):
    "What to do when retrieved the file the class is watching over"
    attr = Connection()
    if attr.getarchive() == 'true':
        t = datetime.now()
    if file.name == "log.log":

            try:
                shutil.copy2(file, attr.getdir() + ".archive/" + str(t.strftime("%Y-%m-%d_%H:%M:%S") + '.log'))
            except OSError:
                print 'Could not copy file'
                raise

        if attr.getremain() == 'false':
                try:
                    os.remove(file)
                except OSError:
                    print 'Could not remove file'
                    raise

完整来源: http://pastie.org/3552079

配置文件的来源: http://pastie.org/3552085

编辑 - > (当然还有错误):

[root]@85.230.122.159:40659 unhandled exception in instance <pyftpdlib.ftpserver.DTPHandler object at 0xb75f49ec>
Traceback (most recent call last):
  File "/usr/lib/python2.6/asyncore.py", line 84, in write
    obj.handle_write_event()
  File "/usr/lib/python2.6/asyncore.py", line 435, in handle_write_event
    self.handle_write()
  File "/usr/lib/python2.6/asynchat.py", line 174, in handle_write
    self.initiate_send()
  File "/usr/lib/python2.6/asynchat.py", line 215, in initiate_send
    self.handle_close()
  File "/usr/local/lib/python2.6/dist-packages/pyftpdlib/ftpserver.py", line 1232, in handle_close
    self.close()
  File "/usr/local/lib/python2.6/dist-packages/pyftpdlib/ftpserver.py", line 1261, in close
    self.cmd_channel.on_file_sent(filename)
  File "ftp.py", line 87, in on_file_sent
  File "ftp.py", line 12, in __init__
  File "/usr/lib/python2.6/ConfigParser.py", line 311, in get
    raise NoSectionError(section)
NoSectionError: No section: 'FTP Section'

2 个答案:

答案 0 :(得分:3)

问题在于您未包含的部分。它说

  File "ftp.py", line 12, in __init__
  File "/usr/lib/python2.6/ConfigParser.py", line 311, in get
    raise NoSectionError(section)
NoSectionError: No section: 'FTP Section'

所以从第一行开始,我们知道ftp.py第12行的问题是什么(因为下面的所有内容都不是我们的代码,所以我们假设它是正确的。)

第12行是这样的:

self.ip = config.get('FTP Section', 'hostname')

错误消息显示“没有部分:'FTP部分'”。

由此我们可以假设配置文件中存在错误(它没有“FTP部分”)。

您是否指向正确的配置文件?它是否在您运行脚本的同一目录中?与脚本位于同一文件夹中的工作,它必须是您从运行脚本的文件夹。

我认为这是你遇到的问题,因为根据the documentation

  

如果不存在任何命名文件,则ConfigParser实例将包含空数据集。

您可以尝试open文件来确认。

答案 1 :(得分:0)

在这种情况下的问题是,通过读取文件,它将文件保持打开状态。

我改为这个并且工作得更好:

config = ConfigParser.RawConfigParser()
    fp = open('config.cfg')
        config.readfp(fp)

然后当我读完构造函数时,我添加:

#Close the file
fp.close()

瞧,你可以打开你想要的课程中有多少个对象,它不会显示任何错误。 :)