Skip to content

Instantly share code, notes, and snippets.

@jslay88
Created July 10, 2018 06:51
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 jslay88/600f32d4fd550ba20a74df9df932f642 to your computer and use it in GitHub Desktop.
Save jslay88/600f32d4fd550ba20a74df9df932f642 to your computer and use it in GitHub Desktop.
Simple utility to upload a file via FTP - Python
#!/usr/bin/env python
import ntpath
import argparse
from ftplib import FTP
def upload_file(host, port,
username, password,
file_path, remote_path):
ftp = FTP()
ftp.connect(host=host, port=port)
ftp.login(username, password)
if remote_path:
paths = remote_path.rstrip('/').split('/')
for path in paths:
ftp.cwd(path if path != '' else '/')
head, tail = ntpath.split(file_path)
ftp.storbinary('STOR %s' % tail or ntpath.basename(head), open(file_path, 'rb'))
ftp.quit()
def parse_args():
parser = argparse.ArgumentParser(description='Simple utility to upload a file via FTP.')
parser.add_argument('-s', '--server', help='FTP Server Address (Hostname or IP).', required=True)
parser.add_argument('-p', '--port', help='FTP Server Port. Default: 21', default=21, type=int)
parser.add_argument('-u', '--username', help='FTP Username. Default is anonymous.', default='anonymous')
parser.add_argument('-P', '--password', help='FTP Password. Default is anonymous@.', default='anonymous@')
parser.add_argument('-f', '--file', help='Path to file that you wish to upload.', required=True)
parser.add_argument('-r', '--remote-path', help='Remote folder path to store the file. Absolute or relative. '
'If not defined, will store in FTP home directory.', default=None)
return parser.parse_args()
if __name__ == '__main__':
args = parse_args()
upload_file(args.server, args.port,
args.username, args.password,
args.file, args.remote_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment