NICKPODD

Git and FTP Server in Python


Below are a couple of examples of code that let you create an ftp or git server locally. Useful if you are want to transfer a repo or a set of files accross a network.

FTP Server

This uses pyftpdlib to create an ftp server. And will simply server files from the location the script is ran from.

import os

from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer

def main():
    # Instantiate a dummy authorizer for managing 'virtual' users
    authorizer = DummyAuthorizer()

    # Define a new user having full r/w permissions and a read-only
    # anonymous user
    authorizer.add_user('myuser', 'abcdefg123456', '.', perm='elradfmwM')
    authorizer.add_anonymous(os.getcwd())

    # Instantiate FTP handler class
    handler = FTPHandler
    handler.authorizer = authorizer

    # Define a customized banner (string returned when client connects)
    handler.banner = "pyftpdlib based ftpd ready."

    # Instantiate FTP server class and listen on 0.0.0.0:2121
    address = ('0.0.0.0', 2121)
    server = FTPServer(address, handler)

    # set a limit for connections
    server.max_cons = 256
    server.max_cons_per_ip = 5

    # start ftp server
    server.serve_forever()

if __name__ == '__main__':
    main()

Git Server

This uses dulwich and the werksung server to create a git server. Just specifiy the repositories and their locations and you are good to go.

from dulwich.repo import Repo
from dulwich.web import HTTPGitApplication
from dulwich import server as dulserver
from werkzeug import run_simple
from werkzeug.serving import make_ssl_devcert

context = make_ssl_devcert('key/yourserver', host='localhost')

repo_path = "/path/to/your/repo"

_d = {'/mti' : Repo(repo_path)}
backend = dulserver.DictBackend(_d)
gitserve = HTTPGitApplication(backend)

def app(environ, start_response):
    return gitserve(environ, start_response)

run_simple('localhost', 8080, app, use_reloader=True, ssl_context = context)




See me at Linked In See me at Google Plus

Page by: Nicholas Podd , Nick Podd
Love you Emilia