[Twisted-Python] SFTP Server sample

Osborn Chan ychan at quova.com
Mon Aug 22 17:10:48 MDT 2011


Hi Michal,

Thanks a lot!
It is a very nice working example, and it is good for newbie as well.

Regards,

Osborn

From: Michal Mach <michal.mach at gmail.com<mailto:michal.mach at gmail.com>>
Reply-To: Twisted general discussion <twisted-python at twistedmatrix.com<mailto:twisted-python at twistedmatrix.com>>
Date: Tue, 23 Aug 2011 00:31:00 +0200
To: Twisted general discussion <twisted-python at twistedmatrix.com<mailto:twisted-python at twistedmatrix.com>>
Subject: Re: [Twisted-Python] SFTP Server sample

Not sure what you mean, but here is a basic example:

from zope.interface import implements

from twisted.application import service, internet
from twisted.conch.ssh.keys import Key
from twisted.conch.ssh.factory import SSHFactory
from twisted.conch.unix import UnixSSHRealm
from twisted.cred.checkers import ICredentialsChecker
from twisted.cred.credentials import IUsernamePassword
from twisted.cred.portal import Portal


def get_key(path):
    return Key.fromString(data=open(path).read())


class DummyChecker(object):
    credentialInterfaces = (IUsernamePassword,)
    implements(ICredentialsChecker)

    def requestAvatarId(self, credentials):
        return credentials.username


def makeService():
    public_key = get_key('id_rsa.pub')
    private_key = get_key('id_rsa')

    factory = SSHFactory()
    factory.privateKeys = {'ssh-rsa': private_key}
    factory.publicKeys = {'ssh-rsa': public_key}
    factory.portal = Portal(UnixSSHRealm())
    factory.portal.registerChecker(DummyChecker())

    return internet.TCPServer(2200, factory)


application = service.Application("sftp server")
sftp_server = makeService()
sftp_server.setServiceParent(application)


Put this content into a file like sftp.tac
The keys can be generated by ckeygen utility which is part of Twisted (e.g. ckeygen -b 2048 -t rsa -f id_rsa)
and run with twistd -ny sftp.tac

If you want more background on Twisted conch i suggest the "Twisted Conch in 60 Seconds" series by
Jean-Paul Calderone on his blog http://as.ynchrono.us/

Michal Mach


On Mon, Aug 22, 2011 at 10:31 PM, Osborn Chan <ychan at quova.com<mailto:ychan at quova.com>> wrote:
Yes. It is SFTP, and I found that filetransfer.FileTransferServer is a
subsystem under UnixConchUser.

       self.subsystemLookup.update({"sftp":
filetransfer.FileTransferServer})

However, I was able to start a ssh shell by using SSHFactory and
UnixSSHReam, but I was not able to start SFTP as sub system.

Do I need to do anything explicitly to start sftp subsystem?

Thanks,

-Osborn
newbie in Python and Twisted.


On 8/22/11 12:50 PM, "Jasper St. Pierre" <jstpierre at mecheye.net<mailto:jstpierre at mecheye.net>> wrote:

>What about twisted.conch.ssh.filetransfer? Is that not SFTP?
>
>On Mon, Aug 22, 2011 at 3:36 PM, Drew Smathers <drew.smathers at gmail.com<mailto:drew.smathers at gmail.com>>
>wrote:
>> On Fri, Aug 19, 2011 at 2:43 PM, Osborn Chan <ychan at quova.com<mailto:ychan at quova.com>> wrote:
>>> Hi,
>>>
>>> I am a newbie in twisted and python.
>>> I would like to create a python SFTP server with twisted, but I cannot
>>>find any sample.
>>>
>>> Can someone give me some advise?
>>>
>>
>> I don't believe there are any examples of building an SFTP server
>> aimed at newcomers to twisted and python but there are existing
>> implementations in the wild--which might honestly be difficult to
>> digest if you don't know python well enough. I think zope at one time
>> has SFTP server based on twisted (which I can't seem to summon with
>> google) and there's also SFTP server implementation in tahoe-lafs
>>
>>(https://github.com/warner/tahoe-lafs/blob/master/src/allmydata/frontends
>>/sftpd.py).
>>
>> -Drew
>>
>> _______________________________________________
>> Twisted-Python mailing list
>> Twisted-Python at twistedmatrix.com<mailto:Twisted-Python at twistedmatrix.com>
>> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>>
>
>
>
>--
>  Jasper
>
>_______________________________________________
>Twisted-Python mailing list
>Twisted-Python at twistedmatrix.com<mailto:Twisted-Python at twistedmatrix.com>
>http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python


_______________________________________________
Twisted-Python mailing list
Twisted-Python at twistedmatrix.com<mailto:Twisted-Python at twistedmatrix.com>
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

_______________________________________________ Twisted-Python mailing list Twisted-Python at twistedmatrix.com<mailto:Twisted-Python at twistedmatrix.com> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python




More information about the Twisted-Python mailing list