Skip to content

Instantly share code, notes, and snippets.

@codeskyblue
Created October 24, 2014 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeskyblue/dfcf1a77b8186128ecce to your computer and use it in GitHub Desktop.
Save codeskyblue/dfcf1a77b8186128ecce to your computer and use it in GitHub Desktop.
ftpserver which support .ftpignore
#!/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