Ftprealm Interpretation of twisted

Source: Internet
Author: User

First, look at my test code.

From twisted.protocols.ftp import ftpfactory, ftprealmfrom twisted.cred.portal import Portalfrom twisted.cred.checkers Import allowanonymousaccess, filepassworddbfrom twisted.internet import reactorfrom twisted.internet import endpoints# Pass.dat the same directory with ftp01.py# =====================# jeff:bozo# grimmtooth:bozo2# ===================== #p = Portal (Ftprealm (anonymousroot= ' e:/', userhome= ' e:/'),           [Allowanonymousaccess (), Filepassworddb ("Pass.dat")]) F = Ftpfactory (P) endpoints.serverfromstring (reactor, ' tcp:21 '). Listen (f) reactor.run ()


The portal class is the Cred authentication class, which is the user login authentication portal, constructs a ftpfactory to provide TCP service through the portal (Ftp,ftp is based on TCP).

Ftprealm is derived from Baseftprealm, Baseftprealm implements the Portal.irealm interface.

Class Baseftprealm: "" "Base class for simple FTP realms which provides a easy hooks for specifying the home dire    Ctory for each user. "" "Implements (portal. Irealm) def __init__ (self, anonymousroot): Self.anonymousroot = filepath. FilePath (Anonymousroot) def gethomedirectory (self, Avatarid): "" "Return a L{filepath} representing the H  ome directory of the given avatar.        Override this in a subclass.        @param avatarid:a User identifier returned from A credentials checker. @type avatarid:c{str} @rtype: L{filepath} "" "Raise Notimplementederror ("%r did not Ride Gethomedirectory "% (self.__class__,)) def requestavatar (self, Avatarid, Mind, *interfaces): for iface in I Nterfaces:if Iface is Iftpshell:if Avatarid is checkers. Anonymous:avatar = Ftpanonymousshell (self.anonymousroot) else:avata r = Ftpshell (selF.gethomedirectory (Avatarid)) return (Iftpshell, Avatar, GetAttr (Avatar, ' logout ', l Ambda:none) Raise Notimplementederror ("Only Iftpshell interface was supported by this realm")

Returns Ftpanonymousshell if an anonymous user requests a login, otherwise returns Ftpshell. Here Ftpbaserealm the user to log in directly to Gethomedirectory () as the FTP user access to the root directory.

Look at Ftprealm, rewrite the gethomedirectory, according to Avatarid to return a subdirectory, it is particularly important to note that the Avatarid is the FTP account login user name. So if you specify a userhome path, you also need to organize a directory with the user name so that the user can access it correctly.

Class Ftprealm (Baseftprealm): ""    "    @type Anonymousroot:l{twisted.python.filepath.filepath}    @ivar Anonymousroot:root of the filesystem to which anonymous        users would be granted access.    @type Userhome:l{filepath. FilePath}    @ivar Userhome:root of the filesystem containing user home directories.    "" " def __init__ (self, anonymousroot, userhome= ' home '):        baseftprealm.__init__ (self, anonymousroot)        Self.userhome = filepath. FilePath (userhome)    def gethomedirectory (self, Avatarid): "" "Use        C{avatarid} as a single path segment to Construct a child of        c{self.userhome} and return this child.        "" Return Self.userHome.child (Avatarid)

Now in turn look at my example in the file system directory, the anonymous user directory and the user directory are e: disk, if the anonymous user access to the e-drive directly, otherwise if the other user access is the e:\{user name} directory. The second parameter of the portal specifies the user's access rights, which can be accessed anonymously, and can be obtained by means of a file password, the format of the file password (by line), User name: password

[Allowanonymousaccess (), Filepassworddb ("Pass.dat")

This enables a simple file server with a few lines of code. You can also do this by implementing the portal. The Irealm interface comes from custom access rights.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Ftprealm Interpretation of twisted

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.