Created
October 24, 2014 07:30
-
-
Save codeskyblue/dfcf1a77b8186128ecce to your computer and use it in GitHub Desktop.
ftpserver which support .ftpignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
# | |
import fnmatch | |
import os | |
import pathspec | |
import pyftpdlib | |
from pyftpdlib.filesystems import AbstractedFS | |
from pyftpdlib.authorizers import DummyAuthorizer | |
from pyftpdlib.handlers import FTPHandler | |
from pyftpdlib.servers import FTPServer | |
class ServerboxFS(AbstractedFS): | |
''' | |
support .ftpignore file | |
''' | |
def __init__(self, root, cmd_channel): | |
AbstractedFS.__init__(self, root, cmd_channel) | |
ignfile = os.path.join(root, '.ftpignore') | |
self._ignmatch = None | |
if self.isfile(ignfile): | |
with open(ignfile, 'r') as file: | |
spec = pathspec.PathSpec.from_lines('gitignore', file) | |
self._ignmatch = spec.match_files | |
def listdir(self, path): | |
names = os.listdir(path) | |
if not self._ignmatch: | |
return names | |
igns = self._ignmatch(names) | |
return list(set(names) - igns) | |
def ftpserver(port=2121, cwd=os.getcwd()): | |
authorizer = DummyAuthorizer() | |
authorizer.add_anonymous(cwd, perm='elradfmwM') | |
handler = FTPHandler | |
handler.authorizer = authorizer | |
handler.banner = "serverbox: pyftpdlib based ftpd ready." | |
handler.abstracted_fs = ServerboxFS | |
server = FTPServer(('', port), handler) | |
return server | |
if __name__ == '__main__': | |
server = ftpserver() | |
server.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment