Design Build Test Repeat

Voluntarily cast upon the waves of outrageous fortune, this lowly geek flops from peak to peak of the technological ocean. Linux, Windows, C, Ruby, Python and embedded systems all attempt to pull our stalwart hero down. Will he survive alone or will the lifeboat of corporate servitude be too tempting.

Python FTP Server using Turbogears as an “Authorizer”

Posted by Diarmuid on November 6, 2007

I need a Python based FTP server for my Turbogears project and was trilled to find the pyftpdlib project. It allows the creation of an ftp server in less than 10 lines of code. I just posted the following to their discussion board as a thanks for all the help those guys have given me.

This is my contribution to the excellent pyftpdlib project. I hope it in some way repays my debt to the project. I am cross posting this to my blog.

I needed an FTP server that would piggy back off my turbogears installation. The system allows an admin user to easily create new Users and for those Users then to upload files to a server using an FTP client.

I have User Object in my Model with a validate_password method. There is no encryption in this example but you would obviously create an MD5 hash and store that in the database.

class Model(SQLObject):
creation_date = DateTimeCol(
description = UnicodeCol()
displayname = UnicodeCol()
uploadname = UnicodeCol()
uploadpassword = UnicodeCol()
archived = BoolCol(default=False)
person = ForeignKey(‘Person’)
idx_owner = DatabaseIndex(‘person’)

def to_dict(self):
return{‘id’:, ‘description’: self.description}

def validate_password(self, password):
return self.uploadpassword == password

The main server code is as follows. Pardon any mistakes and any advice would be welcome. One thing is that the home directory is created on first use.

This server uses the Turbogears whatwhat model for authentication.
The Authorizer used checks a user’s
existence and validates the password.
The working directory is based on a default directory and the
username, which is checked for uniqueness when it is created.
The working directory is created if necessary.

The user has by default write rights as well as read for directory
but not for download.

The config file for the database may be passed at the command line
but defaults to dev.cfg.

The script must be run from the main whatwhat directory.

import os
from pyftpdlib import ftpserver
from whatwhat.model import User
import pkg_resources

defaultworkingdirectory = ‘c:\\temp\\images\\’

import cherrypy
import turbogears
from os.path import *
import sys
if len(sys.argv) > 1:
elif exists(join(dirname(__file__), “”)):

class TurboGearsUserAuthorizer(ftpserver.DummyAuthorizer):
def validate_authentication(self, uploadname, uploadpassword):
“””Return True if the supplied username and password match the stored
userlist = == “%s” %uploadname)
if userlist.count() == 1:
return userlist[0].validate_password(uploadpassword)

return False
def get_home_dir(self, username):
“””Return the user’s home directory.”””
workingdirectory = “%s%s” %(defaultworkingdirectory,username)
return workingdirectory

def has_user(self, uploadname):
“””Whether the username exists in the virtual users table.”””
userlist = == “%s” %uploadname)
if userlist.count() == 1:
return True
return False

def get_msg_login(self, username):
“””Return the user’s login message.”””
return “Login message”

def get_msg_quit(self, username):
“””Return the user’s quitting message.”””
return “Quit message”

def r_perm(self, username, obj=None):
“””Whether the user has read permissions for obj (an absolute pathname
of a file or a directory)”””
return True

def w_perm(self, username, obj=None):
“””Whether the user has write permission for obj (an absolute pathname
of a file or a directory)”””
return True

authorizer = TurboGearsUserAuthorizer()
ftp_handler = ftpserver.FTPHandler
ftp_handler.authorizer = authorizer
ftp_handler.passive_ports = range(60000, 65535)
address = (”, 21)
ftpd = ftpserver.FTPServer(address, ftp_handler)


9 Responses to “Python FTP Server using Turbogears as an “Authorizer””

  1. Your house is valueble for me. Thanks!…

  2. Well I sincerely enjoyed reading it. This

    subject procured by you is very useful for proper planning.

  3. said

    You actually make it seem so easy with your presentation but I find this matter
    to be really

    something which I think I would never understand.
    It seems too complex and very broad for

    me. I’m looking forward for your next post, I will try to get the hang of it!

  4. This design is steller! You most certainly know how to

    keep a reader amused. Between your wit and your videos, I was almost moved to start my own blog (well,

    almost…HaHa!) Fantastic job. I really enjoyed what you had to
    say, and more than

    that, how you presented it. Too cool!

  5. Thanks for every other magnificent post. Where else may anyone wardrobe sort of information such the perfect
    method of writing? I’ve an exhibit next week, and We are with the try to find such info.

  6. Pretty section of content. I just stumbled upon your blog and
    in accession capital to say that I acquire actually
    enjoyed account your blog posts. Any way I’ll be subscribing to your augment and even I achievement you access constantly fast.

  7. This is the perfect blog for anyone who really wants to find out about this topic.

    You understand a whole lot its almost hard to argue with you (not that I actually will
    need to…HaHa). You definitely put a new spin on a subject which has been written about
    for decades. Wonderful stuff, just excellent!

  8. jamikaclark74 said

    Excellent website. A lot of useful

    information here. I am sending it to a few friends ans also

    sharing in delicious. And obviously, thanks on your


  9. Thanks for a marvelous posting! I quite enjoyed reading it, you
    can be a great author.I will make certain to bookmark
    your blog and may come back down the road.
    I want to encourage that you continue your
    great posts, have a nice day!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: