Twisted中的FTP文件服务器

时间:2013-01-01 22:18:39

标签: python macos ftp twisted

我目前正在尝试启动并运行FTP服务器,我甚至无法在Mac OS X上安装Finder进行连接......

Finder错误:

There was a problem connecting to the server “192.168.1.67”.
Check the server name or IP address, and then try again. If you continue to have problems, contact your system administrator.

标准输出

python FTPtoEMAIL.py 
2013-01-01 17:16:01-0500 [-] Log opened.
2013-01-01 17:16:01-0500 [-] FTPFactory starting on 7654
2013-01-01 17:16:01-0500 [-] Starting factory <twisted.protocols.ftp.FTPFactory instance at 0x6aff80>
2013-01-01 17:16:26-0500 [FTP (ProtocolWrapper),0,192.168.1.67] Setting up avatar
2013-01-01 17:16:26-0500 [FTP (ProtocolWrapper),0,192.168.1.67] apple
2013-01-01 17:16:26-0500 [FTP (ProtocolWrapper),1,192.168.1.67] Setting up avatar
2013-01-01 17:16:26-0500 [FTP (ProtocolWrapper),1,192.168.1.67] apple
^C2013-01-01 17:16:35-0500 [-] Received SIGINT, shutting down.
2013-01-01 17:16:35-0500 [twisted.protocols.ftp.FTPFactory] (TCP Port 7654 Closed)
2013-01-01 17:16:35-0500 [twisted.protocols.ftp.FTPFactory] Stopping factory <twisted.protocols.ftp.FTPFactory instance at 0x6aff80>
2013-01-01 17:16:35-0500 [-] Main loop terminated.

代码

import sys
import EMail
from twisted.protocols.ftp import FTPFactory, FTPRealm
from twisted.cred.portal import Portal
from twisted.cred import checkers
from twisted.cred.checkers import AllowAnonymousAccess, FilePasswordDB
from twisted.internet import reactor
from twisted.python import log
from twisted.internet.defer import succeed, failure

from twisted.protocols.ftp import FTPFactory, FTPRealm, FTP, FTPShell, IFTPShell

# EMail Shell
USER = "Apple"
PASSWORD = "Orange"


class EMailtoFTPShell(object):
    """
        An abstraction of the shell commands used by the FTP protocol for
        a given user account.

        All path names must be absolute.
        """

    def __init__(path, avatar):
        print path
        print avatar

        self.root_path = path
        self.avatar = avatar

    def makeDirectory(path):
        """
            Create a directory.

            @param path: The path, as a list of segments, to create
            @type path: C{list} of C{unicode}

            @return: A Deferred which fires when the directory has been
            created, or which fails if the directory cannot be created.
            """
        print path

        path = os.path.join(self.root_path, path)

        os.mkdir(path)

        return succeed(path)

    def removeDirectory(path):
        """
            Remove a directory.

            @param path: The path, as a list of segments, to remove
            @type path: C{list} of C{unicode}

            @return: A Deferred which fires when the directory has been
            removed, or which fails if the directory cannot be removed.
            """
        print path
        path = os.path.join(self.root_path, path)

        os.rremovedir(path)

        return succeed(path)

    def removeFile(path):
        """
            Remove a file.

            @param path: The path, as a list of segments, to remove
            @type path: C{list} of C{unicode}

            @return: A Deferred which fires when the file has been
            removed, or which fails if the file cannot be removed.
            """
        print path

        path = os.path.join(self.root_path, path)

        os.remove(path)

    def rename(fromPath, toPath):
        """
            Rename a file or directory.

            @param fromPath: The current name of the path.
            @type fromPath: C{list} of C{unicode}

            @param toPath: The desired new name of the path.
            @type toPath: C{list} of C{unicode}

            @return: A Deferred which fires when the path has been
            renamed, or which fails if the path cannot be renamed.
            """
        print fromPath
        print toPath

        fromPath = os.path.join(self.root_path, fromPath)
        toPath   = os.path.join(self.root_path, toPath)

        os.rename(fromPath, toPath)

        succeed(toPath)

    def access(path):
        """
            Determine whether access to the given path is allowed.

            @param path: The path, as a list of segments

            @return: A Deferred which fires with None if access is allowed
            or which fails with a specific exception type if access is
            denied.
            """
        print path

        return succeed(None)

    def stat(path, keys=()):
        """
            Retrieve information about the given path.

            This is like list, except it will never return results about
            child paths.
            """
        print path
        print keys

        #todo

    def list(path, keys=()):
        """
            Retrieve information about the given path.

            If the path represents a non-directory, the result list should
            have only one entry with information about that non-directory.
            Otherwise, the result list should have an element for each
            child of the directory.

            @param path: The path, as a list of segments, to list
            @type path: C{list} of C{unicode}

            @param keys: A tuple of keys desired in the resulting
            dictionaries.

            @return: A Deferred which fires with a list of (name, list),
            where the name is the name of the entry as a unicode string
            and each list contains values corresponding to the requested
            keys.  The following are possible elements of keys, and the
            values which should be returned for them:

            - C{'size'}: size in bytes, as an integer (this is kinda required)

            - C{'directory'}: boolean indicating the type of this entry

            - C{'permissions'}: a bitvector (see os.stat(foo).st_mode)

            - C{'hardlinks'}: Number of hard links to this entry

            - C{'modified'}: number of seconds since the epoch since entry was
            modified

            - C{'owner'}: string indicating the user owner of this entry

            - C{'group'}: string indicating the group owner of this entry
            """
        print path
        print keys

        #todo

    def openForReading(path):
        """
            @param path: The path, as a list of segments, to open
            @type path: C{list} of C{unicode}

            @rtype: C{Deferred} which will fire with L{IReadFile}
            """
        print path

        # fetch from the email server
        metadata = EMail.parse_file(os.join(self.root_path, path))

        return fetch_from_mail_server(metadata, "READ")

    def openForWriting(path):
        """
            @param path: The path, as a list of segments, to open
            @type path: C{list} of C{unicode}

            @rtype: C{Deferred} which will fire with L{IWriteFile}
            """
        print path

        # fetch from the email server
        metadata = EMail.parse_file(os.join(self.root_path, path))

        return fetch_from_mail_server(metadata, "WRTE")

class EMailToFTPRealm(object):

    def __init__(self):
        pass

    def requestAvatar(self, avatarId, mind, *interfaces):
        for iface in interfaces:
            if iface is IFTPShell:
                if not avatarId is checkers.ANONYMOUS:
                    print "Setting up avatar"
                    print avatarId
                    avatar = EMailtoFTPShell("/Users/FTP_Server/", avatarId)
                    return IFTPShell, avatar, getattr(avatar, 'logout', lambda: None)
        raise NotImplementedError("Only IFTPShell interface is supported by this realm")


if __name__ == "__main__":

    p = Portal(EMailToFTPRealm(),
           [FilePasswordDB("pass.dat")])

    f = FTPFactory(p)

    log.startLogging(sys.stdout)
    reactor.listenTCP(7654, f)
    reactor.run()

pass.dat

apple:orange

1 个答案:

答案 0 :(得分:1)

在twistd工作的基础上你可以使用Finder连接到FTP服务器,这看起来是OS X问题而不是Twisted问题。

发生写入问题是因为通过Finder建立的FTP连接是只读的(按照Apple Support article

假设您正在使用上面的pass.dat文件,您的计算机的标准登录将无法正常工作,因为它未在该文件中列出!您需要使用pass.dat文件中指定的登录名并且可以访问ftp服务器的根目录。