Skip to content

Instantly share code, notes, and snippets.

@mpurbo
Created April 21, 2014 02:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpurbo/11130775 to your computer and use it in GitHub Desktop.
Save mpurbo/11130775 to your computer and use it in GitHub Desktop.
__author__ = 'purbo'
import os
import time
import threading
import urllib
import re
import logging
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
from boto.s3.connection import S3Connection
from boto.s3.key import Key
# ====================================
# settings
# ====================================
ftp_user_name = '<ftp_user>'
ftp_user_password = '<ftp_password>'
ftp_home_dir = '<path_to_ftp_home_dir>'
ftp_port = 21
s3_access_key_id = '<your_s3_key>'
s3_secret_access_key = '<your_s3_access_key>'
s3_bucket_name = '<your_s3_bucket>'
s3_dir = '<dir_inside_your_s3_bucket>'
log_path = '<path_to_ftp_log_file>'
log_level = logging.INFO
# ====================================
# S3 objects
s3_conn = S3Connection(s3_access_key_id, s3_secret_access_key)
s3_bucket = s3_conn.get_bucket(s3_bucket_name)
class MyHandler(FTPHandler):
def on_file_received(self, file):
# run a background process to upload file to s3 then delete it
def s3_upload_task():
try:
# upload to s3
logging.info('uploading file to S3: %s', os.path.basename(file))
image_path = create_file_path(os.path.basename(file))
image_key = s3_bucket.get_key(image_path)
if image_key is not None:
s3_bucket.delete_key(image_path)
image_key = Key(s3_bucket)
image_key.key = image_path
image_key.set_contents_from_filename(file)
# wait 5 secs for fun, and delete local file
time.sleep(5)
os.remove(file)
except Exception, e:
print logging.exception(e)
self.add_channel()
self.del_channel()
threading.Thread(target=s3_upload_task).start()
def on_incomplete_file_received(self, file):
# remove partially uploaded files
os.remove(file)
def create_file_path(file_key):
return s3_dir+ "/" + file_key
# from: https://github.com/bobeirasa/mini-scripts/blob/master/externalip.py
def get_ip():
group = re.compile(u'(?P<ip>\d+\.\d+\.\d+\.\d+)')\
.search(urllib.URLopener().open('http://jsonip.com/').read())\
.groupdict()
return group['ip']
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(ftp_user_name, ftp_user_password, ftp_home_dir, perm='elradfmwM')
# Instantiate FTP handler class
handler = MyHandler
handler.authorizer = authorizer
# setup logging
logging.basicConfig(filename=log_path, level=log_level, datefmt='%Y-%m-%d %H:%M:%S')
# Define a customized banner (string returned when client connects)
handler.banner = "My custom ftpd ready."
# Specify a masquerade address and the range of ports to use for
# passive connections. Decomment in case you're behind a NAT.
handler.masquerade_address = get_ip()
handler.passive_ports = range(60000, 65535)
# Instantiate FTP server class and listen on 0.0.0.0:<ftp_port>
address = ('', ftp_port)
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment